Sha256: d13def82cb7b3199160a4706e3d25f188cd348f0532debf26993499576149474

Contents?: true

Size: 1.27 KB

Versions: 5

Compression:

Stored size: 1.27 KB

Contents

module Xmlss

  class ElementStack

    # 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.

    attr_reader :markup_type

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

    def empty?; @stack.empty?; end
    def size;   @stack.size;   end
    def first;  @stack.first;  end
    def last;   @stack.last;   end

    alias_method :current, :last
    alias_method :level, :size

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

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

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

    private

    def close(element)
      @writer.pop(@markup_type)
      @written_level -= 1
      element
    end

    def write(element)
      @writer.write(element)
      element
    end

  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
xmlss-1.0.1 lib/xmlss/element_stack.rb
xmlss-1.0.0 lib/xmlss/element_stack.rb
xmlss-1.0.0.rc.4 lib/xmlss/element_stack.rb
xmlss-1.0.0.rc.3 lib/xmlss/element_stack.rb
xmlss-1.0.0.rc.2 lib/xmlss/element_stack.rb