Module: Wisco::PathUtils

Defined in:
lib/wisco/path_utils.rb

Constant Summary collapse

VALID_SECTIONS =
%w[actions triggers].freeze

Class Method Summary collapse

Class Method Details

.parse_path(path_arg, connector) ⇒ Object

Returns an array of [section, key] pairs derived from path_arg.

Accepted forms:

"section.key"  — one specific key in a known section
"section"      — all keys in that section
"key"          — auto-detect section; error if found in both or neither


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/wisco/path_utils.rb', line 13

def parse_path(path_arg, connector)
  parts = path_arg.split('.', 2)

  if parts.size == 2
    section, key = parts
    unless VALID_SECTIONS.include?(section)
      warn "Error: Invalid section '#{section}'. Valid sections: #{VALID_SECTIONS.join(', ')}."
      exit 1
    end
    items = connector[section.to_sym]
    unless items&.key?(key.to_sym)
      warn "Error: '#{key}' not found in #{section}."
      exit 1
    end
    [[section, key]]

  elsif VALID_SECTIONS.include?(path_arg)
    section = path_arg
    items = connector[section.to_sym]
    if items.nil? || items.empty?
      warn "Error: No keys found in #{section}."
      exit 1
    end
    items.keys.map { |k| [section, k.to_s] }

  else
    key = path_arg
    in_actions  = connector[:actions]&.key?(key.to_sym)  || false
    in_triggers = connector[:triggers]&.key?(key.to_sym) || false

    case [in_actions, in_triggers]
    when [true, false]  then [['actions',  key]]
    when [false, true]  then [['triggers', key]]
    when [true, true]
      warn "Error: '#{key}' exists in both actions and triggers."
      warn "       Qualify with section, e.g. 'actions.#{key}'."
      exit 1
    else
      warn "Error: '#{key}' not found in actions or triggers."
      exit 1
    end
  end
end