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
12
13
# File 'lib/ext/stackable_array.rb', line 4

def child(key)
  hash = StackableHash.new
  hash[key] = StackableHash.new

  new_target = self.current
  new_target.merge!(hash)
  new_target.current_key = key
  new_target.parent = self
  new_target
end

#currentObject



31
32
33
# File 'lib/ext/stackable_array.rb', line 31

def current
  self.current_key ? self.last[current_key] : self.last
end

#current=(value) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/ext/stackable_array.rb', line 23

def current=(value)
  if self.current.is_a?(Hash)
    self.last[current_key] = value
  else
    self << value
  end
end

#merge!(hash) ⇒ Object



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

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