Class: RSMP::StatusList

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

Overview

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

Compact Hash (symbol or string keys):

{ S0014: [:status, :source] }
{ 'S0014' => ['status', 'source'] }

Raw wire Array (used in RSMP messages):

[{ 'sCI' => 'S0014', 'n' => 'status' }, { 'sCI' => 'S0014', 'n' => 'source' }]

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ StatusList

Returns a new instance of StatusList.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rsmp/status_list.rb', line 13

def initialize(input)
  @list = case input
          when StatusList
            input.to_a
          when Array
            input
          when Hash
            input.flat_map do |code, names|
              names.map { |name| { 'sCI' => code.to_s, 'n' => name.to_s } }
            end
          else
            raise ArgumentError, "StatusList requires an Array, Hash, or StatusList, got #{input.class}"
          end
end

Instance Method Details

#eachObject



28
29
30
# File 'lib/rsmp/status_list.rb', line 28

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

#to_aObject



32
33
34
# File 'lib/rsmp/status_list.rb', line 32

def to_a
  @list
end

#to_hObject



36
37
38
39
40
41
42
# File 'lib/rsmp/status_list.rb', line 36

def to_h
  @list.each_with_object({}) do |item, hash|
    code = item['sCI']
    name = item['n']
    (hash[code] ||= []) << name
  end
end