Module: ISO::Countries::CountryField::ClassMethods
- Defined in:
- lib/iso/countries/country_field.rb
Instance Method Summary collapse
-
#iso_country(*args) ⇒ Object
Declares a field from a model as a iso code for a country.
Instance Method Details
#iso_country(*args) ⇒ Object
Declares a field from a model as a iso code for a country
Example:
class Company
iso_country :country
end
c = Company.new(:country => "es")
c.country_name # => "Spain"
c.country_name = "France"
c.country # => "fr"
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/iso/countries/country_field.rb', line 21 def iso_country(*args) args.each do |f| class_eval <<-EOC validates_inclusion_of :#{f}, :in => ISO::Countries.country_codes, :allow_nil => true def #{f}_name ISO::Countries.get_country(#{f}) end def #{f}_name=(name) code = ISO::Countries.get_code(name) if code self.#{f} = code else raise ArgumentError, "Invalid country name" end end EOC end end |