Class: Enumpath::Operator::FilterExpression

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

Overview

Implements JSONPath filter expression operator syntax. See README for syntax and examples

Constant Summary collapse

COMPARISON_OPERATOR_REGEX =
/(==|!=|>=|<=|<=>|>|<|=~|!~)/
LOGICAL_OPERATORS_REGEX =
/(&&)|(\|\|)/
OPERATOR_REGEX =
/^\?\((.*)\)$/

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

Whether the operator matches OPERATOR_REGEX

Parameters:

  • operator (String)

    the the full, normalized operator to test

Returns:

  • (true, false)

    whether the operator param appears to represent the operator class



19
20
21
# File 'lib/enumpath/operator/filter_expression.rb', line 19

def detect?(operator)
  !(operator =~ OPERATOR_REGEX).nil?
end

Instance Method Details

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

Yields to the block once for each member of the enumerable that passes the filter expression

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 member of the enumerable that passed the filter

  • resolved_path (Array)

    resolved_path plus the key for each member of the enumerable that passed the filter



32
33
34
35
36
37
# File 'lib/enumpath/operator/filter_expression.rb', line 32

def apply(remaining_path, enum, resolved_path, &block)
  Enumpath.log('Evaluating filter expression') { { expression: operator, to: enum } }
  _match, unpacked_operator = OPERATOR_REGEX.match(operator).to_a
  expressions = unpacked_operator.split(LOGICAL_OPERATORS_REGEX).map(&:strip)
  keys(enum).each { |key| apply_to_key(key, expressions, remaining_path, enum, resolved_path, &block) }
end