Class: Rwanda

Inherits:
Object
  • Object
show all
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.8.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRwanda

Returns a new instance of Rwanda.



28
29
30
31
32
33
# File 'lib/rwanda.rb', line 28

def initialize
  @villages = []
  CSV.foreach(DATA, headers: :first_row) do |row|
    villages << Village.new(row)
  end
end

Instance Attribute Details

#villagesObject

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



65
66
67
# File 'lib/rwanda.rb', line 65

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



103
104
105
106
# File 'lib/rwanda.rb', line 103

def district_like(district)
  @fmd ||= FuzzyMatch.new(districts)
  @fmd.find(district)
end

#district_of(sector) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rwanda.rb', line 44

def district_of(sector)
  villages = @villages.select {|v| v.sector.downcase == sector.downcase}.collect {|v| { sector: v.sector, district: v.district } }.uniq
  case villages.length
    when 0
      nil
    when 1
      villages[0][:district]
    else
      villages.collect {|v| v[:district] }.sort
  end
end

#districtsObject



91
# File 'lib/rwanda.rb', line 91

def districts; @villages.collect{|v| v.district}.uniq; end

#districts_of(province) ⇒ Object

)) Plural Ofs ((



56
57
58
59
60
# File 'lib/rwanda.rb', line 56

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

Returns:

  • (Boolean)


121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rwanda.rb', line 121

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

Returns:

  • (Boolean)


132
# File 'lib/rwanda.rb', line 132

def exists?(*p); exist?(*p); end

#province_like(province) ⇒ Object

)) Matching ((



99
100
101
102
# File 'lib/rwanda.rb', line 99

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

#provincesObject

)) Calleds (( def districts_called(district)

@villages

end )) Lists ((



90
# File 'lib/rwanda.rb', line 90

def provinces; @villages.collect{|v| v.province}.uniq; end

#sector_like(sector) ⇒ Object



107
108
109
110
111
# File 'lib/rwanda.rb', line 107

def sector_like(sector)
  # Already problematic here: there are identical sector names
  @fms ||= FuzzyMatch.new(sectors)
  @fms.find(sector)
end

#sectorsObject

already introduces ambiguity from sectors down: 37 districts are duplicate names



93
94
95
96
# File 'lib/rwanda.rb', line 93

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



61
62
63
64
# File 'lib/rwanda.rb', line 61

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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rwanda.rb', line 71

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 ((



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/rwanda.rb', line 140

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

Returns:

  • (Boolean)


133
134
135
136
137
# File 'lib/rwanda.rb', line 133

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



68
69
70
# File 'lib/rwanda.rb', line 68

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…? ((

Returns:

  • (Boolean)


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
190
191
192
193
194
195
196
# File 'lib/rwanda.rb', line 156

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