Sha256: d88add0765f504b1459ebe0f34c96d1a417c4ae6ddd25308a75a681cac56b9a9

Contents?: true

Size: 1023 Bytes

Versions: 3

Compression:

Stored size: 1023 Bytes

Contents

require 'yaml'

class Country
  @@country_codes = nil
  @@cached_countries = nil

  attr_reader :data

  ATTR_READERS = [
    :alpha2,
    :name,
    :eu_member
  ]

  ATTR_READERS.each do |meth|
    define_method meth do
      @data[meth.to_s]
    end
  end

  def initialize(country_code)
    @data = CountryList::Data.new(country_code).call
  end

  def <=>(other)
    to_s <=> other.to_s
  end

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

  def valid?
    !(@data.nil? || @data.empty?)
  end

  def to_s
    name
  end

  def in_europe?
    @data && @data['eu_member'].nil? ? false : @data['eu_member']
  end
  alias_method :eu_member?, :in_europe?

  class << self

    # Returns big array with all countries
    def all
      CountryList::Data.countries.map{ |country_code, data| Country.new(country_code) }
    end

    def [](country_code)
      country = new(country_code)
      (country && country.valid?) ? country : nil
    end

    def country_codes
      CountryList::Data.country_codes
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
country_list-0.1.3 lib/country_list/country.rb
country_list-0.1.2 lib/country_list/country.rb
country_list-0.1.1 lib/country_list/country.rb