Class: VanillaValidator::ValueExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/vanilla_validator/value_extractor.rb

Class Method Summary collapse

Class Method Details

.get(data, path) ⇒ Object

Public: Extracts data from a nested structure using a path.

data - The nested data structure (Hash or Array) to extract data from. path - A string representing the path to the desired data, where individual segments are separated by periods ('.').

Raises:

  • RuntimeError: If the path contains more than 5 segments, which exceeds the maximum allowed.

Returns: The extracted data, or nil if the path does not lead to a valid value.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/vanilla_validator/value_extractor.rb', line 17

def self.get(data, path)
  path_components = path.split('.')

  raise "Too many path segments (maximum allowed is 5)" if path_components.length > 5

  if path.include?("*")
    self.get_at_wildcard_path(data, path_components)
  else
    self.get_at_explicit_path(data, path_components)
  end
end