Class: Enumpath::Operator::Slice

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

Overview

Implements JSONPath array slice operator syntax. See README for syntax and examples

Constant Summary collapse

OPERATOR_REGEX =
/^(-?[0-9]*):(-?[0-9]*):?(-?[0-9]*)$/

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



14
15
16
# File 'lib/enumpath/operator/slice.rb', line 14

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 local enumerable whose index is included by position between start and up to (but not including) end. If step is included then only every step member is included, starting with the first.

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)

    the included index plus remaining_path

  • enum (Enumerable)

    enum

  • resolved_path (Array)

    resolved_path



28
29
30
31
32
33
34
35
# File 'lib/enumpath/operator/slice.rb', line 28

def apply(remaining_path, enum, resolved_path)
  _match, start, length, step = OPERATOR_REGEX.match(operator).to_a
  max_length = enum.size
  slices(start, length, step, max_length).each do |index|
    Enumpath.log('Applying slice') { { slice: index } }
    yield([index.to_s] + remaining_path, enum, resolved_path)
  end
end