Module: Lite::Validation::Validator::Helpers::Path

Defined in:
lib/lite/validation/validator/helpers/path.rb

Defined Under Namespace

Classes: InvalidPath

Class Method Summary collapse

Class Method Details

.expand_element(element, base_path) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/lite/validation/validator/helpers/path.rb', line 35

def self.expand_element(element, base_path)
  case element
  when ::Array
    raise InvalidPath, "Empty path in a tuple: #{base_path.join(', ')}" if element.empty?

    expand_path(element, base_path)
  else
    [[*base_path, element]]
  end
end

.expand_path(path, base_path) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/lite/validation/validator/helpers/path.rb', line 12

def self.expand_path(path, base_path)
  return [base_path] if path.empty?

  element, *rest = path

  case element
  when ::Array
    raise InvalidPath, <<~ERR.chomp unless rest.empty?
      Can't follow path into a tuple: #{element} -> #{rest.map(&:inspect).join(', ')}
    ERR

    expand_tuple(element, base_path)
  else
    expand_path(rest, [*base_path, element])
  end
end

.expand_tuple(tuple, base_path) ⇒ Object



29
30
31
32
33
# File 'lib/lite/validation/validator/helpers/path.rb', line 29

def self.expand_tuple(tuple, base_path)
  tuple.flat_map do |element|
    expand_element(element, base_path)
  end
end