Class: TurboStreamer::OjEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/turbostreamer/encoders/oj.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, options = {}) ⇒ OjEncoder



8
9
10
11
12
13
14
15
16
17
# File 'lib/turbostreamer/encoders/oj.rb', line 8

def initialize(io, options={})
  @stack = []
  @indexes = []

  @options = {mode: :json}.merge(options)

  @output = io
  @stream_writer = ::Oj::StreamWriter.new(io, @options)
  @write_comma_on_next_push = false
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



6
7
8
# File 'lib/turbostreamer/encoders/oj.rb', line 6

def output
  @output
end

Instance Method Details

#array_closeObject



65
66
67
68
69
# File 'lib/turbostreamer/encoders/oj.rb', line 65

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

#array_openObject



56
57
58
59
60
61
62
63
# File 'lib/turbostreamer/encoders/oj.rb', line 56

def array_open
  @stack << :array
  @indexes << 0
  if @write_comma_on_next_push
    @write_comma_on_next_push = false
  end
  @stream_writer.push_array
end

#capture(to = nil) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/turbostreamer/encoders/oj.rb', line 103

def capture(to=nil)
  @stream_writer.flush

  old_writer = @stream_writer
  old_output = @output
  @indexes << 0

  @output = (to || ::StringIO.new)
  @stream_writer = ::Oj::StreamWriter.new(@output, @options)

  # This is to prevent error from OJ streamer
  # We will strip the brackets afterward
  if @stack.last == :map
    @stream_writer.push_object
  elsif @stack.last == :array
    @stream_writer.push_array
  end

  yield

  @stream_writer.pop_all
  @stream_writer.flush
  result = output.string.sub(/\A,/, ''.freeze).chomp(",".freeze).strip

  # Strip brackets as promised above
  if @stack.last == :map
    result = result.sub(/\A{/, ''.freeze).chomp("}".freeze)
  elsif @stack.last == :array
    result = result.sub(/\A\[/, ''.freeze).chomp("]".freeze)
  end

  # Possible for `output.string` to have value like
  # `[,{"key":"value"}]\n`
  # Thus the comma must be removed here
  result.sub(/\A,/, ''.freeze)
ensure
  @indexes.pop
  @stream_writer = old_writer
  @output = old_output
end

#flushObject



144
145
146
# File 'lib/turbostreamer/encoders/oj.rb', line 144

def flush
  @stream_writer.flush
end

#inject(string) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/turbostreamer/encoders/oj.rb', line 71

def inject(string)
  @stream_writer.flush

  # It's possible to have
  # `@write_comma_on_next_push == true` and `@indexes.last > 0`
  # So there might be double comma written without this flag
  comma_written = false
  if @write_comma_on_next_push
    @output.write(",".freeze)
    @write_comma_on_next_push = false
    comma_written = true
  end

  if @stack.last == :array && !string.empty?
    if @indexes.last > 0
      self.output.write(",") unless comma_written
    else
      @write_comma_on_next_push = true
    end
    @indexes[-1] += 1
  elsif @stack.last == :map && !string.empty?
    if @indexes.last > 0
      self.output.write(",") unless comma_written
    else
      @write_comma_on_next_push = true
    end
    @indexes[-1] += 1
  end

  self.output.write(string.sub(/\A,/, ''.freeze).chomp(",".freeze).strip)
end

#key(k) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/turbostreamer/encoders/oj.rb', line 19

def key(k)
  if @write_comma_on_next_push && (@stack.last == :array || @stack.last == :map)
    @stream_writer.flush
    @output.write(",".freeze)
    @write_comma_on_next_push = false
  end
  @stream_writer.push_key(k)
end

#map_closeObject



50
51
52
53
54
# File 'lib/turbostreamer/encoders/oj.rb', line 50

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

#map_openObject



41
42
43
44
45
46
47
48
# File 'lib/turbostreamer/encoders/oj.rb', line 41

def map_open
  @stack << :map
  @indexes << 0
  if @write_comma_on_next_push
    @write_comma_on_next_push = false
  end
  @stream_writer.push_object
end

#value(v) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/turbostreamer/encoders/oj.rb', line 28

def value(v)
  if @stack.last == :array || @stack.last == :map
    @indexes[-1] += 1

    if @write_comma_on_next_push
      @stream_writer.flush
      @output.write(",".freeze)
      @write_comma_on_next_push = false
    end
  end
  @stream_writer.push_value(v)
end