class Country
Attributes
@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.
@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.
@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.
@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.
@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 countries
Example
Returns an Array with value of all country objects
# File lib/country_with_currency/country.rb, line 34 def all countries end
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
# 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
# File lib/country_with_currency/country.rb, line 67 def add(country) self.countries ||= [] self.countries << country end
# 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
# 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
# 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
# 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
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