Class: StackableArray

Inherits:
Array
  • Object
show all
Defined in:
lib/ext/stackable_array.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_keyObject

Returns the value of attribute current_key.



2
3
4
# File 'lib/ext/stackable_array.rb', line 2

def current_key
  @current_key
end

#parentObject

Returns the value of attribute parent.



2
3
4
# File 'lib/ext/stackable_array.rb', line 2

def parent
  @parent
end

Instance Method Details

#child(key) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/ext/stackable_array.rb', line 4

def child(key)
  hash = {key => StackableHash.new}.stackable
  new_target = self.current
  new_target.merge!(hash)
  new_target.current_key = key
  new_target.parent = self
  new_target
end

#currentObject



25
26
27
# File 'lib/ext/stackable_array.rb', line 25

def current
  self.last[current_key]
end

#current=(value) ⇒ Object



21
22
23
# File 'lib/ext/stackable_array.rb', line 21

def current=(value)
  self.last[current_key] = value
end

#merge!(hash) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/ext/stackable_array.rb', line 13

def merge!(hash)
  if self.last.is_a?(Hash) && !self.last.key?(hash.keys.first)
    self.last.merge!(hash)
  else
    self << hash
  end
end