From f4e956a3984bbc83b70d136d85898046bac5730b Mon Sep 17 00:00:00 2001 From: Alexey Kuleshov <kulesa@gmail.com> Date: Tue, 24 Dec 2013 17:29:15 +0200 Subject: [PATCH] Allow arbitrary attributes on Geocoder::Result::Test --- lib/geocoder/results/test.rb | 18 ++++++++++++++++-- test/test_mode_test.rb | 9 +++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/geocoder/results/test.rb b/lib/geocoder/results/test.rb index 99ceef0c..b0e6bef2 100644 --- a/lib/geocoder/results/test.rb +++ b/lib/geocoder/results/test.rb @@ -4,12 +4,26 @@ module Geocoder module Result class Test < Base + def self.add_result_attribute(attr) + return if respond_to?(attr.to_sym) + + define_method(attr) do + @data[attr.to_s] || @data[attr.to_sym] + end + end + %w[latitude longitude neighborhood city state state_code sub_state sub_state_code province province_code postal_code country country_code address street_address street_number route geometry].each do |attr| - define_method(attr) do - @data[attr.to_s] || @data[attr.to_sym] + add_result_attribute(attr) + end + + def initialize(data) + data.keys.each do |attr| + Test.add_result_attribute(attr) end + + super end end end diff --git a/test/test_mode_test.rb b/test/test_mode_test.rb index 7f4b6115..62911c55 100644 --- a/test/test_mode_test.rb +++ b/test/test_mode_test.rb @@ -42,6 +42,15 @@ class TestModeTest < Test::Unit::TestCase end end + def test_search_with_custom_attributes + custom_attributes = mock_attributes.merge(:custom => 'NY, NY') + Geocoder::Lookup::Test.add_stub("New York, NY", [custom_attributes]) + + result = Geocoder.search("New York, NY").first + + assert_equal 'NY, NY', result.custom + end + private def mock_attributes coordinates = [40.7143528, -74.0059731] -- GitLab