Module: RSMP::Validator::ConfigNormalizer

Defined in:
lib/rsmp/validator/config_normalizer.rb

Overview

Normalizes configuration settings before passing them to RSMP nodes.

Class Method Summary collapse

Class Method Details

.build_programming_array(normalized) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/rsmp/validator/config_normalizer.rb', line 53

def self.build_programming_array(normalized)
  max_key = normalized.keys.max
  program_array = Array.new(max_key + 1)
  normalized.each do |index, value|
    program_array[index] = value
  end
  program_array
end

.deep_dup(value) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/rsmp/validator/config_normalizer.rb', line 91

def self.deep_dup(value)
  case value
  when Hash
    value.transform_values { |v| deep_dup(v) }
  when Array
    value.map { |item| deep_dup(item) }
  else
    value
  end
end

.normalize_input_programming!(settings) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/rsmp/validator/config_normalizer.rb', line 35

def self.normalize_input_programming!(settings)
  programming = settings.dig('inputs', 'programming')
  return unless programming.is_a?(Hash)

  normalized = normalize_programming_keys(programming)
  return unless normalized.keys.all?(Integer)

  settings['inputs'] ||= {}
  settings['inputs']['programming'] = build_programming_array(normalized)
end

.normalize_programming_keys(programming) ⇒ Object



46
47
48
49
50
51
# File 'lib/rsmp/validator/config_normalizer.rb', line 46

def self.normalize_programming_keys(programming)
  programming.each_with_object({}) do |(key, value), memo|
    int_key = key.is_a?(String) && key.match?(/^\d+$/) ? key.to_i : key
    memo[int_key] = value
  end
end

.normalize_security_codes!(settings) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/rsmp/validator/config_normalizer.rb', line 25

def self.normalize_security_codes!(settings)
  codes = settings['security_codes']
  return unless codes.is_a?(Hash)

  settings['security_codes'] = codes.each_with_object({}) do |(key, value), memo|
    int_key = key.is_a?(String) && key.match?(/^\d+$/) ? key.to_i : key
    memo[int_key] = value
  end
end

.normalize_site_settings(settings) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rsmp/validator/config_normalizer.rb', line 5

def self.normalize_site_settings(settings)
  normalized = deep_dup(settings)

  if normalized['security_codes'].nil? && normalized.dig('secrets', 'security_codes').is_a?(Hash)
    normalized['security_codes'] = deep_dup(normalized['secrets']['security_codes'])
  end

  normalize_security_codes!(normalized)
  normalize_input_programming!(normalized)
  normalize_sxls_for_config!(normalized)

  normalized
end

.normalize_supervisor_settings(settings) ⇒ Object



19
20
21
22
23
# File 'lib/rsmp/validator/config_normalizer.rb', line 19

def self.normalize_supervisor_settings(settings)
  normalized = deep_dup(settings)
  normalize_sxls_for_config!(normalized)
  normalized
end

.normalize_sxls_for_config!(value) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rsmp/validator/config_normalizer.rb', line 62

def self.normalize_sxls_for_config!(value)
  case value
  when Hash
    value.each do |key, child|
      value[key] = sxls_array_to_hash(child) if key == 'sxls'
      normalize_sxls_for_config!(value[key])
    end
  when Array
    value.each { |item| normalize_sxls_for_config!(item) }
  end
end

.sxls_array_to_hash(value) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rsmp/validator/config_normalizer.rb', line 74

def self.sxls_array_to_hash(value)
  return value unless value.is_a?(Array)

  value.each_with_object({}) do |item, memo|
    next unless item.is_a?(Hash)

    name = item['name']
    next unless name

    sxl = item.dup
    sxl.delete('name')
    version = sxl.delete('version')
    sxl.delete('prefix')
    memo[name] = sxl.empty? ? version : sxl.merge('version' => version)
  end
end