Class: Location
- Inherits:
-
Struct
- Object
- Struct
- Location
- Defined in:
- lib/rwanda/location.rb
Instance Attribute Summary collapse
-
#cell ⇒ Object
Returns the value of attribute cell.
-
#district ⇒ Object
Returns the value of attribute district.
-
#sector ⇒ Object
Returns the value of attribute sector.
-
#village ⇒ Object
Returns the value of attribute village.
Class Method Summary collapse
-
.index_of(division) ⇒ Object
Class methods.
Instance Method Summary collapse
- #blank? ⇒ Boolean
-
#bottom(n, keep_nils = false) ⇒ Object
from village up, starting with the village will be n-element if keep_nils is true.
-
#division_names(strings = false) ⇒ Object
Rw = Rwanda.instance.
- #divisions(strip_nil = false) ⇒ Object
-
#downto(level, keep_nils = false) ⇒ Object
from district down to and including level.
- #first_missing ⇒ Object
- #n_divisions ⇒ Object
- #present? ⇒ Boolean
-
#to_s ⇒ Object
present? and blank? are rails-only undefined what to do with a blank string – it should never happen.
-
#top(n, keep_nils = false) ⇒ Object
from district down, always returning an n-element array if keep_nils is set.
-
#upto(level, keep_nils = false) ⇒ Object
from village up to and including level.
-
#valid? ⇒ Boolean
is it valid?.
-
#validate! ⇒ Object
get rid of invalid data.
Instance Attribute Details
#cell ⇒ Object
Returns the value of attribute cell
1 2 3 |
# File 'lib/rwanda/location.rb', line 1 def cell @cell end |
#district ⇒ Object
Returns the value of attribute district
1 2 3 |
# File 'lib/rwanda/location.rb', line 1 def district @district end |
#sector ⇒ Object
Returns the value of attribute sector
1 2 3 |
# File 'lib/rwanda/location.rb', line 1 def sector @sector end |
#village ⇒ Object
Returns the value of attribute 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
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_missing ⇒ Object
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_divisions ⇒ Object
6 |
# File 'lib/rwanda/location.rb', line 6 def n_divisions; divisions.count; end |
#present? ⇒ 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_s ⇒ Object
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?
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 |