Class: Undies::NodeStack

Inherits:
Object
  • Object
show all
Defined in:
lib/undies/node_stack.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ NodeStack

Returns a new instance of NodeStack.



20
21
22
23
24
25
# File 'lib/undies/node_stack.rb', line 20

def initialize(output)
  @stack = []
  @cached_node = nil
  @output = output
  @written_level = 0
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



17
18
19
# File 'lib/undies/node_stack.rb', line 17

def buffer
  @buffer
end

#cached_nodeObject

Returns the value of attribute cached_node.



18
19
20
# File 'lib/undies/node_stack.rb', line 18

def cached_node
  @cached_node
end

#outputObject (readonly)

Returns the value of attribute output.



17
18
19
# File 'lib/undies/node_stack.rb', line 17

def output
  @output
end

#stackObject (readonly)

Returns the value of attribute stack.



17
18
19
# File 'lib/undies/node_stack.rb', line 17

def stack
  @stack
end

Class Method Details

.create(output) ⇒ Object

the stack first caches new nodes before pushing them onto the stack and node output is buffered as new nodes are pushed



13
14
15
# File 'lib/undies/node_stack.rb', line 13

def self.create(output)
  output.kind_of?(NodeStack) ? output : NodeStack.new(output)
end

Instance Method Details

#clear_cachedObject



64
65
66
67
68
69
70
71
72
# File 'lib/undies/node_stack.rb', line 64

def clear_cached
  node_to_push, self.cached_node = self.cached_node, nil
  if node_to_push
    push(node_to_push)
    node_to_push.class.builds(node_to_push).each { |build| build.call }
    clear_cached
    pop
  end
end

#empty?Boolean

Returns:

  • (Boolean)


27
# File 'lib/undies/node_stack.rb', line 27

def empty?; @stack.empty?; end

#firstObject



29
# File 'lib/undies/node_stack.rb', line 29

def first;  @stack.first;  end

#flushObject



60
61
62
# File 'lib/undies/node_stack.rb', line 60

def flush
  clear_cached
end

#lastObject Also known as: current



30
# File 'lib/undies/node_stack.rb', line 30

def last;   @stack.last;   end

#node(node) ⇒ Object



55
56
57
58
# File 'lib/undies/node_stack.rb', line 55

def node(node)
  clear_cached
  self.cached_node = node
end

#pop(*args) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/undies/node_stack.rb', line 45

def pop(*args)
  if !empty?
    node = if @written_level < level
      @stack.pop.tap { |node| write(node) }
    else
      @stack.pop.tap { |node| close(node) }
    end
  end
end

#push(node) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/undies/node_stack.rb', line 35

def push(node)
  if current
    current.class.set_children(current, node.kind_of?(Element))
  end
  if @written_level < level
    open(current)
  end
  @stack.push(node)
end

#sizeObject Also known as: level



28
# File 'lib/undies/node_stack.rb', line 28

def size;   @stack.size;   end