Class: Nayyar::Township

Inherits:
Object
  • Object
show all
Defined in:
lib/nayyar/township.rb

Overview

Represents a Township in Myanmar

Constant Summary collapse

ATTRIBUTES =
%i[
  pcode
  name
  my_name
].freeze
INDICES =
%w[pcode].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Township

Returns a new instance of Township.



18
19
20
# File 'lib/nayyar/township.rb', line 18

def initialize(data)
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/nayyar/township.rb', line 5

def data
  @data
end

Class Method Details

.allObject



43
44
45
# File 'lib/nayyar/township.rb', line 43

def all
  townships
end

.find_by(query) ⇒ Object



55
56
57
58
# File 'lib/nayyar/township.rb', line 55

def find_by(query)
  key = get_key(query)
  (index = send("#{key}_index".to_sym).index(query[key])) && townships[index]
end

.find_by!(query) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/nayyar/township.rb', line 60

def find_by!(query)
  if (district = find_by(query))
    district
  else
    key = get_key(query)
    raise Nayyar::TownshipNotFound, "Cannot find State with given #{key}: #{query[key]}"
  end
end

.of_district(district) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/nayyar/township.rb', line 47

def of_district(district)
  state_pcode = district.pcode
  townships = self.townships
  @district_index[state_pcode].map do |index|
    townships[index]
  end
end

Instance Method Details

#[](key) ⇒ Object

allow the values to be retrieved as an array



30
31
32
33
34
35
36
# File 'lib/nayyar/township.rb', line 30

def [](key)
  if ATTRIBUTES.include? key
    @data[key]
  elsif key.to_sym == :district
    district
  end
end

#districtObject



38
39
40
# File 'lib/nayyar/township.rb', line 38

def district
  Nayyar::District.find_by_pcode(@data[:district])
end