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.2"

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



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

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



105
106
107
108
# File 'lib/rwanda.rb', line 105

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

#district_of(sector) ⇒ Object



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

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



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

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

#districts_of(province) ⇒ Object

)) Plural Ofs ((



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

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)


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

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)


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

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

#firstObject

)) To help with testing ((



150
151
152
# File 'lib/rwanda.rb', line 150

def first
  @villages.first
end

#province_like(province) ⇒ Object

)) Matching ((



101
102
103
104
# File 'lib/rwanda.rb', line 101

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
44
# File 'lib/rwanda.rb', line 36

def province_of(district, rw=false)
  #village = @villages.select_first {|v| v.district.downcase == district.downcase}
  village = @villages.select {|v| v.district.downcase == district.downcase}.first
  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 ((



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

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

#sector_like(sector) ⇒ Object



109
110
111
112
113
# File 'lib/rwanda.rb', line 109

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



95
96
97
98
# File 'lib/rwanda.rb', line 95

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



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

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



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

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



141
142
143
144
145
146
147
# File 'lib/rwanda.rb', line 141

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
end

#valid?(*p) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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}
  #@villages.select {|v| v.to_h(:district, :sector, :cell).to_a.sort == { district: district, sector: sector, cell: cell }.to_a.collect{|v| [v[0],v[1].downcase] }.collect {|v| v.village}
end

#where_is?(division) ⇒ Boolean

)) Where is…? ((

Returns:

  • (Boolean)


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/rwanda.rb', line 155

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 each match into a line
        lines << create_line(village, div)
      end
    end
  end
  lines.uniq!
  print_result(lines, division)
end