Class: CountriesCli::COUNTRY
- Inherits:
-
Object
- Object
- CountriesCli::COUNTRY
- Defined in:
- lib/countries_cli/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/countries_cli/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/countries_cli/country.rb', line 3 def capital @capital end |
#currencies ⇒ Object
Returns the value of attribute currencies.
3 4 5 |
# File 'lib/countries_cli/country.rb', line 3 def currencies @currencies end |
#flag ⇒ Object
Returns the value of attribute flag.
3 4 5 |
# File 'lib/countries_cli/country.rb', line 3 def flag @flag end |
#languages ⇒ Object
Returns the value of attribute languages.
3 4 5 |
# File 'lib/countries_cli/country.rb', line 3 def languages @languages end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/countries_cli/country.rb', line 3 def name @name end |
#population ⇒ Object
Returns the value of attribute population.
3 4 5 |
# File 'lib/countries_cli/country.rb', line 3 def population @population end |
#region ⇒ Object
Returns the value of attribute region.
3 4 5 |
# File 'lib/countries_cli/country.rb', line 3 def region @region end |
#subregion ⇒ Object
Returns the value of attribute subregion.
3 4 5 |
# File 'lib/countries_cli/country.rb', line 3 def subregion @subregion end |
Class Method Details
.all ⇒ Object
25 26 27 |
# File 'lib/countries_cli/country.rb', line 25 def self.all @@all end |
.all_country_names ⇒ Object
29 30 31 32 33 |
# File 'lib/countries_cli/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
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/countries_cli/country.rb', line 77 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
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/countries_cli/country.rb', line 65 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
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/countries_cli/country.rb', line 89 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
101 102 103 104 105 106 107 |
# File 'lib/countries_cli/country.rb', line 101 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
109 110 111 |
# File 'lib/countries_cli/country.rb', line 109 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/countries_cli/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
113 114 115 |
# File 'lib/countries_cli/country.rb', line 113 def self.search_by_region(region) self.all.select{|country| country.region.downcase == region.downcase} end |
.search_by_subregion(subregion) ⇒ Object
117 118 119 |
# File 'lib/countries_cli/country.rb', line 117 def self.search_by_subregion(subregion) self.all.select{|country| country.subregion.downcase == subregion.downcase} end |
.search_by_suffix(suffix) ⇒ Object
61 62 63 |
# File 'lib/countries_cli/country.rb', line 61 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 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/countries_cli/country.rb', line 39 def info puts "Name: #{self.name}" puts "Capital: #{self.capital}" print "Currencies: \n" self.currencies.each_with_index do |currency, index| print "\t#{index+1}. code: #{currency["code"]}" print ", name: #{currency["name"]}" print ", symbol: #{currency["symbol"]}\n" end print "Languages: \n" self.languages.each_with_index do |language, index| print "\t#{index+1}. iso639_1: #{language["iso639_1"]}" print ", iso639_2: #{language["iso639_2"]}" print ", name: #{language["name"]}" print ", native name: #{language["nativeName"]}\n" end puts "Population: #{self.population}" puts "Flag: #{self.flag}" puts "Region: #{self.region}" puts "Subregion: #{self.subregion}" end |