Class: ArraySetItemHandler

Inherits:
AbstractCommandHandler show all
Defined in:
lib/javonet-ruby-sdk/core/handler/array_set_item_handler.rb,
lib/javonet-ruby-sdk/Binaries/Ruby/Linux/X64/core/handler/array_set_item_handler.rb,
lib/javonet-ruby-sdk/Binaries/Ruby/MacOs/X64/core/handler/array_set_item_handler.rb,
lib/javonet-ruby-sdk/Binaries/Ruby/Windows/X64/core/handler/array_set_item_handler.rb

Instance Method Summary collapse

Methods inherited from AbstractCommandHandler

#handle_command, #iterate

Constructor Details

#initializeArraySetItemHandler

Returns a new instance of ArraySetItemHandler.



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

def initialize
  @required_parameters_count = 3
end

Instance Method Details

#process(command) ⇒ Object



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

def process(command)
  begin
    if command.payload.length < @required_parameters_count
      raise ArgumentError.new "Array set item parameters mismatch"
    end
    if command.payload[0].is_a? Array
      set_item_array(command)
    elsif command.payload[0].is_a? Hash
      set_item_hash(command)
    else
      raise ArgumentError.new "Cannot set element of %s" % [command.payload[0]]
    end
    return 0
  rescue Exception => e
    return e
  end
end

#set_item_array(command) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/javonet-ruby-sdk/core/handler/array_set_item_handler.rb', line 26

def set_item_array(command)
  array = command.payload[0]
  value = command.payload[2]
  indexes = if command.payload[1].is_a? Array
              command.payload[1]
            else
              [command.payload[1]]
            end
  indexes[..-2].each { |i|
    array = array[i]
  }
  array[indexes[-1]] = value
end

#set_item_hash(command) ⇒ Object



40
41
42
43
44
45
# File 'lib/javonet-ruby-sdk/core/handler/array_set_item_handler.rb', line 40

def set_item_hash(command)
  hash = command.payload[0]
  key = command.payload[1]
  value = command.payload[2]
  hash[key] = value
end