Class: Enumpath::Operator::RecursiveDescent

Inherits:
Base
  • Object
show all
Defined in:
lib/enumpath/operator/recursive_descent.rb

Overview

Implements JSONPath recursive descent operator syntax. See README for syntax and examples

Constant Summary collapse

OPERATOR =
'..'

Instance Attribute Summary

Attributes inherited from Base

#operator

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Enumpath::Operator::Base

Class Method Details

.detect?(operator) ⇒ true, false

Simple test of whether the operator matches the OPERATOR constant

Parameters:

  • operator (String)

    the the full, normalized operator to test

Returns:

  • (true, false)

    whether the operator param appears to represent the operator class



15
16
17
# File 'lib/enumpath/operator/recursive_descent.rb', line 15

def detect?(operator)
  operator == OPERATOR
end

Instance Method Details

#apply(remaining_path, enum, resolved_path) {|remaining_path, enum, resolved_path| ... } ⇒ Object

Yields to the block once for the enumerable itself, and once for every direct member of the enumerable that is also an enumerable

Parameters:

  • remaining_path (Array)

    an array containing the normalized path segments yet to be resolved

  • enum (Enumerable)

    the object to apply the operator to

  • resolved_path (Array)

    an array containing the static path segments that have been resolved

Yields:

  • A block that will be called if the operator is applied successfully. If the operator cannot or should not be applied then the block is not yielded.

Yield Parameters:

  • remaining_path (Array)

    remaining_path for the enumerable itself, or the recursive descent operator plus remaining_path for each direct enumerable member

  • enum (Enumerable)

    enum for the enumerable itself, or the direct enumerable member for each direct enumerable member

  • resolved_path (Array)

    resolved_path for the enumerable itself, or resolved_path plus the key for each direct enumerable member



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/enumpath/operator/recursive_descent.rb', line 31

def apply(remaining_path, enum, resolved_path)
  Enumpath.log('Applying remaining path recursively to enum') { { 'remaining path': remaining_path } }
  yield(remaining_path, enum, resolved_path)
  keys(enum).each do |key|
    value = Enumpath::Resolver::Simple.resolve(key, enum)
    next unless recursable?(value)

    Enumpath.log('Applying remaining path recursively to key') do
      { key: key, 'remaining path': ['..'] + remaining_path }
    end
    yield(['..'] + remaining_path, value, resolved_path + [key])
  end
end