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.



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

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

  # Require 3 nested tasks
  @child = Task.new(@buffer, self, depth + 1) if depth < 2
end

Instance Attribute Details

#childObject

Returns the value of attribute child.



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

def child
  @child
end

#multi_bulkObject

Returns the value of attribute multi_bulk.



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

def multi_bulk
  @multi_bulk
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

Instance Method Details

#processObject



97
98
99
100
101
102
103
104
105
106
# File 'lib/hiredis/ruby/reader.rb', line 97

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



65
66
67
68
69
70
71
# File 'lib/hiredis/ruby/reader.rb', line 65

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



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

def process_error_reply
  RuntimeError.new(@line)
end

#process_integer_replyObject



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

def process_integer_reply
  @line.to_i
end

#process_multi_bulk_replyObject



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

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



93
94
95
# File 'lib/hiredis/ruby/reader.rb', line 93

def process_protocol_error
  raise "Protocol error"
end

#process_status_replyObject



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

def process_status_reply
  @line
end

#reset!Object



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

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

#rootObject



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

def root
  parent ? parent.root : self
end