Class: Enumpath::Operator::SubscriptExpression

Inherits:
Base
  • Object
show all
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 =
/^\((.*)\)$/
MAX_OPERAND_BITS =

The operand of an arithmetic expression comes from the path, so an expression that could use an unbounded amount of CPU or memory is not evaluated and matches nothing. A subscript is an index or a key, so none of these limits get in the way of a useful expression.

The widest integer operand allowed, in bits

64
MAX_REPEAT_SIZE =

The most elements or characters that repeating a string or array (*) may produce

10_000
MAX_POWER_BITS =

The widest result allowed for an integer raised to a power (**), in bits

1_024
EVALUATION_ERRORS =

Errors from evaluating an expression against data that does not fit it, such as a string plus a number or a division by zero. The expression matches nothing in these cases.

[NoMethodError, TypeError, ArgumentError, ZeroDivisionError, RangeError].freeze

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



31
32
33
# File 'lib/enumpath/operator/subscript_expression.rb', line 31

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

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 at the value of the subscript expression

  • resolved_path (Array) —

    resolved_path plus the value of the subscript expression



43
44
45
46
47
48
49
50
51
52
# File 'lib/enumpath/operator/subscript_expression.rb', line 43

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