Sha256: e089e126eea79923ee345ac00bc5d6ecd6cf4e47dd4126e06e354a776e28fbcb

Contents?: true

Size: 803 Bytes

Versions: 2

Compression:

Stored size: 803 Bytes

Contents

require "undies/element"

module Undies
  class ElementStack < ::Array

    # an element stack is used to manage which element is receiving content
    # if an undies template is streaming io, then when an element is pushed,
    # its start tag is added to the stream and its end tag is added when popped.

    attr_reader :io

    def initialize(first_item=nil, io=nil, *args)
      @io = io
      # always initialize empty
      super()
      self.send(:<<, first_item) if first_item
    end

    def push(item)
      unless item.kind_of?(Element)
        raise ArgumentError, 'you can only push element nodes to an ElementStack'
      end
      super
      @io << item.start_tag if @io
      item
    end

    def pop
      item = super
      @io << item.end_tag if @io
      item
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
undies-1.2.0 lib/undies/element_stack.rb
undies-1.1.0 lib/undies/element_stack.rb