Class: Ruty::Datastructure::NodeStream

Inherits:
Object
  • Object
show all
Defined in:
lib/ruty/datastructure.rb

Overview

stream class. Some kind of write only array which just accepts nodes and can be converted into a nodelist afterwards.

Instance Method Summary collapse

Constructor Details

#initialize(parser) ⇒ NodeStream

Returns a new instance of NodeStream.



78
79
80
81
82
# File 'lib/ruty/datastructure.rb', line 78

def initialize parser
  @parser = parser
  @stream = []
  @nodelist = nil
end

Instance Method Details

#<<(node) ⇒ Object

add a new node to the stream

Raises:

  • (RuntimeError)


85
86
87
88
# File 'lib/ruty/datastructure.rb', line 85

def << node
  raise RuntimeError, 'cannot write to closed stream' if @nodelist
  @stream << node
end

#to_nodelistObject

convert the streamed data into a nodelist. This automatically closes the stream, you can’t write to it later.



93
94
95
96
# File 'lib/ruty/datastructure.rb', line 93

def to_nodelist
  @nodelist = NodeList.new(@stream, @parser) if not @nodelist
  @nodelist
end