Class: Groonga::Command::Parser::LoadValuesParser

Inherits:
Object
  • Object
show all
Defined in:
lib/groonga/command/parser/load-values-parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLoadValuesParser

Returns a new instance of LoadValuesParser.



26
27
28
29
30
31
32
# File 'lib/groonga/command/parser/load-values-parser.rb', line 26

def initialize
  @parser = JSON::ResumableParser.new
  @on_value = nil
  @on_consumed = nil
  @on_end = nil
  @n_processed_values = 0
end

Instance Attribute Details

#on_consumed=(value) ⇒ Object (writeonly)

Sets the attribute on_consumed

Parameters:

  • value

    the value to set the attribute on_consumed to.



24
25
26
# File 'lib/groonga/command/parser/load-values-parser.rb', line 24

def on_consumed=(value)
  @on_consumed = value
end

#on_end=(value) ⇒ Object (writeonly)

Sets the attribute on_end

Parameters:

  • value

    the value to set the attribute on_end to.



25
26
27
# File 'lib/groonga/command/parser/load-values-parser.rb', line 25

def on_end=(value)
  @on_end = value
end

#on_value=(value) ⇒ Object (writeonly)

Sets the attribute on_value

Parameters:

  • value

    the value to set the attribute on_value to.



23
24
25
# File 'lib/groonga/command/parser/load-values-parser.rb', line 23

def on_value=(value)
  @on_value = value
end

Instance Method Details

#<<(data) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/groonga/command/parser/load-values-parser.rb', line 34

def <<(data)
  data_size = data.bytesize
  return self if data_size.zero?

  @parser << data
  begin
    done = @parser.parse
  rescue JSON::ParserError => error
    consumed = data_size - @parser.rest.bytesize
    raise Error.new(error.message,
                    data[0, consumed],
                    data[consumed..-1])
  end

  if done
    @parser.value[@n_processed_values..-1].each do |value|
      @on_value.call(value)
    end
    @n_processed_values = 0
  else
    partial_value = @parser.partial_value
    if partial_value
      partial_value[@n_processed_values..-2].each do |value|
        @on_value.call(value)
      end
      @n_processed_values = partial_value.size - 1
    end
  end

  consumed = data.bytesize
  consumed -= @parser.rest.bytesize if done
  if consumed > 0
    if consumed < data_size
      @on_consumed.call(data[0, consumed])
    else
      @on_consumed.call(data)
    end
  end

  if done
    if consumed < data_size
      @on_end.call(data[consumed..-1])
    else
      @on_end.call(nil)
    end
  end

  self
end