Class: Country

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

Constant Summary collapse

ATTR_READERS =
[
  :alpha2,
  :name,
  :eu_member
]
@@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.



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

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



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

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

.allObject

Returns big array with all countries



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

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

.country_codesObject



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

def country_codes
  CountryList::Data.country_codes
end

Instance Method Details

#<=>(other) ⇒ Object



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

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

#==(other) ⇒ Object



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

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

#eu_member?Boolean

Returns:

  • (Boolean)


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

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

#in_europe?Boolean

Returns:

  • (Boolean)


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

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

#to_sObject



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

def to_s
  name
end

#valid?Boolean

Returns:

  • (Boolean)


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

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