class Country

Attributes

alpha3[R]
countries[RW]
currency[R]

@return [Integer] Returns the Number of country. @return [Integer] Returns the ISO 3166-1 code of country. @return [Integer] Returns the ISO 4217 code of currency. @return [String] Returns the Name of country. @return [String] Returns the Symbol of currency.

iso[R]

@return [Integer] Returns the Number of country. @return [Integer] Returns the ISO 3166-1 code of country. @return [Integer] Returns the ISO 4217 code of currency. @return [String] Returns the Name of country. @return [String] Returns the Symbol of currency.

name[R]

@return [Integer] Returns the Number of country. @return [Integer] Returns the ISO 3166-1 code of country. @return [Integer] Returns the ISO 4217 code of currency. @return [String] Returns the Name of country. @return [String] Returns the Symbol of currency.

number[R]

@return [Integer] Returns the Number of country. @return [Integer] Returns the ISO 3166-1 code of country. @return [Integer] Returns the ISO 4217 code of currency. @return [String] Returns the Name of country. @return [String] Returns the Symbol of currency.

symbol[R]

@return [Integer] Returns the Number of country. @return [Integer] Returns the ISO 3166-1 code of country. @return [Integer] Returns the ISO 4217 code of currency. @return [String] Returns the Name of country. @return [String] Returns the Symbol of currency.

Public Class Methods

all() click to toggle source

All countries

Example

Country.all

Returns an Array with value of all country objects

# File lib/country_with_currency/country.rb, line 34
def all
  countries
end
method_missing(*args) click to toggle source

Overriding method_missing to add dynamic finders.

*args - standard method_missing arguments

Examples

Country.find_by_iso(“USA”) Country.find_by_name(“United States”)

Returns an Array with value of country object/objects Raises NoMethodError if couldn’t find a method Raises UnknownAttribute if couldn’t find a valid attribute Raises UnknownCountry if couldn’t find any country object

Calls superclass method
# File lib/country_with_currency/country.rb, line 51
def method_missing(*args)
  regex = args.first.to_s.match(/^find_by_(.*)/)
  # Check if the missing method applies to Country
  super if !regex || $1.nil?
  @alpha3 = args[1]
  select_by($1, @alpha3)
end

Private Class Methods

add(country) click to toggle source
# File lib/country_with_currency/country.rb, line 67
def add(country)
  self.countries ||= []
  self.countries << country
end
load_file(file) click to toggle source
# File lib/country_with_currency/country.rb, line 61
def load_file(file)
  YAML.load_file(file).each do |code, options|
    add(new(code))
  end
end
new(data={}) click to toggle source
# File lib/country_with_currency/country.rb, line 15
def initialize(data={})
  @number = data["number"]
  @iso = data["iso3"]
  @currency = data["currency"]
  @name = data["name"]
  @symbol = data["symbol"]
end
parse_attributes(attr, val) click to toggle source
# File lib/country_with_currency/country.rb, line 80
def parse_attributes(attr, val)
  raise UnknownAttribute, "Invalid attribute '#{attr}'." unless [:number, :iso, :currency, :name, :symbol].include?(attr.to_sym)

  [attr, val]
end
select_by(attribute, val) click to toggle source
# File lib/country_with_currency/country.rb, line 72
def select_by(attribute, val)
  attr, value = parse_attributes(attribute.downcase, (val ? val.to_s.downcase : val))

  data = countries.select { |c| c.send(attr.to_sym).downcase == value }
  validate(data)
  data
end
validate(country) click to toggle source

Raise errors for invalid countries.

# File lib/country_with_currency/country.rb, line 87
def validate(country)
  raise UnknownCountry, "Country not found." if country.empty?
end