Class: Nayyar::District
- Inherits:
-
Object
- Object
- Nayyar::District
- 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
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](key) ⇒ Object
allow the values to be retrieved as an array.
-
#initialize(data) ⇒ District
constructor
A new instance of District.
- #state ⇒ Object
- #townships ⇒ Object
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
#data ⇒ Object (readonly)
Returns the value of attribute data.
5 6 7 |
# File 'lib/nayyar/district.rb', line 5 def data @data end |
Class Method Details
.all ⇒ Object
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 |
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 |
#state ⇒ Object
38 39 40 |
# File 'lib/nayyar/district.rb', line 38 def state Nayyar::State.find_by_pcode(@data[:state]) end |
#townships ⇒ Object
42 43 44 |
# File 'lib/nayyar/district.rb', line 42 def townships Nayyar::Township.of_district(self) end |