Class: Hypertube::Core::Handler::GetStaticFieldHandler

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

Instance Method Summary collapse

Methods inherited from AbstractHandler

#handle_command, #validate

Constructor Details

#initializeGetStaticFieldHandler

Returns a new instance of GetStaticFieldHandler.



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

def initialize
  @required_parameters_count = 2
end

Instance Method Details

#get_static_field(command) ⇒ Object

Raises:

  • (ArgumentError)


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

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

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

  class_variable_defined = begin
    type.class_variable_defined?(value)
  rescue NameError
    false
  end
  return type.class_variable_get(value) if class_variable_defined

  constant_defined = begin
    type.const_defined?(value)
  rescue NameError
    false
  end
  return type.const_get(value) if constant_defined

  class_variables = type.class_variables
  message = "Field #{value} not found in object of class #{type.class.name}. Available fields:\n"
  class_variables.each { |field_iter| message += "#{field_iter}\n" }
  raise ArgumentError, message
end

#process(command) ⇒ Object



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

def process(command)
  get_static_field(command)
end