Class: Location

Inherits:
Struct
  • Object
show all
Defined in:
lib/rwanda/location.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cellObject

Returns the value of attribute cell

Returns:

  • (Object)

    the current value of cell



1
2
3
# File 'lib/rwanda/location.rb', line 1

def cell
  @cell
end

#districtObject

Returns the value of attribute district

Returns:

  • (Object)

    the current value of district



1
2
3
# File 'lib/rwanda/location.rb', line 1

def district
  @district
end

#sectorObject

Returns the value of attribute sector

Returns:

  • (Object)

    the current value of sector



1
2
3
# File 'lib/rwanda/location.rb', line 1

def sector
  @sector
end

#villageObject

Returns the value of attribute village

Returns:

  • (Object)

    the current value of village



1
2
3
# File 'lib/rwanda/location.rb', line 1

def village
  @village
end

Class Method Details

.index_of(division) ⇒ Object

Class methods



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

def self.index_of(division)
  members.index(division) + 1
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


63
# File 'lib/rwanda/location.rb', line 63

def blank?; !present?; end

#bottom(n, keep_nils = false) ⇒ Object

from village up, starting with the village will be n-element if keep_nils is true



35
36
37
38
# 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) ⇒ Object

Rw = Rwanda.instance



4
# File 'lib/rwanda/location.rb', line 4

def division_names(strings=false); strings ? self.members.map(&:humanize) : self.members; end

#divisions(strip_nil = false) ⇒ Object



5
# 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) ⇒ Object

from district down to and including level



29
30
31
32
# 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_missingObject



45
46
47
48
49
50
51
# 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_divisionsObject



6
# File 'lib/rwanda/location.rb', line 6

def n_divisions; divisions.count; end

#present?Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
# 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_sObject

present? and blank? are rails-only undefined what to do with a blank string – it should never happen



56
57
58
59
60
61
62
# 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) ⇒ Object

from district down, always returning an n-element array if keep_nils is set



25
26
27
# 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) ⇒ Object

from village up to and including level



40
41
42
43
# 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?Boolean

is it valid?

Returns:

  • (Boolean)


8
9
10
# File 'lib/rwanda/location.rb', line 8

def valid? # is it valid?
  Rwanda.instance.valid? *divisions
end

#validate!Object

get rid of invalid data



11
12
13
14
15
16
17
18
19
20
21
22
# 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