Class: Country
- Inherits:
-
Object
- Object
- Country
- 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
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
- .[](country_code) ⇒ Object
-
.all ⇒ Object
Returns big array with all countries.
- .country_codes ⇒ Object
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #eu_member? ⇒ Boolean
- #in_europe? ⇒ Boolean
-
#initialize(country_code) ⇒ Country
constructor
A new instance of Country.
- #to_s ⇒ Object
- #valid? ⇒ Boolean
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
#data ⇒ Object (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 |
.all ⇒ Object
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_codes ⇒ Object
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
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
45 46 47 |
# File 'lib/country_list/country.rb', line 45 def in_europe? @data && @data['continent'].nil? ? false : @data['continent'] == 'Europe' end |
#to_s ⇒ Object
37 38 39 |
# File 'lib/country_list/country.rb', line 37 def to_s name end |
#valid? ⇒ Boolean
33 34 35 |
# File 'lib/country_list/country.rb', line 33 def valid? !(@data.nil? || @data.empty?) end |