Class: Elster::Streamer
- Inherits:
-
Object
- Object
- Elster::Streamer
- Defined in:
- lib/elster/streamer.rb
Instance Method Summary (collapse)
-
- (Object) add(value = nil, &block)
Output an array.
-
- (Object) close(close_stream = false)
Close the encoding optionally closing the provided output stream.
-
- (Streamer) initialize(output)
constructor
Create a new instance of Streamer with the specified output stream.
-
- (Object) key(key, value = nil, &block)
Output an object key value pair.
Constructor Details
- (Streamer) initialize(output)
Create a new instance of Streamer with the specified output stream. The `output` must respond to `write`.
8 9 10 11 12 13 |
# File 'lib/elster/streamer.rb', line 8 def initialize(output) @output = output # Some mutable state to make Rich Hickey weep. @stack = [] @item_count = 0 end |
Instance Method Details
- (Object) add(value = nil, &block)
Output an array. If a block is passes the `value` will be ignored and a nested value started.
Example:
json.add(1)
json.add("Ansible")
[ 1, "Ansible" ]
json.add(1)
json.add do
json.set(:name, "Wiggens")
end
[ 1, { "name" : "Wiggens" } ]
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/elster/streamer.rb', line 83 def add(value=nil, &block) if @item_count > 0 write "," else @current_type = :array begin_section end if block nest_in block.call nest_out else write encode_value(value) end @item_count += 1 end |
- (Object) close(close_stream = false)
Close the encoding optionally closing the provided output stream.
16 17 18 19 20 21 |
# File 'lib/elster/streamer.rb', line 16 def close(close_stream=false) end_section if close_stream @output.close end end |
- (Object) key(key, value = nil, &block)
Output an object key value pair. If a block is passed, the `value` will be ignored and a nested object or array can be added.
Example:
json.key(:mistake, "HUGE!")
{ "mistake" : "HUGE!" }
json.key(:mistake) do
json.key(:type, "HUGE!")
end
{ "mistake" : { "type" : "HUGE!" } }
json.key(:mistake) do
json.add("HUGE!")
end
{ "mistake" : [ "HUGE!" ] }
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/elster/streamer.rb', line 45 def key(key, value=nil, &block) if @item_count > 0 write "," else @current_type = :object begin_section end write encode_value(key) write ":" if block nest_in block.call nest_out else write encode_value(value) end @item_count += 1 end |