class ISO3166::Country

def [](query)

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

def all

def all
  Data.map { |country,data| [data['name'],country] }
end

def currency

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

def find_all_by(attribute, val)

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

def initialize(country_data)

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

def method_missing(*m)

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

def search(query)

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

def subdivisions

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

def subdivisions?

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

def valid?

def valid?
  !!@data
end