Skip to content

Instantly share code, notes, and snippets.

@sterrym
Created March 23, 2009 15:37
Show Gist options
  • Save sterrym/83600 to your computer and use it in GitHub Desktop.
Save sterrym/83600 to your computer and use it in GitHub Desktop.
define_builder(Country) do |klass, overrides|
# set up some presets
canada = { :iso => 'CA', :name => 'CANADA', :printable_name => 'Canada', :iso3 => 'CAN', :numcode => '124'}
usa = { :iso => 'US', :name => 'UNITED STATES', :printable_name => 'United States', :iso3 => 'USA', :numcode => '840' }
uk = { :iso => 'GB', :name => 'UNITED KINGDOM', :printable_name => 'United Kingdom', :iso3 => 'GBR', :numcode => '826' }
zimbabwe = { :iso => 'ZW', :name => 'ZIMBABWE', :printable_name => 'Zimbabwe', :iso3 => 'ZWE', :numcode => '716' }
# set up the default
country_attributes = canada
# check for a preset
overrides.process(:preset) do |preset|
case preset.downcase
when "usa"
overrides = usa
when "uk"
overrides = uk
when "other"
overrides = zimbabwe
end
end
klass.new(country_attributes)
end
--
>> new_country(:preset => "uk")
=> #<Country id: nil, iso: "CA", name: "CANADA", printable_name: "Canada", iso3: "CAN", numcode: 124>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment