Class: Hiredis::Ruby::Reader::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/hiredis/ruby/reader.rb

Constant Summary collapse

METHOD_INDEX =
method_index.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer, parent = nil, depth = 0) ⇒ Task

Returns a new instance of Task.



39
40
41
# File 'lib/hiredis/ruby/reader.rb', line 39

def initialize(buffer, parent = nil, depth = 0)
  @buffer, @parent, @depth = buffer, parent, depth
end

Instance Attribute Details

#depthObject (readonly)

Returns the value of attribute depth.



36
37
38
# File 'lib/hiredis/ruby/reader.rb', line 36

def depth
  @depth
end

#multi_bulkObject

Returns the value of attribute multi_bulk.



37
38
39
# File 'lib/hiredis/ruby/reader.rb', line 37

def multi_bulk
  @multi_bulk
end

#parentObject (readonly)

Returns the value of attribute parent.



35
36
37
# File 'lib/hiredis/ruby/reader.rb', line 35

def parent
  @parent
end

Instance Method Details

#childObject

Note: task depth is not checked.



44
45
46
# File 'lib/hiredis/ruby/reader.rb', line 44

def child
  @child ||= Task.new(@buffer, self, depth + 1)
end

#processObject



100
101
102
103
104
105
106
107
108
109
# File 'lib/hiredis/ruby/reader.rb', line 100

def process
  @line ||= @buffer.read_line
  return false if @line == false

  @type ||= @line.slice!(0)
  reply = send(METHOD_INDEX[@type] || :process_protocol_error)

  reset! if reply != false
  reply
end

#process_bulk_replyObject



68
69
70
71
72
73
74
# File 'lib/hiredis/ruby/reader.rb', line 68

def process_bulk_reply
  bulk_length = @line.to_i
  return nil if bulk_length < 0

  # Caught by caller function when false
  @buffer.read(bulk_length, 2)
end

#process_error_replyObject



56
57
58
# File 'lib/hiredis/ruby/reader.rb', line 56

def process_error_reply
  RuntimeError.new(@line)
end

#process_integer_replyObject



64
65
66
# File 'lib/hiredis/ruby/reader.rb', line 64

def process_integer_reply
  @line.to_i
end

#process_multi_bulk_replyObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/hiredis/ruby/reader.rb', line 76

def process_multi_bulk_reply
  multi_bulk_length = @line.to_i

  if multi_bulk_length > 0
    @multi_bulk ||= []

    # We know the multi bulk is not complete when this path is taken.
    while (element = child.process) != false
      @multi_bulk << element
      return @multi_bulk if @multi_bulk.length == multi_bulk_length
    end

    false
  elsif multi_bulk_length == 0
    []
  else
    nil
  end
end

#process_protocol_errorObject



96
97
98
# File 'lib/hiredis/ruby/reader.rb', line 96

def process_protocol_error
  raise "Protocol error"
end

#process_status_replyObject



60
61
62
# File 'lib/hiredis/ruby/reader.rb', line 60

def process_status_reply
  @line
end

#reset!Object



52
53
54
# File 'lib/hiredis/ruby/reader.rb', line 52

def reset!
  @line = @type = @multi_bulk = nil
end

#rootObject



48
49
50
# File 'lib/hiredis/ruby/reader.rb', line 48

def root
  parent ? parent.root : self
end