Class: Hypertube::Core::Handler::GetInstanceFieldHandler

Inherits:
AbstractHandler show all
Defined in:
lib/hypertube-ruby-sdk/core/handler/get_instance_field_handler.rb

Instance Method Summary collapse

Methods inherited from AbstractHandler

#handle_command, #validate

Constructor Details

#initializeGetInstanceFieldHandler

Returns a new instance of GetInstanceFieldHandler.



9
10
11
# File 'lib/hypertube-ruby-sdk/core/handler/get_instance_field_handler.rb', line 9

def initialize
  @required_parameters_count = 2
end

Instance Method Details

#get_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
# File 'lib/hypertube-ruby-sdk/core/handler/get_instance_field_handler.rb', line 17

def get_instance_field(command)
  validate(command, @required_parameters_count, self.class.name)

  instance = command.payload[0]
  value = command.payload[1].to_s

  field = value.start_with?('@') ? value : "@#{value}"
  field_defined = begin
    instance.instance_variable_defined?(field)
  rescue NameError
    false
  end

  unless field_defined
    fields = instance.instance_variables
    message = "Field #{value} not found in object of class #{instance.class.name}. Available fields:\n"
    fields.each { |field_iter| message += "#{field_iter}\n" }
    raise ArgumentError, message
  end
  instance.instance_variable_get(field)
end

#process(command) ⇒ Object



13
14
15
# File 'lib/hypertube-ruby-sdk/core/handler/get_instance_field_handler.rb', line 13

def process(command)
  get_instance_field(command)
end