Class: Xmlss::ElementStack
- Inherits:
-
Object
- Object
- Xmlss::ElementStack
- Defined in:
- lib/xmlss/element_stack.rb
Instance Attribute Summary collapse
-
#markup_type ⇒ Object
readonly
this class is just a wrapper to Array.
Instance Method Summary collapse
- #empty? ⇒ Boolean
- #first ⇒ Object
-
#initialize(writer, markup_type) ⇒ ElementStack
constructor
A new instance of ElementStack.
- #last ⇒ Object (also: #current)
- #pop ⇒ Object
- #push(element) ⇒ Object
- #size ⇒ Object (also: #level)
- #using(element, &block) ⇒ Object
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_type ⇒ Object (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
19 |
# File 'lib/xmlss/element_stack.rb', line 19 def empty?; @stack.empty?; end |
#first ⇒ Object
21 |
# File 'lib/xmlss/element_stack.rb', line 21 def first; @stack.first; end |
#last ⇒ Object Also known as: current
22 |
# File 'lib/xmlss/element_stack.rb', line 22 def last; @stack.last; end |
#pop ⇒ Object
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 |
#size ⇒ Object 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 |