Class: ISO3166::Country
- Inherits:
-
Object
show all
- Defined in:
- lib/countries/country.rb,
lib/countries/mongoid.rb
Defined Under Namespace
Classes: BadMongoidTypeError
Constant Summary
collapse
- Data =
YAML.load_file(File.join(File.dirname(__FILE__), '..', 'data', 'countries.yaml')) || {}
- Names =
Data.map {|k,v| [v['name'],k]}.sort_by { |d| d[0] }
- NameIndex =
- AttrReaders =
[
:number,
:alpha2,
:alpha3,
:currency,
:name,
:names,
:translations,
:latitude,
:longitude,
:continent,
:region,
:subregion,
:country_code,
:national_destination_code_lengths,
:national_number_lengths,
:international_prefix,
:national_prefix,
:address_format,
:ioc,
:un_locode,
:languages,
:nationality,
:eu_member
]
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(country_data) ⇒ Country
Returns a new instance of Country.
42
43
44
|
# File 'lib/countries/country.rb', line 42
def initialize(country_data)
@data = country_data.is_a?(Hash) ? country_data : Data[country_data.to_s.upcase]
end
|
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
40
41
42
|
# File 'lib/countries/country.rb', line 40
def data
@data
end
|
Class Method Details
.[](query) ⇒ Object
99
100
101
|
# File 'lib/countries/country.rb', line 99
def [](query)
self.search(query)
end
|
.all(&blk) ⇒ Object
Also known as:
countries
87
88
89
90
|
# File 'lib/countries/country.rb', line 87
def all(&blk)
blk ||= Proc.new { |country ,data| [data['name'], country] }
Data.map &blk
end
|
.demongoize(alpha2) ⇒ Object
22
23
24
|
# File 'lib/countries/mongoid.rb', line 22
def demongoize(alpha2)
new(alpha2)
end
|
.evolve(country) ⇒ Object
26
27
28
|
# File 'lib/countries/mongoid.rb', line 26
def evolve(country)
mongoize(country)
end
|
.find_all_by(attribute, val) ⇒ Object
111
112
113
114
115
116
117
118
119
|
# File 'lib/countries/country.rb', line 111
def find_all_by(attribute, val)
attributes, value = parse_attributes(attribute, val)
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
103
104
105
106
107
108
109
|
# File 'lib/countries/country.rb', line 103
def method_missing(*m)
regex = m.first.to_s.match(/^find_(all_)?(country_|countries_)?by_(.+)/)
super unless regex
countries = self.find_by($3, m[1], $2)
$1 ? countries : countries.last
end
|
.mongoize(country) ⇒ Object
12
13
14
15
16
17
18
19
20
|
# File 'lib/countries/mongoid.rb', line 12
def mongoize(country)
if country.is_a?(self) && !country.data.nil?
country.alpha2
elsif self.send(:valid_alpha2?, country)
new(country).alpha2
else
raise BadMongoidTypeError.new('Given value is neither a valid country object nor a valid alpha2 code')
end
end
|
.new(country_data) ⇒ Object
81
82
83
84
85
|
# File 'lib/countries/country.rb', line 81
def new(country_data)
if country_data.is_a?(Hash) || Data.keys.include?(country_data.to_s.upcase)
super
end
end
|
.search(query) ⇒ Object
94
95
96
97
|
# File 'lib/countries/country.rb', line 94
def search(query)
country = self.new(query.to_s.upcase)
(country && country.valid?) ? country : nil
end
|
Instance Method Details
#==(other) ⇒ Object
50
51
52
|
# File 'lib/countries/country.rb', line 50
def ==(other)
self.data == other.data
end
|
#currency ⇒ Object
54
55
56
|
# File 'lib/countries/country.rb', line 54
def currency
ISO4217::Currency.from_code(@data['currency'])
end
|
#currency_code ⇒ Object
58
59
60
|
# File 'lib/countries/country.rb', line 58
def currency_code
@data['currency']
end
|
#in_eu? ⇒ Boolean
72
73
74
|
# File 'lib/countries/country.rb', line 72
def in_eu?
@data['eu_member'].nil? ? false : @data['eu_member']
end
|
#subdivisions ⇒ Object
Also known as:
states
62
63
64
|
# File 'lib/countries/country.rb', line 62
def subdivisions
@subdivisions ||= subdivisions? ? YAML.load_file(File.join(File.dirname(__FILE__), '..', 'data', 'subdivisions', "#{alpha2}.yaml")) : {}
end
|
#subdivisions? ⇒ Boolean
68
69
70
|
# File 'lib/countries/country.rb', line 68
def subdivisions?
File.exist?(File.join(File.dirname(__FILE__), '..', 'data', 'subdivisions', "#{alpha2}.yaml"))
end
|
#to_s ⇒ Object
76
77
78
|
# File 'lib/countries/country.rb', line 76
def to_s
@data['name']
end
|
#valid? ⇒ Boolean
46
47
48
|
# File 'lib/countries/country.rb', line 46
def valid?
not (@data.nil? or @data.empty?)
end
|