Class: InvokeInstanceMethodHandler

Inherits:
AbstractCommandHandler show all
Defined in:
lib/javonet-ruby-sdk/core/handler/invoke_instance_method_handler.rb,
lib/javonet-ruby-sdk/Binaries/Ruby/Linux/X64/core/handler/invoke_instance_method_handler.rb,
lib/javonet-ruby-sdk/Binaries/Ruby/MacOs/X64/core/handler/invoke_instance_method_handler.rb,
lib/javonet-ruby-sdk/Binaries/Ruby/Windows/X64/core/handler/invoke_instance_method_handler.rb

Instance Method Summary collapse

Methods inherited from AbstractCommandHandler

#handle_command, #iterate

Constructor Details

#initializeInvokeInstanceMethodHandler

Returns a new instance of InvokeInstanceMethodHandler.



4
5
6
# File 'lib/javonet-ruby-sdk/core/handler/invoke_instance_method_handler.rb', line 4

def initialize
  @required_parameters_count = 2
end

Instance Method Details

#invoke_instance_method(command) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/javonet-ruby-sdk/core/handler/invoke_instance_method_handler.rb', line 12

def invoke_instance_method(command)
  begin
    if command.payload.length < @required_parameters_count
      raise ArgumentError.new "InvokeInstanceMethod parameters mismatch"
    end

    instance = command.payload[0]
    method_name = command.payload[1]

    if command.payload.length > 2
      arguments = command.payload[2..]
      return instance.send(method_name, *arguments)
    else
      return instance.send(method_name)
    end
  rescue NoMethodError
    methods = instance.methods
    message = "Method #{method_name} not found in object of class #{instance.class.name}. Available methods:\n"
    methods.each { |method_iter| message += "#{method_iter}\n" }
    raise Exception, message
  rescue Exception => e
    return e
  end
end

#process(command) ⇒ Object



8
9
10
# File 'lib/javonet-ruby-sdk/core/handler/invoke_instance_method_handler.rb', line 8

def process(command)
  invoke_instance_method(command)
end