Class: TurboStreamer::WankelEncoder
- Inherits:
-
Wankel::StreamEncoder
- Object
- Wankel::StreamEncoder
- TurboStreamer::WankelEncoder
- Defined in:
- lib/turbostreamer/encoders/wankel.rb
Instance Method Summary collapse
- #array_close ⇒ Object
- #array_open ⇒ Object
- #capture(to = nil) ⇒ Object
-
#initialize(io, options = {}) ⇒ WankelEncoder
constructor
A new instance of WankelEncoder.
- #inject(string) ⇒ Object
- #key(k) ⇒ Object
- #map_close ⇒ Object
- #map_open ⇒ Object
- #value(v) ⇒ Object
Constructor Details
#initialize(io, options = {}) ⇒ WankelEncoder
Returns a new instance of WankelEncoder.
6 7 8 9 10 11 |
# File 'lib/turbostreamer/encoders/wankel.rb', line 6 def initialize(io, ={}) @stack = [] @indexes = [] super(io, {mode: :as_json}.merge()) end |
Instance Method Details
#array_close ⇒ Object
42 43 44 45 46 |
# File 'lib/turbostreamer/encoders/wankel.rb', line 42 def array_close @indexes.pop @stack.pop super end |
#array_open ⇒ Object
36 37 38 39 40 |
# File 'lib/turbostreamer/encoders/wankel.rb', line 36 def array_open @stack << :array @indexes << 0 super end |
#capture(to = nil) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/turbostreamer/encoders/wankel.rb', line 66 def capture(to=nil) flush old_output = self.output to = to || ::StringIO.new @indexes << 0 self.output = to yield flush to.string.sub(/\A,/, ''.freeze).chomp(",".freeze) ensure @indexes.pop self.output = old_output end |
#inject(string) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/turbostreamer/encoders/wankel.rb', line 48 def inject(string) flush if @stack.last == :array self.output.write(','.freeze) if @indexes.last > 0 @indexes[-1] += 1 elsif @stack.last == :map self.output.write(','.freeze) if @indexes.last > 0 capture do string("".freeze) string("".freeze) end @indexes[-1] += 1 end self.output.write(string) end |
#key(k) ⇒ Object
13 14 15 |
# File 'lib/turbostreamer/encoders/wankel.rb', line 13 def key(k) string(k) end |
#map_close ⇒ Object
30 31 32 33 34 |
# File 'lib/turbostreamer/encoders/wankel.rb', line 30 def map_close @indexes.pop @stack.pop super end |
#map_open ⇒ Object
24 25 26 27 28 |
# File 'lib/turbostreamer/encoders/wankel.rb', line 24 def map_open @stack << :map @indexes << 0 super end |
#value(v) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/turbostreamer/encoders/wankel.rb', line 17 def value(v) if @stack.last == :array || @stack.last == :map @indexes[-1] += 1 end super end |