class Rwanda

Constants

DIVISIONS
RW
VERSION

Attributes

villages[RW]

Public Class Methods

new() click to toggle source
# File lib/rwanda.rb, line 28
def initialize
  @villages = []
  CSV.foreach(DATA, headers: :first_row) do |row|
    villages << Village.new(row)
  end
end

Public Instance Methods

cells_of(district, sector) click to toggle source
# 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) click to toggle source
# File lib/rwanda.rb, line 105
def district_like(district)
  @fmd ||= FuzzyMatch.new(districts)
  @fmd.find(district)
end
district_of(sector) click to toggle source
# 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
districts() click to toggle source
# File lib/rwanda.rb, line 93
def districts; @villages.collect{|v| v.district}.uniq.sort; end
districts_of(province) click to toggle source

)) Plural Ofs ((

# 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) click to toggle source
# 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) click to toggle source
# File lib/rwanda.rb, line 133
def exists?(*p); exist?(*p); end
first() click to toggle source

)) To help with testing ((

# File lib/rwanda.rb, line 150
def first
  @villages.first
end
province_like(province) click to toggle source

)) Matching ((

# File lib/rwanda.rb, line 101
def province_like(province)
  @fmp ||= FuzzyMatch.new(provinces)
  @fmp.find(province)
end
province_of(district, rw=false) click to toggle source

Singular Ofs ((

# 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
provinces() click to toggle source
)) Calleds ((

def districts_called(district)

@villages

end

)) Lists ((
# File lib/rwanda.rb, line 92
def provinces; @villages.collect{|v| v.province}.uniq.sort; end
sector_like(sector) click to toggle source
# 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
sectors() click to toggle source

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

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

)) Translation ((

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

)) Where is…? ((

# 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

Private Instance Methods

create_line(village, div) click to toggle source
# File lib/rwanda.rb, line 173
def create_line(village, div)
  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
  new_line
end
print_result(lines, division) click to toggle source