Class: Enumpath::Operator::FilterExpression
- 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
Class Method Summary collapse
-
.detect?(operator) ⇒ true, false
Whether the operator matches OPERATOR_REGEX.
Instance Method Summary collapse
-
#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.
Methods inherited from Base
Constructor Details
This class inherits a constructor from Enumpath::Operator::Base
Class Method Details
.detect?(operator) ⇒ true, false
Whether the operator matches OPERATOR_REGEX
19 20 21 |
# File 'lib/enumpath/operator/filter_expression.rb', line 19 def detect?(operator) !!(operator =~ OPERATOR_REGEX) 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
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# 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 do |key| value = Enumpath::Resolver::Simple.resolve(key, enum) Enumpath.log('Applying filter to key') { { key: key, enum: value } } if pass?(expressions.dup, value) Enumpath.log('Applying filtered key') { { 'filtered key': key, 'filtered enum': value } } yield(remaining_path, value, resolved_path + [key.to_s]) end end end |