Class: SetStaticFieldHandler

Inherits:
AbstractCommandHandler show all
Defined in:
lib/javonet-ruby-sdk/core/handler/set_static_field_handler.rb

Instance Method Summary collapse

Methods inherited from AbstractCommandHandler

#handle_command, #iterate

Constructor Details

#initializeSetStaticFieldHandler

Returns a new instance of SetStaticFieldHandler.



6
7
8
# File 'lib/javonet-ruby-sdk/core/handler/set_static_field_handler.rb', line 6

def initialize
  @required_parameters_count = 3
end

Instance Method Details

#process(command) ⇒ Object



10
11
12
# File 'lib/javonet-ruby-sdk/core/handler/set_static_field_handler.rb', line 10

def process(command)
  Command.new(RuntimeNameJavonet::RUBY, CommandType::VALUE, [set_static_field(command)])
end

#set_static_field(command) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/javonet-ruby-sdk/core/handler/set_static_field_handler.rb', line 14

def set_static_field(command)
  if command.payload.length != @required_parameters_count
    raise ArgumentError.new 'Set static field parameters mismatch'
  end

  type = command.payload[0]
  field_name = command.payload[1]
  field_value = command.payload[2]

  type.class_variable_set(field_name, field_value)
  type
rescue Exception
  fields = type.class_variables
  message = "Field #{command.payload[1]} not found in class #{type.name}. Available fields:\n"
  fields.each { |field_iter| message += "#{field_iter}\n" }
  raise ArgumentError, message
end