Class: Nayyar::District

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

Overview

Represents a District 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) ⇒ District

Returns a new instance of District.



18
19
20
# File 'lib/nayyar/district.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/district.rb', line 5

def data
  @data
end

Class Method Details

.allObject



47
48
49
# File 'lib/nayyar/district.rb', line 47

def all
  districts
end

.find_by(query) ⇒ Object



59
60
61
62
# File 'lib/nayyar/district.rb', line 59

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

.find_by!(query) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/nayyar/district.rb', line 64

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

.of_state(state) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/nayyar/district.rb', line 51

def of_state(state)
  state_pcode = state.pcode
  districts = self.districts
  @state_index[state_pcode].map do |index|
    districts[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/district.rb', line 30

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

#stateObject



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

def state
  Nayyar::State.find_by_pcode(@data[:state])
end

#townshipsObject



42
43
44
# File 'lib/nayyar/district.rb', line 42

def townships
  Nayyar::Township.of_district(self)
end