Class: RSMP::SXL::Processing::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/rsmp/sxl/processing/document.rb

Overview

A validated SXL YAML document with a normalized component structure.

Defined Under Namespace

Classes: SymbolDefinition

Constant Summary collapse

NAME =
%r{\A[a-z0-9][a-z0-9_/-]*\z}
PREFIX =
%r{\A[a-zA-Z0-9_/-]+/\z}
MESSAGE_KINDS =
%w[alarms statuses commands].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, data:) ⇒ Document



40
41
42
43
44
45
46
47
48
# File 'lib/rsmp/sxl/processing/document.rb', line 40

def initialize(path:, data:)
  @path = path
  @data = normalize_document(require_hash(data, 'document'))
  
  read_dependencies
  read_symbols
rescue Error => e
  raise Error, "#{path}: #{e.message}"
end

Instance Attribute Details

#component_typesObject (readonly)

Returns the value of attribute component_types.



14
15
16
# File 'lib/rsmp/sxl/processing/document.rb', line 14

def component_types
  @component_types
end

#dataObject (readonly)

Returns the value of attribute data.



14
15
16
# File 'lib/rsmp/sxl/processing/document.rb', line 14

def data
  @data
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



14
15
16
# File 'lib/rsmp/sxl/processing/document.rb', line 14

def dependencies
  @dependencies
end

#message_codesObject (readonly)

Returns the value of attribute message_codes.



14
15
16
# File 'lib/rsmp/sxl/processing/document.rb', line 14

def message_codes
  @message_codes
end

#minimum_core_versionObject (readonly)

Returns the value of attribute minimum_core_version.



14
15
16
# File 'lib/rsmp/sxl/processing/document.rb', line 14

def minimum_core_version
  @minimum_core_version
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/rsmp/sxl/processing/document.rb', line 14

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



14
15
16
# File 'lib/rsmp/sxl/processing/document.rb', line 14

def path
  @path
end

#versionObject (readonly)

Returns the value of attribute version.



14
15
16
# File 'lib/rsmp/sxl/processing/document.rb', line 14

def version
  @version
end

#version_stringObject (readonly)

Returns the value of attribute version_string.



14
15
16
# File 'lib/rsmp/sxl/processing/document.rb', line 14

def version_string
  @version_string
end

Class Method Details

.load(path) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/rsmp/sxl/processing/document.rb', line 17

def self.load(path)
  expanded_path = File.expand_path(path)
  raise Error, "SXL source #{path} not found" unless File.exist?(expanded_path)
  raise Error, "SXL source #{path} is not a file" unless File.file?(expanded_path)

  parse(File.read(expanded_path, encoding: 'UTF-8'), path: expanded_path)
rescue EncodingError => e
  raise Error, "Cannot read SXL source #{path}: #{e.message}"
end

.parse(source, path: '(SXL source)') ⇒ Object



27
28
29
30
31
32
# File 'lib/rsmp/sxl/processing/document.rb', line 27

def self.parse(source, path: '(SXL source)')
  data = Psych.safe_load(source, permitted_classes: [], permitted_symbols: [], aliases: false)
  new(path: path, data: data)
rescue Psych::Exception => e
  raise Error, "Cannot read SXL source #{path}: #{e.message}"
end

.validate_name!(value, label: 'SXL name') ⇒ Object

Raises:



34
35
36
37
38
# File 'lib/rsmp/sxl/processing/document.rb', line 34

def self.validate_name!(value, label: 'SXL name')
  return value if value.is_a?(String) && NAME.match?(value)

  raise Error, "Invalid #{label} #{value.inspect}"
end