Class: Rwanda
- Inherits:
-
Object
- Object
- Rwanda
- Includes:
- Singleton
- Defined in:
- lib/rwanda.rb,
lib/rwanda/version.rb
Constant Summary collapse
- DIVISIONS =
[:province,:district,:sector,:cell,:village]
- RW =
{ 'Northern Province' => 'Amajyaruguru', 'Southern Province' => 'Amajyepfo', 'Eastern Province' => 'Iburasirazuba', 'Western Province' => 'Iburengerazuba', 'Kigali City' => 'Umujyi wa Kigali' }
- VERSION =
"0.7.1"
Instance Attribute Summary collapse
-
#villages ⇒ Object
Returns the value of attribute villages.
Instance Method Summary collapse
- #cells_of(district, sector) ⇒ Object
- #district_like(district) ⇒ Object
- #district_of(sector) ⇒ Object
- #districts ⇒ Object
-
#districts_of(province) ⇒ Object
)) Plural Ofs ((.
- #exist?(district = false, sector = false, cell = false, village = false) ⇒ Boolean
- #exists?(*p) ⇒ Boolean
-
#initialize ⇒ Rwanda
constructor
A new instance of Rwanda.
-
#province_like(province) ⇒ Object
)) Matching ((.
-
#province_of(district, rw = false) ⇒ Object
Singular Ofs ((.
-
#provinces ⇒ Object
)) Calleds (( def districts_called(district) @villages end )) Lists ((.
- #sector_like(sector) ⇒ Object
-
#sectors ⇒ Object
already introduces ambiguity from sectors down: 37 districts are duplicate names.
- #sectors_of(district) ⇒ Object
- #subdivisions_of(arr) ⇒ Object
-
#translate(province) ⇒ Object
)) Translation ((.
- #valid?(*p) ⇒ Boolean
- #villages_of(district, sector, cell) ⇒ Object
-
#where_is?(division) ⇒ Boolean
)) Where is…? ((.
Constructor Details
Instance Attribute Details
#villages ⇒ Object
Returns the value of attribute villages.
18 19 20 |
# File 'lib/rwanda.rb', line 18 def villages @villages end |
Instance Method Details
#cells_of(district, sector) ⇒ Object
58 59 60 |
# File 'lib/rwanda.rb', line 58 def cells_of(district, sector) @villages.select {|v| v.district.downcase == district.downcase and v.sector.downcase == sector.downcase}.collect {|v| v.cell}.uniq end |
#district_like(district) ⇒ Object
96 97 98 99 |
# File 'lib/rwanda.rb', line 96 def district_like(district) @fmd ||= FuzzyMatch.new(districts) @fmd.find(district) end |
#district_of(sector) ⇒ Object
44 45 46 47 |
# File 'lib/rwanda.rb', line 44 def district_of(sector) village = @villages.select_first {|v| v.sector.downcase == sector.downcase} village ? village.district : nil end |
#districts ⇒ Object
84 |
# File 'lib/rwanda.rb', line 84 def districts; @villages.collect{|v| v.district}.uniq; end |
#districts_of(province) ⇒ Object
)) Plural Ofs ((
49 50 51 52 53 |
# File 'lib/rwanda.rb', line 49 def districts_of(province) districts = @villages.select {|v| v.province.downcase == province.downcase }.collect {|v| v.district}.uniq #binding.pry districts.empty? ? nil : districts end |
#exist?(district = false, sector = false, cell = false, village = false) ⇒ Boolean
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/rwanda.rb', line 114 def exist?(district=false, sector=false, cell=false, village=false) villages = @villages.dup return false unless district {district: district, sector: sector, cell: cell, village: village}.each_pair do |division_name,division| #binding.pry return true unless division villages.select! {|v| v.send(division_name).downcase == division.downcase} return false if villages.empty? end true end |
#exists?(*p) ⇒ Boolean
125 |
# File 'lib/rwanda.rb', line 125 def exists?(*p); exist?(*p); end |
#province_like(province) ⇒ Object
)) Matching ((
92 93 94 95 |
# File 'lib/rwanda.rb', line 92 def province_like(province) @fmp ||= FuzzyMatch.new(provinces) @fmp.find(province) end |
#province_of(district, rw = false) ⇒ Object
Singular Ofs ((
36 37 38 39 40 41 42 43 |
# File 'lib/rwanda.rb', line 36 def province_of(district, rw=false) village = @villages.select_first {|v| v.district.downcase == district.downcase} if village if rw then RW[village.province] else village.province end else nil end end |
#provinces ⇒ Object
)) Calleds (( def districts_called(district)
@villages
end )) Lists ((
83 |
# File 'lib/rwanda.rb', line 83 def provinces; @villages.collect{|v| v.province}.uniq; end |
#sector_like(sector) ⇒ Object
100 101 102 103 104 |
# File 'lib/rwanda.rb', line 100 def sector_like(sector) # Already problematic here: there are identical sector names @fms ||= FuzzyMatch.new(sectors) @fms.find(sector) end |
#sectors ⇒ Object
already introduces ambiguity from sectors down: 37 districts are duplicate names
86 87 88 89 |
# File 'lib/rwanda.rb', line 86 def sectors @sectors ||= @villages.collect {|v| [v.district, v.sector] }.uniq.collect {|ds| ds[1]}.sort #@villages.collect{|v| v.sector}.uniq end |
#sectors_of(district) ⇒ Object
54 55 56 57 |
# File 'lib/rwanda.rb', line 54 def sectors_of(district) sectors = @villages.select {|v| v.district.downcase == district.downcase }.collect {|v| v.sector}.uniq sectors.empty? ? nil : sectors end |
#subdivisions_of(arr) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rwanda.rb', line 64 def subdivisions_of(arr) case arr.length when 0 districts when 1 sectors_of *arr when 2 cells_of *arr when 3 villages_of *arr else raise "subdivisions_of requires an array of between 0 and 3 elements (do NOT include a province): received #{arr}" end end |
#translate(province) ⇒ Object
)) Translation ((
133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/rwanda.rb', line 133 def translate(province) kin = RW.find { |eng,kin| eng.downcase == province.downcase } # returns [key, val] return kin[1] if kin eng = RW.find { |eng,kin| kin.downcase == province.downcase } return eng[0] if eng nil #if RW.has_key? province # RW[province] #elsif RW.has_value? province # RW.key province #else # nil #end end |
#valid?(*p) ⇒ Boolean
126 127 128 129 130 |
# File 'lib/rwanda.rb', line 126 def valid?(*p) # Is this location either (a) real, or (b) nil (both of which are valid if an object's address can be null) return true if p.reject{|i| i.nil?}.empty? # all nils is fine exist? *p end |
#villages_of(district, sector, cell) ⇒ Object
61 62 63 |
# File 'lib/rwanda.rb', line 61 def villages_of(district, sector, cell) @villages.select {|v| v.district.downcase == district.downcase and v.sector.downcase == sector.downcase and v.cell.downcase == cell.downcase}.collect {|v| v.village} end |
#where_is?(division) ⇒ Boolean
)) Where is…? ((
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/rwanda.rb', line 149 def where_is?(division) matching = { province: [], district: [], sector: [], cell: [], village: [] } lines = [] @villages.each do |village| matches = village.match(division) unless matches.empty? matches.each do |div| matching[div].push village # convert villages into lines new_line = " #{village.send(div)} is a #{div}#{' in' unless div==:province}" unless div == :province (0...Rwanda::DIVISIONS.index(div)).to_a.reverse.each do |n| new_line << " #{village[n]}#{' '+Rwanda::DIVISIONS[n].to_s.capitalize+',' unless n==0}" end end lines << new_line end end end lines.uniq! output = '' # summary line if lines.empty? output += "Rwanda has no divisions called #{division.capitalize}\n" else output += 'Rwanda has' n = [] comma = '' (0..3).each do |i| n = lines.count { |l| l.count(',') == i } output << "#{comma} #{i == 3 ? 'and ' : ''}#{n} #{Rwanda::DIVISIONS[i+1]}#{n == 1 ? '' : 's'}" comma = ',' end output << " called #{division.capitalize}:\n" end # detail lines.each { |line| output << line << "\n" } puts output end |