Class: RHCP::Client::CommandStub

Inherits:
RHCP::Command show all
Defined in:
lib/rhcp/client/command_stub.rb

Overview

This is a stub that represents a remote RHCP::Command. Instances of this class will live in a client that uses one of the brokers from the RHCP::Client package. The interesting aspect about this stub is that the execute method is modified so that it does not execute the command directly, but can be modified from outside (by setting the execute_block property) so that broker classes can inject their remote invocation logic.

Instance Attribute Summary collapse

Attributes inherited from RHCP::Command

#accepts_extra_params, #available_if, #block, #default_param, #description, #enabled_through_context_keys, #full_name, #ignores_extra_params, #is_read_only, #name, #notifications, #params, #result_hints

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RHCP::Command

#accept_extra_params, #add_as_instance_method, #add_param, #execute, #execute_from_ruby_args, #get_mandatory_params, #get_param, #ignore_extra_params, #is_enabled?, #mark_as_read_only, #to_json

Constructor Details

#initialize(name, description) ⇒ CommandStub

constructs a new instance should not be invoked directly, but is called from reconstruct_from_json



24
25
26
# File 'lib/rhcp/client/command_stub.rb', line 24

def initialize(name, description)
  super(name, description, lambda {})
end

Instance Attribute Details

#execute_blockObject

Returns the value of attribute execute_block.



20
21
22
# File 'lib/rhcp/client/command_stub.rb', line 20

def execute_block
  @execute_block
end

Class Method Details

.reconstruct_from_json(json_data) ⇒ Object

builds a CommandStub out of some json data (either serialized as string or already unpacked into a ruby-hash) all nested params are unmarshalled as RHCP::CommandParamStub instances so that we are able to inject the logic for invoking get_lookup_values remotely



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rhcp/client/command_stub.rb', line 33

def self.reconstruct_from_json(json_data)
  object = json_data.instance_of?(Hash) ? json_data : JSON.parse(json_data)
  args = object.values_at('name', 'description')
  instance = self.new(*args)
  if object.values_at('read_only')
    instance.mark_as_read_only
  end
  instance.result_hints = {
    :display_type => object['result_hints']['display_type'],
    :overview_columns => object['result_hints']['overview_columns'],
    :column_titles => object['result_hints']['column_titles']
  }
  object['params'].each do |p|
    param = RHCP::Client::CommandParamStub.reconstruct_from_json(p)
    instance.add_param(param)
  end
  instance
end

Instance Method Details

#execute_request(request) ⇒ Object

we don’t want to execute the command block as the normal RHCP::Command does, but rather want to call the block injected by the registry that has created this stub.



55
56
57
# File 'lib/rhcp/client/command_stub.rb', line 55

def execute_request(request)
  @execute_block.call(request)
end