class Location
Public Class Methods
index_of(division)
click to toggle source
Class methods
# File lib/rwanda/location.rb, line 111 def self.index_of(division) members.index(division) + 1 end
Public Instance Methods
blank?()
click to toggle source
# File lib/rwanda/location.rb, line 63 def blank?; !present?; end
bottom(n, keep_nils=false)
click to toggle source
from village up, starting with the village will be n-element if keep_nils is true
# File lib/rwanda/location.rb, line 35 def bottom(n, keep_nils=false) d = divisions.reverse[0...n] keep_nils ? d : d.compact end
division_names(strings=false)
click to toggle source
# File lib/rwanda/location.rb, line 4 def division_names(strings=false); strings ? self.members.map(&:humanize) : self.members; end
divisions(strip_nil=false)
click to toggle source
# File lib/rwanda/location.rb, line 5 def divisions(strip_nil=false); strip_nil ? self.to_a.grep(String) : self.to_a; end
downto(level, keep_nils=false)
click to toggle source
from district down to and including level
# File lib/rwanda/location.rb, line 29 def downto(level, keep_nils=false) d = top(division_names.index(level.to_sym)+1) keep_nils ? d : d.compact end
first_missing()
click to toggle source
# File lib/rwanda/location.rb, line 45 def first_missing raise "Called first_missing on invalid Location [#{self.to_s}]" unless valid? each_pair do |level, div| return level if div == nil end false end
n_divisions()
click to toggle source
# File lib/rwanda/location.rb, line 6 def n_divisions; divisions.count; end
present?()
click to toggle source
# File lib/rwanda/location.rb, line 64 def present? if district.respond_to? :present? # defined in rails, which may be available district.present? else !district.nil? && !district.empty? end end
to_s()
click to toggle source
present? and blank? are rails-only undefined what to do with a blank string – it should never happen
# File lib/rwanda/location.rb, line 56 def to_s if present? to_h.inject([]) { |r, (k,v)| r << "#{v} #{k.to_s.capitalize}" if v; r }.join(', ') else 'Unknown' end end
top(n, keep_nils=false)
click to toggle source
from district down, always returning an n-element array if keep_nils is set
# File lib/rwanda/location.rb, line 25 def top(n, keep_nils=false) keep_nils ? divisions[0...n] : divisions[0...n].compact end
upto(level, keep_nils=false)
click to toggle source
from village up to and including level
# File lib/rwanda/location.rb, line 40 def upto(level, keep_nils=false) divisions = bottom(n_divisions - division_names.index(level.to_sym)) keep_nils ? divisions : divisions.compact end
valid?()
click to toggle source
# File lib/rwanda/location.rb, line 8 def valid? # is it valid? Rwanda.instance.valid? *divisions end
validate!()
click to toggle source
# File lib/rwanda/location.rb, line 11 def validate! # get rid of invalid data # NOT WORKING BECAUSE CP! DOESN'T WORK #binding.pry each_with_index do |div, n| #binding.pry unless Rwanda.instance.exists? *top(n+1, true) duplicate!(Location.new(*top(n))) return self end end self end
Private Instance Methods
duplicate!(other)
click to toggle source
Useful but broken
def cp!(other) # copies other's attributes to self
self.division_names do |level| self[:level] = other[:level] end self
end
# File lib/rwanda/location.rb, line 81 def duplicate!(other) # to_h first implemented in 2.0, but also seemingly provided by rails other.to_h.each_pair do |k,v| self[k] = v end self end