Class: Country

Inherits:
Object
  • Object
show all
Defined in:
lib/country_list/country.rb

Constant Summary collapse

ATTR_READERS =
[
  :alpha2,
  :name,
  :eu_member,
  :currency_code
]
@@country_codes =
nil
@@cached_countries =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(country_code) ⇒ Country

Returns a new instance of Country.



22
23
24
# File 'lib/country_list/country.rb', line 22

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

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/country_list/country.rb', line 7

def data
  @data
end

Class Method Details

.[](country_code) ⇒ Object



57
58
59
60
# File 'lib/country_list/country.rb', line 57

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

.allObject

Returns big array with all countries



53
54
55
# File 'lib/country_list/country.rb', line 53

def all
  CountryList::Data.countries.map{ |country_code, data| Country.new(country_code) }
end

.country_codesObject



62
63
64
# File 'lib/country_list/country.rb', line 62

def country_codes
  CountryList::Data.country_codes
end

Instance Method Details

#<=>(other) ⇒ Object



26
27
28
# File 'lib/country_list/country.rb', line 26

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

#==(other) ⇒ Object



30
31
32
# File 'lib/country_list/country.rb', line 30

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

#eu_member?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/country_list/country.rb', line 42

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

#in_europe?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/country_list/country.rb', line 46

def in_europe?
  @data && @data['continent'].nil? ? false : @data['continent'] == 'Europe'
end

#to_sObject



38
39
40
# File 'lib/country_list/country.rb', line 38

def to_s
  name
end

#valid?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/country_list/country.rb', line 34

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