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
16
# File 'lib/enumpath/operator/child.rb', line 13

def detect?(operator, enum)
  !Enumpath::Resolver::Simple.resolve(operator, enum).nil? ||
    !Enumpath::Resolver::Property.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



27
28
29
30
31
# File 'lib/enumpath/operator/child.rb', line 27

def apply(remaining_path, enum, resolved_path)
  value = Enumpath::Resolver::Simple.resolve(operator, enum)
  value = Enumpath::Resolver::Property.resolve(operator, enum) if value.nil?
  yield(remaining_path, value, resolved_path + [operator]) unless value.nil?
end