Class: CountriesOfTheWorld::Country

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

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, url) ⇒ Country

initialize with name & url, other info are added only when called



5
6
7
8
9
10
# File 'lib/countries_of_the_world/country.rb', line 5

def initialize(name, url) # initialize with name & url, other info are added only when called
  @name = name
  @url = url
  #self.add_info
  @@all << self
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



2
3
4
# File 'lib/countries_of_the_world/country.rb', line 2

def description
  @description
end

#gdpObject

Returns the value of attribute gdp.



2
3
4
# File 'lib/countries_of_the_world/country.rb', line 2

def gdp
  @gdp
end

#gdp_growthObject

Returns the value of attribute gdp_growth.



2
3
4
# File 'lib/countries_of_the_world/country.rb', line 2

def gdp_growth
  @gdp_growth
end

#income_levelObject

Returns the value of attribute income_level.



2
3
4
# File 'lib/countries_of_the_world/country.rb', line 2

def income_level
  @income_level
end

#inflationObject

Returns the value of attribute inflation.



2
3
4
# File 'lib/countries_of_the_world/country.rb', line 2

def inflation
  @inflation
end

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/countries_of_the_world/country.rb', line 2

def name
  @name
end

#populationObject

Returns the value of attribute population.



2
3
4
# File 'lib/countries_of_the_world/country.rb', line 2

def population
  @population
end

#regionObject

Returns the value of attribute region.



2
3
4
# File 'lib/countries_of_the_world/country.rb', line 2

def region
  @region
end

#urlObject

Returns the value of attribute url.



2
3
4
# File 'lib/countries_of_the_world/country.rb', line 2

def url
  @url
end

Class Method Details

.allObject



12
13
14
# File 'lib/countries_of_the_world/country.rb', line 12

def self.all
  @@all
end

.list_countries(n) ⇒ Object

print n-items per row



20
21
22
23
24
25
26
27
28
# File 'lib/countries_of_the_world/country.rb', line 20

def self.list_countries (n) #print n-items per row
  max_num = @@all.max_by{ |c| c.name.length}.name.length
  #find the country name with longest name
  @@all.each.with_index(1) do |c, i|
    print "#{i.to_s.ljust(5)}".bold.light_blue.on_cyan
    print "#{c.name.ljust(max_num)}".on_cyan
    print "\n" if i%n == 0 || i == @@all.size #print \n every nth row
  end
end

Instance Method Details

#add_info(hash_info) ⇒ Object



16
17
18
# File 'lib/countries_of_the_world/country.rb', line 16

def add_info (hash_info)
  hash_info.each{|k, v| self.send(("#{k}="), v)}
end

#list_detailObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/countries_of_the_world/country.rb', line 30

def list_detail
  puts "\n"
  puts "Name:         ".bold.magenta+"#{@name}".bold
  puts "********************************************************".colorize(:red)
  puts "Population:   ".bold.light_blue+"#{@population}"
  puts "GDP:          ".bold.light_blue+"#{@gdp}"
  puts "GDP Growth:   ".bold.light_blue+"#{@gdp_growth}"
  puts "Inflation:    ".bold.light_blue+"#{@inflation}"
  puts "Region:       ".bold.light_blue+"#{@region}"
  puts "Income_level: ".bold.light_blue+"#{@income_level}"
  puts "Overview:     ".bold.light_blue+"#{@description}"
  puts "URL:          ".bold.light_blue+"#{@url}".bold.light_blue
  puts "********************************************************".colorize(:red)
  puts ""
end