This repository has been archived on 2015-04-30. You can view files and clone it, but cannot push or open issues or pull requests.
stream/ruby/jsh.rb
2015-04-29 17:14:31 -04:00

121 lines
1.8 KiB
Ruby

require 'json'
class Counter
def initialize
@count = 0
end
def increment
@count += 1
end
def clear
@count = 0
end
def count
@count
end
end
class Stream
def initialize(out, buf_size = 10, counter = nil, root = self)
counter = Counter.new if counter.nil?
@buf_size = buf_size
@out = out
@counter = counter
@root = root
@data = {}
@substreams = {}
@first = true
end
def start
'['
end
def start!
@out.write(start)
end
def flush
if @root == self
r = to_json
clear
@out.write(r)
@out.flush
return r
end
end
def stop
']'
end
def stop!
flush
@out.write(stop)
end
def clear
@counter.clear
@data = {}
@substreams.values.each do | substream |
substream.clear
end
end
def output(stream_name, obj)
if not @data.keys.include? stream_name
@data[stream_name] = []
end
@data[stream_name].push(obj)
@counter.increment
if @counter.count >= @buf_size
@root.flush
end
end
def new_stream(name)
s = Stream.new(@out, @buf_size, @counter, @root)
@substreams[name] = s
s
end
def empty?
empty = @data == {}
@substreams.values.each do |substream|
empty = empty and substream.empty?
end
empty
end
def add_comma_if_not_first(s)
if @first
@first = false
else
s.push(',')
end
end
def to_json
s = []
if not @data.empty?
add_comma_if_not_first(s)
s.push(@data.to_json)
end
@substreams.each do |key, stream|
if not stream.empty?
add_comma_if_not_first(s)
s.push("{#{key}:")
s.push(stream.start)
s.push(stream.to_json)
s.push(stream.stop)
s.push('}')
end
end
s.join ''
end
end