Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new full_address_as_hash method which return the required address… #1965

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
AmrAdelKhalil marked this conversation as resolved.
Show resolved Hide resolved
# :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 @@ -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