Class: Enumpath::Operator::Child

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

Overview

Implements JSONPath child operator syntax. See README for syntax and examples.

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, enum) ⇒ true, false

Checks to see if an operator is valid as a child operator. It is considered valid if the enumerable contains an index, key, member, or property that responds to child.

Parameters:

  • operator (String) —

    the the full, normalized operator to test

Returns:

  • (true, false) —

    whether the operator param appears to represent the operator class



13
14
15
# File 'lib/enumpath/operator/child.rb', line 13

def detect?(operator, enum)
  !resolve(operator, enum).nil?
end

Instance Method Details

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

Resolves a child operator against an enumerable. If the child operator matches a index, key, member, or property of the enumerable it is yielded to the block.

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

  • enum (Enumerable) —

    the resolved value of the enumerable

  • resolved_path (Array) —

    resolved_path plus the child operator



63
64
65
66
# File 'lib/enumpath/operator/child.rb', line 63

def apply(remaining_path, enum, resolved_path)
  value = detected_value(enum)
  yield(remaining_path, value, resolved_path + [operator]) unless value.nil?
end

#detect?(enum) ⇒ true, false

Whether the operator resolves against an enumerable. The resolved value is kept, so a following #apply to the same enumerable uses it instead of resolving the operator a second time. That matters when a property resolver policy lets the operator call a method, which must run once per path segment.

Parameters:

  • enum (Enumerable) —

    the enumerable to resolve the operator against

Returns:

  • (true, false) —

    whether the operator resolved to a value



50
51
52
53
# File 'lib/enumpath/operator/child.rb', line 50

def detect?(enum)
  keep(enum, self.class.resolve(operator, enum))
  !@detected_value.nil?
end