From 0f482fb093aa82dbeff039496c3e7a18ab248e0f Mon Sep 17 00:00:00 2001 From: Amr Adel Date: Sat, 18 Apr 2020 18:18:51 +0200 Subject: [PATCH 1/3] Add new full_address_as_hash method which return the required address information as a hash --- lib/faker/default/address.rb | 21 +++++++++++++++++++++ test/faker/default/test_faker_address.rb | 8 ++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/faker/default/address.rb b/lib/faker/default/address.rb index 9fca4af6b0..8ddd5172fb 100644 --- a/lib/faker/default/address.rb +++ b/lib/faker/default/address.rb @@ -333,6 +333,27 @@ def longitude def full_address parse('address.full_address') end + + ## + # Produces Address hash of required fields + # + # @return [Hash] + # + # @example + # Faker::Address.full_address_as_hash(:longitude, + # :latitude, + # :country_name_to_code, + # country_name_to_code: {name: 'united_states'}) + # #=> {:longitude=>-101.74428917174603, :latitude=>-37.40056749089944, :country_name_to_code=>"US"} + # + # @faker.version 2.11.0 + def full_address_as_hash(*attrs, **attrs_params) + attrs.map!(&:to_sym) + attrs_params.transform_keys!(&:to_sym) + attrs.map do |attr| + { "#{attr}": attrs_params[attr] ? send(attr, **attrs_params[attr]) : send(attr) } + end.reduce({}, :merge) + end end end end diff --git a/test/faker/default/test_faker_address.rb b/test/faker/default/test_faker_address.rb index 339feca939..045c7b09fa 100644 --- a/test/faker/default/test_faker_address.rb +++ b/test/faker/default/test_faker_address.rb @@ -86,4 +86,12 @@ def test_longitude def test_full_address assert @tester.full_address.match(/\w*\.?\s?\d*\s?\d+\s\w+\s\w+,\s\w+\s?\w*,\s[A-Z]{2}\s\d+/) end + + def test_full_address_as_hash + assert_instance_of Hash, @tester.full_address_as_hash + end + + def test_full_address_as_hash_by_longitude + assert_instance_of Float, @tester.full_address_as_hash(:longitude)[:longitude] + end end From c8b1602e4b9cc45fea9bd836d32ba40c7f7207ae Mon Sep 17 00:00:00 2001 From: Amr Adel Date: Wed, 20 May 2020 01:39:34 +0200 Subject: [PATCH 2/3] Update faker version documentation to be "next" Co-authored-by: Stephen A. Wilson --- lib/faker/default/address.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/faker/default/address.rb b/lib/faker/default/address.rb index 8ddd5172fb..54dc96f693 100644 --- a/lib/faker/default/address.rb +++ b/lib/faker/default/address.rb @@ -346,7 +346,7 @@ def full_address # country_name_to_code: {name: 'united_states'}) # #=> {:longitude=>-101.74428917174603, :latitude=>-37.40056749089944, :country_name_to_code=>"US"} # - # @faker.version 2.11.0 + # @faker.version next def full_address_as_hash(*attrs, **attrs_params) attrs.map!(&:to_sym) attrs_params.transform_keys!(&:to_sym) From 24c8797656e63057f0e52c6c44f2712f859f404b Mon Sep 17 00:00:00 2001 From: Amr Adel Date: Wed, 20 May 2020 01:56:23 +0200 Subject: [PATCH 3/3] Add more examples for clarifying full_address_as_hash usages --- lib/faker/default/address.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/faker/default/address.rb b/lib/faker/default/address.rb index 54dc96f693..d7d108948e 100644 --- a/lib/faker/default/address.rb +++ b/lib/faker/default/address.rb @@ -346,6 +346,15 @@ def full_address # country_name_to_code: {name: 'united_states'}) # #=> {:longitude=>-101.74428917174603, :latitude=>-37.40056749089944, :country_name_to_code=>"US"} # + # Faker::Address.full_address_as_hash(:full_address) + # #=> {:full_address=>"87635 Rice Street, Lake Brentonton, OR 61896-5968"} + # + # Faker::Address.full_address_as_hash(:city, :time_zone) + # #=> {:city=>"East Faustina", :time_zone=>"America/Mexico_City"} + # + # Faker::Address.full_address_as_hash(:street_address, street_address: {include_secondary: true}) + # #=> {:street_address=>"29423 Kenneth Causeway Suite 563"} + # # @faker.version next def full_address_as_hash(*attrs, **attrs_params) attrs.map!(&:to_sym)