Class: InvocationContext
- Inherits:
-
AbstractInvocationContext
- Object
- AbstractInvocationContext
- InvocationContext
- Includes:
- Enumerable
- Defined in:
- lib/hypertube-ruby-sdk/sdk/invocation_context.rb
Overview
InvocationContext is a class that represents a context for invoking commands. It implements several interfaces for different types of interactions. This class is used to construct chains of invocations, representing expressions of interaction that have not yet been executed.
Instance Attribute Summary collapse
-
#current_command ⇒ Object
readonly
Returns the value of attribute current_command.
-
#response_command ⇒ Object
readonly
Returns the value of attribute response_command.
Instance Method Summary collapse
- #[](*indexes) ⇒ Object
- #[]=(*indexes_and_value) ⇒ Object
-
#as_dto(act_as_dto = true) ⇒ InvocationContext
Returns a new InvocationContext that acts as a DTO (Data Transfer Object).
- #build_command(command) ⇒ Object
-
#convert_response_to_dto(response_command) ⇒ InvocationContext
Converts a ProjectResultAsDto response into an InvocationContext DTO.
-
#create_instance(*args) ⇒ InvocationContext
Creates a new instance of a class in the target runtime.
-
#create_null ⇒ InvocationContext
Creates a null object of a specific type in the target runtime.
- #encapsulate_payload_item(payload_item) ⇒ Object
-
#execute ⇒ InvocationContext
Executes the current command.
- #execute_async ⇒ Object
-
#get_enum_name ⇒ InvocationContext
Retrieves the name of an enum from the target runtime.
-
#get_enum_value ⇒ InvocationContext
Retrieves the value of an enum from the target runtime.
-
#get_index(*indexes) ⇒ InvocationContext
Retrieves the value at a specific indexes in an array from the target runtime.
-
#get_instance_field(field_name) ⇒ InvocationContext
Retrieves the value of an instance field from the target runtime.
-
#get_instance_method_as_delegate(method_name, *args) ⇒ InvocationContext
Retrieves an instance method as a delegate from the target runtime.
-
#get_rank ⇒ InvocationContext
Retrieves the rank of an array from the target runtime.
-
#get_ref_value ⇒ InvocationContext
Retrieves the value of a reference from the target runtime.
-
#get_size ⇒ InvocationContext
Retrieves the number of elements from the target runtime.
-
#get_static_field(field_name) ⇒ InvocationContext
Retrieves the value of a static field from the target runtime.
-
#get_static_method_as_delegate(method_name, *args) ⇒ InvocationContext
Retrieves a static method as a delegate from the target runtime.
-
#get_value ⇒ Object
Retrieves the value of the current command from the target runtime.
-
#initialize(runtime_name, connection_type, tcp_ip_address, command, is_executed = false, is_dto = false) ⇒ InvocationContext
constructor
A new instance of InvocationContext.
-
#invoke_generic_method(method_name, *args) ⇒ InvocationContext
Invokes a generic method on the target runtime.
-
#invoke_generic_static_method(method_name, *args) ⇒ InvocationContext
Invokes a generic static method on the target runtime.
-
#invoke_instance_method(method_name, *args) ⇒ InvocationContext
Invokes an instance method on the target runtime.
-
#invoke_static_method(method_name, *args) ⇒ InvocationContext
Invokes a static method on the target runtime.
-
#iterator ⇒ Object
def finalize(command, is_executed, interpreter, connection_type, tcp_ip_address) proc do if command.command_type == CommandType::REFERENCE && is_executed == true destruct_command = Command.new(@runtime_name, CommandType::DESTRUCT_REFERENCE, command.payload) interpreter.execute(destruct_command, connection_type, tcp_ip_address) end end end.
-
#project_result_as_dto(*properties_names) ⇒ InvocationContext
Projects the result as a DTO with the specified property names.
-
#register_for_update ⇒ InvocationContext
Registers the current context for updates by wrapping it in a REGISTER_FOR_UPDATE command.
-
#retrieve_array ⇒ Object
Retrieves an array from the target runtime.
-
#set_index(indexes, value) ⇒ InvocationContext
Sets the value at a specific index in an array in the target runtime.
-
#set_instance_field(field_name, value) ⇒ InvocationContext
Sets the value of an instance field in the target runtime.
-
#set_static_field(field_name, value) ⇒ InvocationContext
Sets the value of a static field in the target runtime.
Methods inherited from AbstractInvocationContext
Constructor Details
#initialize(runtime_name, connection_type, tcp_ip_address, command, is_executed = false, is_dto = false) ⇒ InvocationContext
Returns a new instance of InvocationContext.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 18 def initialize(runtime_name, connection_type, tcp_ip_address, command, is_executed = false, is_dto = false) @is_executed = is_executed @is_dto = is_dto @runtime_name = runtime_name @connection_type = connection_type @tcp_ip_address = tcp_ip_address @current_command = command @response_command = nil # generates error: ruby/lib/RuntimeBridge.rb-ruby-sdk/sdk/internal/invocation_context.rb:17: warning: finalizer references object to be finalized # ObjectSpace.define_finalizer(self, self.finalize(@current_command, @is_executed, @interpreter, @connection_type, @tcp_ip_address)) end |
Instance Attribute Details
#current_command ⇒ Object (readonly)
Returns the value of attribute current_command.
16 17 18 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 16 def current_command @current_command end |
#response_command ⇒ Object (readonly)
Returns the value of attribute response_command.
16 17 18 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 16 def response_command @response_command end |
Instance Method Details
#[](*indexes) ⇒ Object
48 49 50 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 48 def [](*indexes) get_index(*indexes).execute end |
#[]=(*indexes_and_value) ⇒ Object
52 53 54 55 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 52 def []=(*indexes_and_value) value = indexes_and_value.pop set_index(indexes_and_value, value).execute end |
#as_dto(act_as_dto = true) ⇒ InvocationContext
Returns a new InvocationContext that acts as a DTO (Data Transfer Object). When act_as_dto is true, field get/set operate on local state without remote calls.
334 335 336 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 334 def as_dto(act_as_dto = true) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, @current_command, false, act_as_dto) end |
#build_command(command) ⇒ Object
358 359 360 361 362 363 364 365 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 358 def build_command(command) return @current_command if command.nil? || command.payload.nil? command.payload.each_index do |i| command.payload[i] = encapsulate_payload_item(command.payload[i]) end command.prepend_arg_to_payload(@current_command) end |
#convert_response_to_dto(response_command) ⇒ InvocationContext
Converts a ProjectResultAsDto response into an InvocationContext DTO.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 92 def convert_response_to_dto(response_command) instance_reference_command = response_command.payload[0] get_type_command = response_command.payload[1] project_result_as_dto_inv_ctx = InvocationContext.new( @runtime_name, @connection_type, @tcp_ip_address, Command.new(@runtime_name, CommandType::CREATE_CLASS_INSTANCE, [get_type_command]), true, true ) if response_command.payload.length >= 3 (2...response_command.payload.length).each do |i| item = response_command.payload[i] next unless item.is_a?(Command) && item.command_type == CommandType::DTO_PROPERTY property_name = item.payload[0] ? item.payload[0].to_s : '' property_value = item.payload.length > 1 ? item.payload[1] : '' project_result_as_dto_inv_ctx = project_result_as_dto_inv_ctx.set_instance_field( property_name, property_value ) end end project_result_as_dto_inv_ctx end |
#create_instance(*args) ⇒ InvocationContext
Creates a new instance of a class in the target runtime. Invokes a constructor on the target runtime and registers the returned context for updates. When context is DTO, skips register_for_update.
142 143 144 145 146 147 148 149 150 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 142 def create_instance(*args) local_command = Command.new(@runtime_name, CommandType::CREATE_CLASS_INSTANCE, [*args]) create_instance_inv_ctx = InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command), @is_executed, @is_dto) return create_instance_inv_ctx if @is_dto # create_instance_inv_ctx.register_for_update create_instance_inv_ctx end |
#create_null ⇒ InvocationContext
Creates a null object of a specific type in the target runtime.
285 286 287 288 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 285 def create_null local_command = Command.new(@runtime_name, CommandType::CREATE_NULL, []) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#encapsulate_payload_item(payload_item) ⇒ Object
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 367 def encapsulate_payload_item(payload_item) if payload_item.is_a? Command payload_item.payload.each_index do |i| payload_item.payload[i] = encapsulate_payload_item(payload_item.payload[i]) end payload_item elsif payload_item.is_a? InvocationContext payload_item.current_command elsif payload_item.is_a? Array copied_array = payload_item.map { |item| encapsulate_payload_item(item) } Command.new(@runtime_name, CommandType::ARRAY, copied_array) elsif TypesHandler.primitive_or_none?(payload_item) Command.new(@runtime_name, CommandType::VALUE, payload_item.nil? ? [nil] : [*payload_item]) else raise TypeError, "Unsupported payload item type: #{payload_item.class} for payload item: #{payload_item}." end end |
#execute ⇒ InvocationContext
Executes the current command. Because invocation context is building the intent of executing particular expression on target environment, we call the initial state of invocation context as non-materialized. The non-materialized context wraps either single command or chain of recursively nested commands. Commands are becoming nested through each invocation of methods on Invocation Context. Each invocation triggers the creation of new Invocation Context instance wrapping the current command with new parent command valid for invoked method. Developer can decide on any moment of the materialization for the context taking full control of the chunks of the expression being transferred and processed on target runtime.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 65 def execute return self if @is_dto @response_command = Interpreter.execute(@current_command, @connection_type, @tcp_ip_address) if @response_command.command_type == CommandType::EXCEPTION exception = ExceptionThrower.throw_exception(@response_command) SdkMessageHelper.('SdkException', exception.) raise(exception) end if @current_command.command_type == CommandType::PROJECT_RESULT_AS_DTO return convert_response_to_dto(@response_command) end InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, @response_command, true, @is_dto) end |
#execute_async ⇒ Object
83 84 85 86 87 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 83 def execute_async Thread.new do execute end end |
#get_enum_name ⇒ InvocationContext
Retrieves the name of an enum from the target runtime.
261 262 263 264 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 261 def get_enum_name local_command = Command.new(@runtime_name, CommandType::GET_ENUM_NAME, []) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#get_enum_value ⇒ InvocationContext
Retrieves the value of an enum from the target runtime.
269 270 271 272 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 269 def get_enum_value local_command = Command.new(@runtime_name, CommandType::GET_ENUM_VALUE, []) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#get_index(*indexes) ⇒ InvocationContext
Retrieves the value at a specific indexes in an array from the target runtime.
207 208 209 210 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 207 def get_index(*indexes) local_command = Command.new(@runtime_name, CommandType::ARRAY_GET_ITEM, [*indexes]) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#get_instance_field(field_name) ⇒ InvocationContext
Retrieves the value of an instance field from the target runtime. When context is DTO, reads from local DTO state via DtoHelper.
176 177 178 179 180 181 182 183 184 185 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 176 def get_instance_field(field_name) if @is_dto value = DtoHelper.try_get_dto_field_value(@current_command, field_name) value = nil if value.nil? value_command = Command.new(@runtime_name, CommandType::VALUE, [value]) return InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, value_command, false, @is_dto) end local_command = Command.new(@runtime_name, CommandType::GET_INSTANCE_FIELD, [field_name]) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#get_instance_method_as_delegate(method_name, *args) ⇒ InvocationContext
Retrieves an instance method as a delegate from the target runtime. # @see Refer to this article on RuntimeBridge Guides
305 306 307 308 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 305 def get_instance_method_as_delegate(method_name, *args) local_command = Command.new(@runtime_name, CommandType::GET_INSTANCE_METHOD_AS_DELEGATE, [method_name, *args]) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#get_rank ⇒ InvocationContext
Retrieves the rank of an array from the target runtime.
233 234 235 236 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 233 def get_rank local_command = Command.new(@runtime_name, CommandType::ARRAY_GET_RANK, []) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#get_ref_value ⇒ InvocationContext
Retrieves the value of a reference from the target runtime.
277 278 279 280 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 277 def get_ref_value local_command = Command.new(@runtime_name, CommandType::GET_REF_VALUE, []) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#get_size ⇒ InvocationContext
Retrieves the number of elements from the target runtime.
225 226 227 228 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 225 def get_size local_command = Command.new(@runtime_name, CommandType::ARRAY_GET_SIZE, []) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#get_static_field(field_name) ⇒ InvocationContext
Retrieves the value of a static field from the target runtime.
156 157 158 159 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 156 def get_static_field(field_name) local_command = Command.new(@runtime_name, CommandType::GET_STATIC_FIELD, [field_name]) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#get_static_method_as_delegate(method_name, *args) ⇒ InvocationContext
Retrieves a static method as a delegate from the target runtime.
295 296 297 298 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 295 def get_static_method_as_delegate(method_name, *args) local_command = Command.new(@runtime_name, CommandType::GET_STATIC_METHOD_AS_DELEGATE, [method_name, *args]) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#get_value ⇒ Object
Retrieves the value of the current command from the target runtime.
326 327 328 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 326 def get_value @current_command.payload[0] end |
#invoke_generic_method(method_name, *args) ⇒ InvocationContext
Invokes a generic method on the target runtime.
253 254 255 256 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 253 def invoke_generic_method(method_name, *args) local_command = Command.new(@runtime_name, CommandType::INVOKE_GENERIC_METHOD, [method_name, *args]) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#invoke_generic_static_method(method_name, *args) ⇒ InvocationContext
Invokes a generic static method on the target runtime.
243 244 245 246 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 243 def invoke_generic_static_method(method_name, *args) local_command = Command.new(@runtime_name, CommandType::INVOKE_GENERIC_STATIC_METHOD, [method_name, *args]) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#invoke_instance_method(method_name, *args) ⇒ InvocationContext
Invokes an instance method on the target runtime.
131 132 133 134 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 131 def invoke_instance_method(method_name, *args) local_command = Command.new(@runtime_name, CommandType::INVOKE_INSTANCE_METHOD, [method_name, *args]) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#invoke_static_method(method_name, *args) ⇒ InvocationContext
Invokes a static method on the target runtime.
120 121 122 123 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 120 def invoke_static_method(method_name, *args) local_command = Command.new(@runtime_name, CommandType::INVOKE_STATIC_METHOD, [method_name, *args]) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#iterator ⇒ Object
def finalize(command, is_executed, interpreter, connection_type, tcp_ip_address)
proc do
if command.command_type == CommandType::REFERENCE && is_executed == true
destruct_command = Command.new(@runtime_name, CommandType::DESTRUCT_REFERENCE, command.payload)
interpreter.execute(destruct_command, connection_type, tcp_ip_address)
end
end
end
39 40 41 42 43 44 45 46 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 39 def iterator if ![CommandType::REFERENCE, CommandType::ARRAY_GET_ITEM, CommandType::ARRAY_SET_ITEM].include? @current_command.command_type raise TypeError, "Object is not iterable for command type #{@current_command.command_type}" else InvocationContextIterator.new(self) end end |
#project_result_as_dto(*properties_names) ⇒ InvocationContext
Projects the result as a DTO with the specified property names. When properties_names is a single empty string or nil, returns this context unchanged.
342 343 344 345 346 347 348 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 342 def project_result_as_dto(*properties_names) return self if properties_names.length == 1 && (properties_names[0].nil? || properties_names[0] == '') payload = properties_names local_command = Command.new(@runtime_name, CommandType::PROJECT_RESULT_AS_DTO, payload) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command), false, false) end |
#register_for_update ⇒ InvocationContext
Registers the current context for updates by wrapping it in a REGISTER_FOR_UPDATE command.
352 353 354 355 356 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 352 def register_for_update local_command = Command.new(@runtime_name, CommandType::REGISTER_FOR_UPDATE, []) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command), @is_executed, @is_dto) end |
#retrieve_array ⇒ Object
Retrieves an array from the target runtime.
313 314 315 316 317 318 319 320 321 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 313 def retrieve_array local_command = Command.new(@runtime_name, CommandType::RETRIEVE_ARRAY, []) local_inv_ctx = InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) array_inv_ctx = local_inv_ctx.execute response_array = [] response_array = array_inv_ctx.current_command.payload if array_inv_ctx.current_command.payload.length > 0 response_array end |
#set_index(indexes, value) ⇒ InvocationContext
Sets the value at a specific index in an array in the target runtime.
217 218 219 220 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 217 def set_index(indexes, value) local_command = Command.new(@runtime_name, CommandType::ARRAY_SET_ITEM, [indexes, value]) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#set_instance_field(field_name, value) ⇒ InvocationContext
Sets the value of an instance field in the target runtime. When context is DTO, updates local DTO state and returns self.
193 194 195 196 197 198 199 200 201 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 193 def set_instance_field(field_name, value) local_command = Command.new(@runtime_name, CommandType::SET_INSTANCE_FIELD, [field_name, value]) new_command = build_command(local_command) if @is_dto @current_command = new_command return self end InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, new_command, @is_executed, @is_dto) end |
#set_static_field(field_name, value) ⇒ InvocationContext
Sets the value of a static field in the target runtime.
166 167 168 169 |
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 166 def set_static_field(field_name, value) local_command = Command.new(@runtime_name, CommandType::SET_STATIC_FIELD, [field_name, value]) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |