Skip to content

Commit

Permalink
Merge pull request #1965 from AmrAdelKhalil/address/full_address_as_hash
Browse files Browse the repository at this point in the history
Add new full_address_as_hash method which return the required address…
  • Loading branch information
Zeragamba committed Jun 10, 2020
2 parents a09c5d4 + 24c8797 commit 7a67bb4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/faker/default/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,36 @@ 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::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)
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
8 changes: 8 additions & 0 deletions test/faker/default/test_faker_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,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

0 comments on commit 7a67bb4

Please sign in to comment.