Class: TurboStreamer::WankelEncoder

Inherits:
Wankel::StreamEncoder
  • Object
show all
Defined in:
lib/turbostreamer/encoders/wankel.rb

Instance Method Summary collapse

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, options={})
  @stack = []
  @indexes = []

  super(io, {mode: :as_json}.merge(options))
end

Instance Method Details

#array_closeObject



42
43
44
45
46
# File 'lib/turbostreamer/encoders/wankel.rb', line 42

def array_close
  @indexes.pop
  @stack.pop
  super
end

#array_openObject



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_closeObject



30
31
32
33
34
# File 'lib/turbostreamer/encoders/wankel.rb', line 30

def map_close
  @indexes.pop
  @stack.pop
  super
end

#map_openObject



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