Sha256: 8cf38669ad41a805793cd7468f9401cb2810bf80065e2571ba86ca4a83f0275b

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

require File.join(File.dirname(__FILE__), 'countries', 'select_helper.rb')
require 'YAML'

class Country
  attr_reader :data
   
  AttrReaders = [
    :number,
    :alpha2,
    :alpha3,
    :name,
    :names,
    :latitude,
    :longitude,
    :region,
    :subregion,
    :country_code,
    :national_destination_code_lengths,
    :national_number_lengths,
    :international_prefix,
    :national_prefix,
    :address_format,
    :currency
  ]
  
  AttrReaders.each do |meth|
    define_method meth do
      @data[meth.to_s]
    end
  end
  
  Data = YAML.load_file(File.join(File.dirname(__FILE__), 'data', 'countries.yaml')) || {}
  Names = Data.map {|k,v| [v['name'],k]}.sort
  NameIndex = Hash[*Names.flatten]
    
  def initialize(country_code)
    @data = Data[country_code]
  end
  
  def self.search(query)
    Country.new(query)
  end
  
  def self.[](query)
    self.search(query)
  end
  
  def subdivisions
    @subdivisions ||= subdivisions? ? YAML.load_file(File.join(File.dirname(__FILE__), 'data', 'subdivisions', "#{alpha2}.yaml")) : {}
  end
  
  def subdivisions?
    File.exist?(File.join(File.dirname(__FILE__), 'data', 'subdivisions', "#{alpha2}.yaml"))
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
countries-0.2.2 lib/countries.rb