Class: RSMP::CommandList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rsmp/command_list.rb

Overview

Represents an RSMP command list and converts between the two common formats:

Compact form (used in validators and helpers):

RSMP::CommandList.new(:M0001, :setValue, securityCode: '1111', status: 'NormalControl')

Raw wire Array (used in RSMP messages):

[
  { 'cCI' => 'M0001', 'cO' => 'setValue', 'n' => 'securityCode', 'v' => '1111' },
  { 'cCI' => 'M0001', 'cO' => 'setValue', 'n' => 'status',       'v' => 'NormalControl' }
]

Instance Method Summary collapse

Constructor Details

#initialize(command_code_id, command_name, values) ⇒ CommandList

Returns a new instance of CommandList.



15
16
17
18
19
20
21
22
23
24
# File 'lib/rsmp/command_list.rb', line 15

def initialize(command_code_id, command_name, values)
  @list = values.compact.map do |n, v|
    {
      'cCI' => command_code_id.to_s,
      'cO' => command_name.to_s,
      'n' => n.to_s,
      'v' => v.to_s
    }
  end
end

Instance Method Details

#eachObject



26
27
28
# File 'lib/rsmp/command_list.rb', line 26

def each(&)
  @list.each(&)
end

#to_aObject



30
31
32
# File 'lib/rsmp/command_list.rb', line 30

def to_a
  @list
end

#to_hObject



34
35
36
37
38
39
40
# File 'lib/rsmp/command_list.rb', line 34

def to_h
  @list.each_with_object({}) do |item, hash|
    code = item['cCI']
    command = item['cO']
    ((hash[code] ||= {})[command] ||= {})[item['n']] = item['v']
  end
end