Skip to content

Commit

Permalink
Added method_missing to Address to allow locale-specific datasets to …
Browse files Browse the repository at this point in the history
…be added to the locale file and show up as methods. Removed the uk_country method in favor of this approach.
  • Loading branch information
stympy committed Dec 22, 2010
1 parent b232362 commit 264d78d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
19 changes: 13 additions & 6 deletions lib/faker/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,23 @@ def zip_code
alias_method :zip, :zip_code
alias_method :postcode, :zip_code

# UK Variant
def uk_country
['England', 'Scotland', 'Wales', 'Northern Ireland'].rand
end

%w(street_suffix city_suffix city_prefix state_abbr state country county).each do |meth|
define_method(meth) do
I18n.translate("address.#{meth}").rand
end
end
end

# You can add whatever you want to the locale file, and it will get
# caught here... e.g., create a country_code array in your locale,
# then you can call #country_code and it will act like #country
def method_missing(m, *args, &block)
# Use the alternate form of translate to get a nil rather than a "missing translation" string
if translation = I18n.translate(:address)[m]
translation.respond_to?(:rand) ? translation.rand : translation
else
super
end
end

# Deprecated
alias_method :earth_country, :country
Expand Down
3 changes: 2 additions & 1 deletion lib/locales/en-gb.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
en-gb:
address:
postcode: ['??# #??', '??## #??']
county: [Avon, Bedfordshire, Berkshire, Borders, Buckinghamshire, Cambridgeshire, Central, Cheshire, Cleveland, Clwyd, Cornwall, County Antrim, County Armagh, County Down, County Fermanagh, County Londonderry, County Tyrone, Cumbria, Derbyshire, Devon, Dorset, Dumfries and Galloway, Durham, Dyfed, East Sussex, Essex, Fife, Gloucestershire, Grampian, Greater Manchester, Gwent, Gwynedd County, Hampshire, Herefordshire, Hertfordshire, Highlands and Islands, Humberside, Isle of Wight, Kent, Lancashire, Leicestershire, Lincolnshire, Lothian, Merseyside, Mid Glamorgan, Norfolk, North Yorkshire, Northamptonshire, Northumberland, Nottinghamshire, Oxfordshire, Powys, Rutland, Shropshire, Somerset, South Glamorgan, South Yorkshire, Staffordshire, Strathclyde, Suffolk, Surrey, Tayside, Tyne and Wear, Warwickshire, West Glamorgan, West Midlands, West Sussex, West Yorkshire, Wiltshire, Worcestershire]
county: [Avon, Bedfordshire, Berkshire, Borders, Buckinghamshire, Cambridgeshire, Central, Cheshire, Cleveland, Clwyd, Cornwall, County Antrim, County Armagh, County Down, County Fermanagh, County Londonderry, County Tyrone, Cumbria, Derbyshire, Devon, Dorset, Dumfries and Galloway, Durham, Dyfed, East Sussex, Essex, Fife, Gloucestershire, Grampian, Greater Manchester, Gwent, Gwynedd County, Hampshire, Herefordshire, Hertfordshire, Highlands and Islands, Humberside, Isle of Wight, Kent, Lancashire, Leicestershire, Lincolnshire, Lothian, Merseyside, Mid Glamorgan, Norfolk, North Yorkshire, Northamptonshire, Northumberland, Nottinghamshire, Oxfordshire, Powys, Rutland, Shropshire, Somerset, South Glamorgan, South Yorkshire, Staffordshire, Strathclyde, Suffolk, Surrey, Tayside, Tyne and Wear, Warwickshire, West Glamorgan, West Midlands, West Sussex, West Yorkshire, Wiltshire, Worcestershire]
uk_country: [England, Scotland, Wales, Northern Ireland]

0 comments on commit 264d78d

Please sign in to comment.