Class: ArrayGetItemHandler

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

Instance Method Summary collapse

Methods inherited from AbstractCommandHandler

#handle_command, #iterate

Constructor Details

#initializeArrayGetItemHandler

Returns a new instance of ArrayGetItemHandler.



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

def initialize
  @required_parameters_count = 2
end

Instance Method Details

#get_item_array(command) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/javonet-ruby-sdk/core/handler/array_get_item_handler.rb', line 22

def get_item_array(command)
  array = command.payload[0]
  indexes = if command.payload[1].is_a? Array
              command.payload[1]
            else
              command.payload[1..]
            end

  return unless array.is_a? Array

  indexes.each do |i|
    array = array[i]
  end
  array
end

#get_item_hash(command) ⇒ Object



38
39
40
41
42
# File 'lib/javonet-ruby-sdk/core/handler/array_get_item_handler.rb', line 38

def get_item_hash(command)
  hash = command.payload[0]
  key = command.payload[1]
  hash[key]
end

#process(command) ⇒ Object



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

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

  if command.payload[0].is_a? Array
    get_item_array(command)
  elsif command.payload[0].is_a? Hash
    get_item_hash(command)
  else
    raise ArgumentError.new format('Cannot get element from %s', command.payload[0])
  end
end