Class: Hypertube::Core::Handler::ArraySetItemHandler
Instance Method Summary
collapse
#handle_command, #validate
Constructor Details
Returns a new instance of ArraySetItemHandler.
9
10
11
|
# File 'lib/hypertube-ruby-sdk/core/handler/array_set_item_handler.rb', line 9
def initialize
@required_parameters_count = 3
end
|
Instance Method Details
#process(command) ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/hypertube-ruby-sdk/core/handler/array_set_item_handler.rb', line 13
def process(command)
validate(command, @required_parameters_count, self.class.name)
data = command.payload[0]
if data.is_a? Array
set_item_array(command)
elsif data.is_a? Hash
set_item_hash(command)
else
raise ArgumentError.new format('Cannot set element of %s', data)
end
data
end
|
#set_item_array(command) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/hypertube-ruby-sdk/core/handler/array_set_item_handler.rb', line 27
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 do |i|
array = array[i]
end
array[indexes[-1]] = value
end
|
#set_item_hash(command) ⇒ Object
41
42
43
44
45
46
|
# File 'lib/hypertube-ruby-sdk/core/handler/array_set_item_handler.rb', line 41
def set_item_hash(command)
hash = command.payload[0]
key = command.payload[1]
value = command.payload[2]
hash[key] = value
end
|