Class: Xmlss::ElementStack

Inherits:
Object
  • Object
show all
Defined in:
lib/xmlss/element_stack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(writer, markup_type) ⇒ ElementStack

Returns a new instance of ElementStack.



12
13
14
15
16
17
# File 'lib/xmlss/element_stack.rb', line 12

def initialize(writer, markup_type)
  @stack = []
  @writer = writer
  @markup_type = markup_type.to_s
  @written_level = 0
end

Instance Attribute Details

#markup_typeObject (readonly)

this class is just a wrapper to Array. I want to treat this as a stack of objects for the workbook DSL to reference. I need to push an object onto the stack, reference it using the ‘current’ method, and pop it off the stack when I’m done.



10
11
12
# File 'lib/xmlss/element_stack.rb', line 10

def markup_type
  @markup_type
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


19
# File 'lib/xmlss/element_stack.rb', line 19

def empty?; @stack.empty?; end

#firstObject



21
# File 'lib/xmlss/element_stack.rb', line 21

def first;  @stack.first;  end

#lastObject Also known as: current



22
# File 'lib/xmlss/element_stack.rb', line 22

def last;   @stack.last;   end

#popObject



42
43
44
45
46
# File 'lib/xmlss/element_stack.rb', line 42

def pop
  if !empty?
    @written_level < level ? write(@stack.pop) : close(@stack.pop)
  end
end

#push(element) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/xmlss/element_stack.rb', line 33

def push(element)
  if @written_level < level
    write(current)
    @writer.push(@markup_type)
    @written_level += 1
  end
  @stack.push(element)
end

#sizeObject Also known as: level



20
# File 'lib/xmlss/element_stack.rb', line 20

def size;   @stack.size;   end

#using(element, &block) ⇒ Object



27
28
29
30
31
# File 'lib/xmlss/element_stack.rb', line 27

def using(element, &block)
  push(element)
  (block || Proc.new {}).call
  pop
end