class Enumpath::Operator::Slice
Implements JSONPath array slice operator syntax. See {file:README.md#label-Slice+operator} for syntax and examples
Constants
- OPERATOR_REGEX
Public Class Methods
Whether the operator matches {Enumpath::Operator::Slice::OPERATOR_REGEX}
@param operator (see Enumpath::Operator::Base.detect?
) @return (see Enumpath::Operator::Base.detect?
)
# File lib/enumpath/operator/slice.rb, line 14 def detect?(operator) !(operator =~ OPERATOR_REGEX).nil? end
Public Instance Methods
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.
@param (see Enumpath::Operator::Base#apply
) @yield (see Enumpath::Operator::Base#apply
) @yieldparam remaining_path [Array] the included index plus remaining_path @yieldparam enum [Enumerable] enum @yieldparam resolved_path [Array] resolved_path
# 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
Private Instance Methods
# File lib/enumpath/operator/slice.rb, line 51 def slice_length(length, max_length) length = length.empty? ? max_length : length.to_i length.negative? ? [0, length + max_length].max : [max_length, length].min end
# File lib/enumpath/operator/slice.rb, line 46 def slice_start(start, max_length) start = start.empty? ? 0 : start.to_i start.negative? ? [0, start + max_length].max : [max_length, start].min end
# File lib/enumpath/operator/slice.rb, line 56 def slice_step(step) step.empty? ? 1 : step.to_i end
# File lib/enumpath/operator/slice.rb, line 39 def slices(start, length, step, max_length) start = slice_start(start, max_length) length = slice_length(length, max_length) step = slice_step(step) (start...length).step(step) end