Class: JsonEmitter::Stream

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/json-emitter/stream.rb

Overview

Represents a stream of JSON to be generated and yielded. It can be treated like any Enumerable. Unlike UnbufferedStream, the size of the yielded strings can vary from 1 to 1000’s.

Instance Method Summary collapse

Constructor Details

#initialize(enum) ⇒ Stream

Initialize a new stream.

Parameters:

  • enum (Enumerator)

    An enumerator that yields pieces of JSON.



14
15
16
# File 'lib/json-emitter/stream.rb', line 14

def initialize(enum)
  @enum = enum
end

Instance Method Details

#eachEnumerator

If a block is given, each chunk of JSON is yielded to it. If not block is given, an Enumerator is returned.

Returns:

  • (Enumerator)


34
35
36
37
38
39
40
41
42
# File 'lib/json-emitter/stream.rb', line 34

def each
  if block_given?
    @enum.each { |str|
      yield str
    }
  else
    @enum
  end
end

#write(io) ⇒ Object

Write the stream to the specified IO object.

Parameters:

  • io (IO)


23
24
25
26
27
# File 'lib/json-emitter/stream.rb', line 23

def write(io)
  each { |str|
    io << str
  }
end