Class: Brujula::MapObject
- Inherits:
-
Object
- Object
- Brujula::MapObject
- Includes:
- Enumerable
- Defined in:
- lib/brujula/map_object.rb
Instance Attribute Summary collapse
-
#child_class ⇒ Object
readonly
Returns the value of attribute child_class.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #each ⇒ Object
- #expand ⇒ Object
- #fetch(key) ⇒ Object
-
#initialize(data:, parent: nil, name:, child_class:) ⇒ MapObject
constructor
A new instance of MapObject.
- #key?(key) ⇒ Boolean
- #merge(key, value) ⇒ Object
Constructor Details
#initialize(data:, parent: nil, name:, child_class:) ⇒ MapObject
Returns a new instance of MapObject.
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/brujula/map_object.rb', line 7 def initialize(data:, parent: nil, name:, child_class:) @parent = parent @name = name @child_class = child_class @collection = {} each_flatted_child(data) do |child_name, child_data| child_object = build_object(child_name, child_data) @collection.merge!(child_name.to_s => child_object) end end |
Instance Attribute Details
#child_class ⇒ Object (readonly)
Returns the value of attribute child_class.
5 6 7 |
# File 'lib/brujula/map_object.rb', line 5 def child_class @child_class end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/brujula/map_object.rb', line 5 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
5 6 7 |
# File 'lib/brujula/map_object.rb', line 5 def parent @parent end |
Instance Method Details
#[](key) ⇒ Object
23 24 25 |
# File 'lib/brujula/map_object.rb', line 23 def [](key) @collection.fetch(key.to_s, nil) end |
#each ⇒ Object
38 39 40 |
# File 'lib/brujula/map_object.rb', line 38 def each @collection.values.each { |item| yield item } end |
#expand ⇒ Object
42 43 44 |
# File 'lib/brujula/map_object.rb', line 42 def self end |
#fetch(key) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/brujula/map_object.rb', line 27 def fetch(key) @collection.fetch(key.to_s) rescue KeyError => error $stderr.puts "Couldn't find item #{ key } in collection #{ name }" raise error end |
#key?(key) ⇒ Boolean
19 20 21 |
# File 'lib/brujula/map_object.rb', line 19 def key?(key) @collection.key?(key.to_s) end |
#merge(key, value) ⇒ Object
34 35 36 |
# File 'lib/brujula/map_object.rb', line 34 def merge(key, value) @collection.merge!(key.to_s => value) end |