Class: Enumpath::Operator::SubscriptExpression
- Defined in:
- lib/enumpath/operator/subscript_expression.rb
Overview
Implements JSONPath subscript expressions operator syntax. See README for syntax and examples
Constant Summary collapse
- ARITHMETIC_OPERATOR_REGEX =
%r{(\+|-|\*\*|\*|\/|%)}
- 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 if the subscript expression evaluates to a member of the enumerable.
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
16 17 18 |
# File 'lib/enumpath/operator/subscript_expression.rb', line 16 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 if the subscript expression evaluates to a member of the enumerable
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/enumpath/operator/subscript_expression.rb', line 28 def apply(remaining_path, enum, resolved_path) Enumpath.log('Applying subscript expression') { { expression: operator, to: enum } } _match, unpacked_operator = OPERATOR_REGEX.match(operator).to_a result = evaluate(unpacked_operator, enum) value = Enumpath::Resolver::Simple.resolve(result, enum) Enumpath.log('Applying subscript') { { 'enum at subscript': value } } unless value.nil? yield(remaining_path, value, resolved_path + [result.to_s]) unless value.nil? end |