Class: ISO3166::Country
- Inherits:
-
Object
- Object
- ISO3166::Country
- Defined in:
- lib/countries/country.rb,
lib/countries/mongoid.rb
Direct Known Subclasses
Constant Summary collapse
- Setup =
ISO3166::Setup.new
- AttrReaders =
[ :number, :alpha2, :alpha3, :name, :names, :latitude, :longitude, :continent, :region, :subregion, :world_region, :country_code, :national_destination_code_lengths, :national_number_lengths, :international_prefix, :national_prefix, :address_format, :translations, :translated_names, :ioc, :gec, :un_locode, :languages, :nationality, :dissolved_on, :eu_member, :alt_currency, :vat_rates, :postal_code, :min_longitude, :min_latitude, :max_longitude, :max_latitude, :latitude_dec, :longitude_dec ]
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
- .[](query) ⇒ Object
- .all(&blk) ⇒ Object (also: countries)
- .all_names_with_codes(locale = 'en') ⇒ Object
- .all_translated(locale = 'en') ⇒ Object
- .codes ⇒ Object
- .demongoize(alpha2) ⇒ Object
- .evolve(country) ⇒ Object
- .find_all_by(attribute, val) ⇒ Object
- .method_missing(*m) ⇒ Object
- .mongoize(country) ⇒ Object
- .new(country_data) ⇒ Object
- .search(query) ⇒ Object
- .translations(locale = 'en') ⇒ Object
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #currency ⇒ Object
- #currency_code ⇒ Object
- #in_eu? ⇒ Boolean
-
#initialize(country_data) ⇒ Country
constructor
A new instance of Country.
- #mongoize ⇒ Object
- #subdivisions ⇒ Object (also: #states)
- #subdivisions? ⇒ Boolean
- #to_s ⇒ Object
- #translation(locale = 'en') ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
50 51 52 |
# File 'lib/countries/country.rb', line 50 def data @data end |
Class Method Details
.[](query) ⇒ Object
141 142 143 |
# File 'lib/countries/country.rb', line 141 def [](query) search(query) end |
.all(&blk) ⇒ Object Also known as: countries
115 116 117 118 |
# File 'lib/countries/country.rb', line 115 def all(&blk) blk ||= proc {|alpha2, d| ISO3166::Country.new(d)} Setup.data.map(&blk) end |
.all_names_with_codes(locale = 'en') ⇒ Object
126 127 128 129 130 |
# File 'lib/countries/country.rb', line 126 def all_names_with_codes(locale = 'en') ISO3166::Country.all.map do |c| [(c.translation(locale) || c.name ).html_safe, c.alpha2] end.sort_by { |d| d[0] } end |
.all_translated(locale = 'en') ⇒ Object
122 123 124 |
# File 'lib/countries/country.rb', line 122 def all_translated(locale = 'en') translations(locale).values end |
.codes ⇒ Object
111 112 113 |
# File 'lib/countries/country.rb', line 111 def codes Setup.codes end |
.demongoize(alpha2) ⇒ Object
19 20 21 |
# File 'lib/countries/mongoid.rb', line 19 def demongoize(alpha2) new(alpha2) end |
.evolve(country) ⇒ Object
23 24 25 |
# File 'lib/countries/mongoid.rb', line 23 def evolve(country) mongoize(country) end |
.find_all_by(attribute, val) ⇒ Object
153 154 155 156 157 158 159 160 161 |
# File 'lib/countries/country.rb', line 153 def find_all_by(attribute, val) attributes, value = parse_attributes(attribute, val) Setup.data.select do |_, v| attributes.map do |attr| Array(v[attr]).any? { |n| value === n.to_s.downcase } end.include?(true) end end |
.method_missing(*m) ⇒ Object
145 146 147 148 149 150 151 |
# File 'lib/countries/country.rb', line 145 def method_missing(*m) regex = m.first.to_s.match(/^find_(all_)?(country_|countries_)?by_(.+)/) super unless regex countries = find_by(Regexp.last_match[3], m[1], Regexp.last_match[2]) Regexp.last_match[1] ? countries : countries.last end |
.mongoize(country) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/countries/mongoid.rb', line 9 def mongoize(country) if country.is_a?(self) && !country.data.nil? country.alpha2 elsif send(:valid_alpha2?, country) new(country).alpha2 else nil end end |
.new(country_data) ⇒ Object
105 106 107 108 109 |
# File 'lib/countries/country.rb', line 105 def new(country_data) if country_data.is_a?(Hash) || codes.include?(country_data.to_s.upcase) super end end |
.search(query) ⇒ Object
136 137 138 139 |
# File 'lib/countries/country.rb', line 136 def search(query) country = new(query.to_s.upcase) (country && country.valid?) ? country : nil end |
.translations(locale = 'en') ⇒ Object
132 133 134 |
# File 'lib/countries/country.rb', line 132 def translations(locale = 'en') I18nData.countries(locale.upcase) end |
Instance Method Details
#<=>(other) ⇒ Object
68 69 70 |
# File 'lib/countries/country.rb', line 68 def <=>(other) self.to_s <=> other.to_s end |
#==(other) ⇒ Object
64 65 66 |
# File 'lib/countries/country.rb', line 64 def ==(other) other == data end |
#currency ⇒ Object
72 73 74 |
# File 'lib/countries/country.rb', line 72 def currency ISO4217::Currency.from_code(@data['currency']) end |
#currency_code ⇒ Object
76 77 78 |
# File 'lib/countries/country.rb', line 76 def currency_code @data['currency'] end |
#in_eu? ⇒ Boolean
90 91 92 |
# File 'lib/countries/country.rb', line 90 def in_eu? @data['eu_member'].nil? ? false : @data['eu_member'] end |
#mongoize ⇒ Object
4 5 6 |
# File 'lib/countries/mongoid.rb', line 4 def mongoize ISO3166::Country.mongoize(self) end |
#subdivisions ⇒ Object Also known as: states
80 81 82 |
# File 'lib/countries/country.rb', line 80 def subdivisions @subdivisions ||= subdivisions? ? YAML.load_file(File.join(File.dirname(__FILE__), '..', 'data', 'subdivisions', "#{alpha2}.yaml")) : {} end |
#subdivisions? ⇒ Boolean
86 87 88 |
# File 'lib/countries/country.rb', line 86 def subdivisions? File.exist?(File.join(File.dirname(__FILE__), '..', 'data', 'subdivisions', "#{alpha2}.yaml")) end |
#to_s ⇒ Object
94 95 96 |
# File 'lib/countries/country.rb', line 94 def to_s @data['name'] end |
#translation(locale = 'en') ⇒ Object
98 99 100 |
# File 'lib/countries/country.rb', line 98 def translation(locale = 'en') @data['translations'][locale.downcase] end |
#valid? ⇒ Boolean
56 57 58 |
# File 'lib/countries/country.rb', line 56 def valid? !(@data.nil? || @data.empty?) end |