secret
anonymous / tamasszabo.rb
Created

  • Download Gist
tamasszabo.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
require 'rexml/document'
require 'test/unit'
 
DESCENDING = -1
 
class Statistics
def initialize(xml_file)
@countries = Countries.new('cia-1996.xml')
end
 
def most_populated
@countries.max_by(&:population)
end
 
def highest_5_inflation
@countries.sort_by {|c| DESCENDING * c.inflation}.first(5)
end
 
def group_by_continent
grouping = @countries.group_by(&:continent)
sorted_grouping = {}
grouping.keys.sort.each do |continent|
sorted_grouping[continent] = grouping[continent].sort_by(&:name)
end
return sorted_grouping
end
end
 
class Countries
include Enumerable
 
def initialize(xml_file)
doc = REXML::Document.new(File.new(xml_file))
@countries = doc.elements.each('/cia/country') {|c| c}.map {|c| Country.new(c)}
end
 
def each
@countries.each { |c| yield c }
end
end
 
class Country
ConversionMethod = Hash.new(:to_s).update(
{
:population => :to_i,
:inflation => :to_f,
})
 
def initialize(xml_element)
@xml_element = xml_element
end
 
def method_missing(id, *args)
attr = @xml_element.attributes[id.to_s]
attr.send(self.class::ConversionMethod[id])
end
 
def to_s
name
end
 
end
 
class StatsTests < Test::Unit::TestCase
 
def setup
@stats = Statistics.new('cia-1996.xml')
end
 
def test_most_populated
most_populated = @stats.most_populated
assert_equal('China', most_populated.name)
assert_equal(1210004956, most_populated.population)
end
 
def test_inflation_list
highest_5_inflation = @stats.highest_5_inflation
assert_equal(5, highest_5_inflation.size)
assert_equal('Belarus', highest_5_inflation[0].name)
assert_equal(244, highest_5_inflation[0].inflation)
assert_equal('Yemen', highest_5_inflation[4].name)
end
 
def test_continent_list
continents = @stats.group_by_continent
assert_equal(6, continents.size)
assert_equal(%w{Africa Asia Australia/Oceania Europe North\ America South\ America}, continents.keys)
assert_equal('Austria', continents['Europe'][2].name)
end
end
 
# Display - BONUS :)
 
def display
stats = Statistics.new('cia-1996.xml')
 
most_populated = stats.most_populated
highest_5_inflation = stats.highest_5_inflation
continents = stats.group_by_continent
 
def title(t)
puts
puts "=== #{t} ==="
puts
end
 
def subtitle(t)
puts
puts ' ' + t
puts '-' * (t.size+1)
end
 
 
title("Highest population")
puts "#{most_populated.name}: #{most_populated.population}"
 
title("Highest 5 inflation")
highest_5_inflation.each_with_index do |country, i|
longest_name = highest_5_inflation.max_by {|c| c.name.size}.name
longest_inflation = highest_5_inflation.max_by {|c| c.inflation.to_s.size}.inflation
cname = country.name.ljust(longest_name.size+1)
infl = country.inflation.to_s.rjust(longest_inflation.to_s.size+1)
puts "|#{i+1}. |#{cname}|#{infl}|"
end
 
title("All Continents")
puts "Continents: " + continents.keys.join(', ')
 
title("Countries by Continents")
continents.keys.each do |continent|
subtitle(continent)
puts "\t" + continents[continent].join(", ")
end
end
 
#display

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.