Class: Country::COUNTRY
- Inherits:
-
Object
- Object
- Country::COUNTRY
- Defined in:
- lib/Country/country.rb
Constant Summary collapse
- @@all =
[]
Instance Attribute Summary collapse
-
#capital ⇒ Object
Returns the value of attribute capital.
-
#currencies ⇒ Object
Returns the value of attribute currencies.
-
#flag ⇒ Object
Returns the value of attribute flag.
-
#languages ⇒ Object
Returns the value of attribute languages.
-
#name ⇒ Object
Returns the value of attribute name.
-
#population ⇒ Object
Returns the value of attribute population.
-
#region ⇒ Object
Returns the value of attribute region.
-
#subregion ⇒ Object
Returns the value of attribute subregion.
Class Method Summary collapse
- .all ⇒ Object
- .all_country_names ⇒ Object
- .search_all_with_currency_name(currency_name) ⇒ Object
- .search_all_with_currency_symbol(currency_symbol) ⇒ Object
- .search_all_with_language(language) ⇒ Object
- .search_all_with_population(comparison, population) ⇒ Object
- .search_by_capital(capital) ⇒ Object
- .search_by_name(name) ⇒ Object
- .search_by_region(region) ⇒ Object
- .search_by_subregion(subregion) ⇒ Object
- .search_by_suffix(suffix) ⇒ Object
Instance Method Summary collapse
- #info ⇒ Object
-
#initialize(attr_hash) ⇒ COUNTRY
constructor
A new instance of COUNTRY.
Constructor Details
#initialize(attr_hash) ⇒ COUNTRY
Returns a new instance of COUNTRY.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/Country/country.rb', line 7 def initialize(attr_hash) attr_hash.each do |attribute, value| if self.respond_to?(attribute) self.send(("#{attribute}="), value) end end if self.capital == "" self.capital = "n/a" end if self.region == "" self.region = "n/a" end if self.subregion == "" self.subregion = "n/a" end @@all << self end |
Instance Attribute Details
#capital ⇒ Object
Returns the value of attribute capital.
3 4 5 |
# File 'lib/Country/country.rb', line 3 def capital @capital end |
#currencies ⇒ Object
Returns the value of attribute currencies.
3 4 5 |
# File 'lib/Country/country.rb', line 3 def currencies @currencies end |
#flag ⇒ Object
Returns the value of attribute flag.
3 4 5 |
# File 'lib/Country/country.rb', line 3 def flag @flag end |
#languages ⇒ Object
Returns the value of attribute languages.
3 4 5 |
# File 'lib/Country/country.rb', line 3 def languages @languages end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/Country/country.rb', line 3 def name @name end |
#population ⇒ Object
Returns the value of attribute population.
3 4 5 |
# File 'lib/Country/country.rb', line 3 def population @population end |
#region ⇒ Object
Returns the value of attribute region.
3 4 5 |
# File 'lib/Country/country.rb', line 3 def region @region end |
#subregion ⇒ Object
Returns the value of attribute subregion.
3 4 5 |
# File 'lib/Country/country.rb', line 3 def subregion @subregion end |
Class Method Details
.all ⇒ Object
25 26 27 |
# File 'lib/Country/country.rb', line 25 def self.all @@all end |
.all_country_names ⇒ Object
29 30 31 32 33 |
# File 'lib/Country/country.rb', line 29 def self.all_country_names self.all.each_with_index do |country, i| puts "#{i+1}. #{country.name}" end end |
.search_all_with_currency_name(currency_name) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/Country/country.rb', line 66 def self.search_all_with_currency_name(currency_name) self.all.select do |country| bool = false country.currencies.each do |currency_info| if currency_info["name"] && currency_info["name"].downcase == currency_name.downcase bool = true end end bool end end |
.search_all_with_currency_symbol(currency_symbol) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/Country/country.rb', line 54 def self.search_all_with_currency_symbol(currency_symbol) self.all.select do |country| bool = false country.currencies.each do |currency_info| if currency_info["symbol"] == currency_symbol bool = true end end bool end end |
.search_all_with_language(language) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/Country/country.rb', line 78 def self.search_all_with_language(language) self.all.select do |country| bool = false country.languages.each do |language_info| if language_info["name"] && (language_info["name"].downcase == language.downcase || language_info["nativeName"].downcase == language.downcase) bool = true end end bool end end |
.search_all_with_population(comparison, population) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/Country/country.rb', line 90 def self.search_all_with_population(comparison, population) if comparison == "l" self.all.select{|country| country.population < population}.sort_by{|country| country.population} elsif comparison == "g" self.all.select{|country| country.population > population}.sort_by{|country| country.population} end end |
.search_by_capital(capital) ⇒ Object
98 99 100 |
# File 'lib/Country/country.rb', line 98 def self.search_by_capital(capital) self.all.detect{|country| country.capital.downcase == capital.downcase} end |
.search_by_name(name) ⇒ Object
35 36 37 |
# File 'lib/Country/country.rb', line 35 def self.search_by_name(name) self.all.detect{|country| country.name.downcase == name.downcase} end |
.search_by_region(region) ⇒ Object
102 103 104 |
# File 'lib/Country/country.rb', line 102 def self.search_by_region(region) self.all.select{|country| country.region.downcase == region.downcase} end |
.search_by_subregion(subregion) ⇒ Object
106 107 108 |
# File 'lib/Country/country.rb', line 106 def self.search_by_subregion(subregion) self.all.select{|country| country.subregion.downcase == subregion.downcase} end |
.search_by_suffix(suffix) ⇒ Object
50 51 52 |
# File 'lib/Country/country.rb', line 50 def self.search_by_suffix(suffix) self.all.detect{|country| country.name.downcase.match(/^(#{suffix.downcase})/)} end |
Instance Method Details
#info ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/Country/country.rb', line 39 def info puts "Name: #{self.name}" puts "Capital: #{self.capital}" puts "Currencies: #{self.currencies}" puts "Languages: #{self.languages}" puts "Population: #{self.population}" puts "Flag: #{self.flag}" puts "Region: #{self.region}" puts "Subregion: #{self.subregion}" end |