Class: Command
- Inherits:
-
Object
- Object
- Command
- Defined in:
- lib/hypertube-ruby-sdk/utils/command.rb
Instance Attribute Summary collapse
-
#command_type ⇒ Object
readonly
Returns the value of attribute command_type.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#runtime_name ⇒ Object
readonly
Returns the value of attribute runtime_name.
Class Method Summary collapse
Instance Method Summary collapse
- #add_arg_to_payload(argument) ⇒ Object
- #drop_first_payload_argument ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
-
#initialize(runtime_name, command_type, *payload) ⇒ Command
constructor
A new instance of Command.
- #prepend_arg_to_payload(arg_command) ⇒ Object
- #to_s ⇒ Object
- #to_string ⇒ Object
Constructor Details
#initialize(runtime_name, command_type, *payload) ⇒ Command
Returns a new instance of Command.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/hypertube-ruby-sdk/utils/command.rb', line 11 def initialize(runtime_name, command_type, *payload) @runtime_name = runtime_name @command_type = command_type @payload = if payload.empty? [] elsif payload.size == 1 && payload[0].is_a?(Array) # caller passed a single array -> reuse it (no extra copy) payload[0] || [] else # varargs or multiple args -> use as provided payload end end |
Instance Attribute Details
#command_type ⇒ Object (readonly)
Returns the value of attribute command_type.
9 10 11 |
# File 'lib/hypertube-ruby-sdk/utils/command.rb', line 9 def command_type @command_type end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
9 10 11 |
# File 'lib/hypertube-ruby-sdk/utils/command.rb', line 9 def payload @payload end |
#runtime_name ⇒ Object (readonly)
Returns the value of attribute runtime_name.
9 10 11 |
# File 'lib/hypertube-ruby-sdk/utils/command.rb', line 9 def runtime_name @runtime_name end |
Class Method Details
.create_reference(guid, runtime_name) ⇒ Object
30 31 32 |
# File 'lib/hypertube-ruby-sdk/utils/command.rb', line 30 def self.create_reference(guid, runtime_name) Command.new(runtime_name, CommandType::REFERENCE, guid) end |
.create_response(response, runtime_name) ⇒ Object
26 27 28 |
# File 'lib/hypertube-ruby-sdk/utils/command.rb', line 26 def self.create_response(response, runtime_name) Command.new(runtime_name, CommandType::VALUE, response) end |
Instance Method Details
#add_arg_to_payload(argument) ⇒ Object
41 42 43 44 |
# File 'lib/hypertube-ruby-sdk/utils/command.rb', line 41 def add_arg_to_payload(argument) new_payload = @payload + [argument] Command.new(@runtime_name, @command_type, new_payload) end |
#drop_first_payload_argument ⇒ Object
34 35 36 37 38 39 |
# File 'lib/hypertube-ruby-sdk/utils/command.rb', line 34 def drop_first_payload_argument return Command.new(@runtime_name, @command_type, []) if @payload.length <= 1 new_payload = @payload[1..-1] || [] Command.new(@runtime_name, @command_type, new_payload) end |
#eql?(other) ⇒ Boolean Also known as: ==
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/hypertube-ruby-sdk/utils/command.rb', line 95 def eql?(other) return true if equal?(other) return false unless other.is_a?(Command) return false unless runtime_name == other.runtime_name && command_type == other.command_type return false unless payload.length == other.payload.length payload.each_with_index do |item, i| return false unless item.eql?(other.payload[i]) end true end |
#prepend_arg_to_payload(arg_command) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/hypertube-ruby-sdk/utils/command.rb', line 46 def prepend_arg_to_payload(arg_command) return self if arg_command.nil? new_payload = [arg_command] + @payload Command.new(@runtime_name, @command_type, new_payload) end |
#to_s ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/hypertube-ruby-sdk/utils/command.rb', line 53 def to_s result = ' { ' result += "RuntimeName: #{@runtime_name}, " result += "CommandType: #{@command_type}, " result += "Payload: \n" result += ' [' payload = @payload len = payload.length if len > 0 result += "\n" len.times do |i| item = payload[i] item_str = if item.nil? 'null' elsif item.is_a?(String) " \"#{item}\"" elsif item.is_a?(Command) item.to_string.gsub("\n", "\n ") else " #{item}" end result += " #{item_str}" result += i < len - 1 ? ",\n" : "\n" end result += " ]\n" else result += " ]\n" end result += ' }' result rescue StandardError => e "Error while converting command to string:#{e.}" end |
#to_string ⇒ Object
91 92 93 |
# File 'lib/hypertube-ruby-sdk/utils/command.rb', line 91 def to_string to_s end |