Class: JsonEmitter::Emitter

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

Overview

Builds Enumerators that yield JSON from Ruby Arrays or Hashes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEmitter



9
10
11
# File 'lib/json-emitter/emitter.rb', line 9

def initialize
  @context = Context.new
end

Instance Attribute Details

#contextJsonEmitter::Context (readonly)



7
8
9
# File 'lib/json-emitter/emitter.rb', line 7

def context
  @context
end

Instance Method Details

#array(enum) { ... } ⇒ Enumerator

Generates an Enumerator that will stream out a JSON array.

Yields:

  • If a block is given, it will be yielded each value in the array. The return value from the block will be converted to JSON instead of the original value.



20
21
22
23
24
25
26
27
28
# File 'lib/json-emitter/emitter.rb', line 20

def array(enum, &mapper)
  Enumerator.new { |y|
    context.execute {
      array_generator(enum, &mapper).each { |json_val|
        y << json_val
      }
    }
  }
end

#object(hash) ⇒ Enumerator

Generates an Enumerator that will stream out a JSON object.



36
37
38
39
40
41
42
43
44
# File 'lib/json-emitter/emitter.rb', line 36

def object(hash)
  Enumerator.new { |y|
    context.execute {
      object_generator(hash).each { |json_val|
        y << json_val
      }
    }
  }
end