Class: Brujula::MapObject

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/brujula/map_object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_classObject (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

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/brujula/map_object.rb', line 5

def name
  @name
end

#parentObject (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

#eachObject



38
39
40
# File 'lib/brujula/map_object.rb', line 38

def each
  @collection.values.each { |item| yield item }
end

#expandObject



42
43
44
# File 'lib/brujula/map_object.rb', line 42

def expand
  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

Returns:

  • (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