Class: Hypertube::Core::Handler::SetInstanceFieldHandler
Instance Method Summary
collapse
#handle_command, #validate
Constructor Details
Returns a new instance of SetInstanceFieldHandler.
9
10
11
|
# File 'lib/hypertube-ruby-sdk/core/handler/set_instance_field_handler.rb', line 9
def initialize
@required_parameters_count = 3
end
|
Instance Method Details
#process(command) ⇒ Object
13
14
15
|
# File 'lib/hypertube-ruby-sdk/core/handler/set_instance_field_handler.rb', line 13
def process(command)
set_instance_field(command)
end
|
#set_instance_field(command) ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/hypertube-ruby-sdk/core/handler/set_instance_field_handler.rb', line 17
def set_instance_field(command)
validate(command, @required_parameters_count, self.class.name)
instance = command.payload[0]
field_name = command.payload[1].to_s
new_value = command.payload[2]
field = field_name.start_with?('@') ? field_name : "@#{field_name}"
field_defined = begin
instance.instance_variable_defined?(field)
rescue NameError
false
end
unless field_defined
fields = instance.instance_variables
value_type = new_value.nil? ? 'null' : new_value.class.name
class_name = instance.class.name
message = "Failed to set value of type #{value_type} for #{field_name} in class #{class_name}.\nAvailable public instance variables:\n"
fields.each { |field_iter| message += "#{field_iter}\n" }
raise ArgumentError, message
end
instance.instance_variable_set(field, new_value)
instance
end
|