Class: ArrayGetSizeHandler

Inherits:
AbstractCommandHandler show all
Defined in:
lib/hypertube-ruby-sdk/core/handler/array_get_size_handler.rb

Instance Method Summary collapse

Methods inherited from AbstractCommandHandler

#handle_command, #iterate

Constructor Details

#initializeArrayGetSizeHandler

Returns a new instance of ArrayGetSizeHandler.



6
7
8
# File 'lib/hypertube-ruby-sdk/core/handler/array_get_size_handler.rb', line 6

def initialize
  @required_parameters_count = 1
end

Instance Method Details

#get_size(command) ⇒ Object



28
29
30
# File 'lib/hypertube-ruby-sdk/core/handler/array_get_size_handler.rb', line 28

def get_size(command)
  command.payload[0].length
end

#get_size_array(command) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/hypertube-ruby-sdk/core/handler/array_get_size_handler.rb', line 18

def get_size_array(command)
  array = command.payload[0]
  size = 1
  while array.is_a? Array
    size *= array.length
    array = array[0]
  end
  size
end

#process(command) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/hypertube-ruby-sdk/core/handler/array_get_size_handler.rb', line 10

def process(command)
  raise ArgumentError.new 'Array get size parameters mismatch' if command.payload.length < @required_parameters_count

  return get_size_array(command) if command.payload[0].is_a? Array

  get_size(command)
end