Class: ISO3166::Country

Inherits:
Object
  • Object
show all
Defined in:
lib/countries/country.rb

Direct Known Subclasses

Country

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,
  :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
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(country_data) ⇒ Country

Returns a new instance of Country.



40
41
42
# File 'lib/countries/country.rb', line 40

def initialize(country_data)
  @data = country_data.is_a?(Hash) ? country_data : Data[country_data]
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



38
39
40
# File 'lib/countries/country.rb', line 38

def data
  @data
end

Class Method Details

.[](query) ⇒ Object



79
80
81
# File 'lib/countries/country.rb', line 79

def [](query)
  self.search(query)
end

.all(&blk) ⇒ Object Also known as: countries



67
68
69
70
# File 'lib/countries/country.rb', line 67

def all(&blk)
  blk ||= Proc.new { |country ,data| [data['name'], country] }
  Data.map &blk
end

.find_all_by(attribute, val) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/countries/country.rb', line 97

def find_all_by(attribute, val)
  raise "Invalid attribute name '#{attribute}'" unless AttrReaders.include?(attribute.to_sym)
  attribute = attribute.to_s
  if val.is_a?(Regexp)
    val = Regexp.new(val.source, 'i')
  else
    val = val.to_s.downcase
  end
  attribute = ['name', 'names'] if attribute == 'name'
  Data.select do |k,v|
    Array(attribute).map do |attr|
      if v[attr].kind_of?(Enumerable)
        v[attr].any?{ |n| val === n.downcase }
      else
        v[attr] && val === v[attr].downcase
      end
    end.uniq.include?(true)
  end
end

.method_missing(*m) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/countries/country.rb', line 83

def method_missing(*m)
  if m.first.to_s.match /^find_(country_)?by_(.+)/
    country = self.find_all_by($~[2].downcase, m[1]).first
    $~[1].nil? ? country : self.new(country.last) if country
  elsif m.first.to_s.match /^find_all_(countries_)?by_(.+)/
    self.find_all_by($~[2].downcase, m[1]).inject([]) do |list, c|
      list << ($~[1].nil? ? c : self.new(c.last)) if c
      list
    end
  else
    super
  end
end

.search(query) ⇒ Object



74
75
76
77
# File 'lib/countries/country.rb', line 74

def search(query)
  country = self.new(query.to_s.upcase)
  country.valid? ? country : false
end

Instance Method Details

#==(other) ⇒ Object



48
49
50
# File 'lib/countries/country.rb', line 48

def ==(other)
  self.data == other.data
end

#currencyObject



52
53
54
# File 'lib/countries/country.rb', line 52

def currency
  ISO4217::Currency.from_code(@data['currency'])
end

#subdivisionsObject Also known as: states



56
57
58
# File 'lib/countries/country.rb', line 56

def subdivisions
  @subdivisions ||= subdivisions? ? YAML.load_file(File.join(File.dirname(__FILE__), '..', 'data', 'subdivisions', "#{alpha2}.yaml")) : {}
end

#subdivisions?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/countries/country.rb', line 62

def subdivisions?
  File.exist?(File.join(File.dirname(__FILE__), '..', 'data', 'subdivisions', "#{alpha2}.yaml"))
end

#valid?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/countries/country.rb', line 44

def valid?
  !!@data
end