Class: Command

Inherits:
Object
  • Object
show all
Defined in:
lib/javonet-ruby-sdk/utils/command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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/javonet-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_typeObject (readonly)

Returns the value of attribute command_type.



9
10
11
# File 'lib/javonet-ruby-sdk/utils/command.rb', line 9

def command_type
  @command_type
end

#payloadObject (readonly)

Returns the value of attribute payload.



9
10
11
# File 'lib/javonet-ruby-sdk/utils/command.rb', line 9

def payload
  @payload
end

#runtime_nameObject (readonly)

Returns the value of attribute runtime_name.



9
10
11
# File 'lib/javonet-ruby-sdk/utils/command.rb', line 9

def runtime_name
  @runtime_name
end

Class Method Details

.create_array_response(array, runtime_name) ⇒ Object



34
35
36
# File 'lib/javonet-ruby-sdk/utils/command.rb', line 34

def self.create_array_response(array, runtime_name)
  Command.new(runtime_name, CommandType::ARRAY, array)
end

.create_reference(guid, runtime_name) ⇒ Object



30
31
32
# File 'lib/javonet-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/javonet-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



45
46
47
48
# File 'lib/javonet-ruby-sdk/utils/command.rb', line 45

def add_arg_to_payload(argument)
  new_payload = @payload + [argument]
  Command.new(@runtime_name, @command_type, new_payload)
end

#drop_first_payload_argumentObject



38
39
40
41
42
43
# File 'lib/javonet-ruby-sdk/utils/command.rb', line 38

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: ==

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/javonet-ruby-sdk/utils/command.rb', line 99

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



50
51
52
53
54
55
# File 'lib/javonet-ruby-sdk/utils/command.rb', line 50

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_sObject



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
90
91
92
93
# File 'lib/javonet-ruby-sdk/utils/command.rb', line 57

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.message}"
end

#to_stringObject



95
96
97
# File 'lib/javonet-ruby-sdk/utils/command.rb', line 95

def to_string
  to_s
end