Class: ArrayGetSizeHandler

Inherits:
AbstractCommandHandler show all
Defined in:
lib/javonet-ruby-sdk/core/handler/array_get_size_handler.rb,
lib/javonet-ruby-sdk/Binaries/Ruby/Linux/X64/core/handler/array_get_size_handler.rb,
lib/javonet-ruby-sdk/Binaries/Ruby/MacOs/X64/core/handler/array_get_size_handler.rb,
lib/javonet-ruby-sdk/Binaries/Ruby/Windows/X64/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.



4
5
6
# File 'lib/javonet-ruby-sdk/core/handler/array_get_size_handler.rb', line 4

def initialize
  @required_parameters_count = 1
end

Instance Method Details

#get_size(command) ⇒ Object



34
35
36
# File 'lib/javonet-ruby-sdk/core/handler/array_get_size_handler.rb', line 34

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

#get_size_array(command) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/javonet-ruby-sdk/core/handler/array_get_size_handler.rb', line 24

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

#process(command) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/javonet-ruby-sdk/core/handler/array_get_size_handler.rb', line 8

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

    if command.payload[0].is_a? Array
      return get_size_array(command)
    else
      return get_size(command)
    end
  rescue Exception => e
    return e
  end
end