Class: JMESPath::Nodes::Slice Private

Inherits:
Node
  • Object
show all
Defined in:
lib/jmespath/nodes/slice.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Direct Known Subclasses

SimpleSlice

Instance Method Summary collapse

Methods inherited from Node

#chains_with?

Constructor Details

#initialize(start, stop, step) ⇒ Slice

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Slice.



6
7
8
9
10
11
# File 'lib/jmespath/nodes/slice.rb', line 6

def initialize(start, stop, step)
  @start = start
  @stop = stop
  @step = step
  raise Errors::InvalidValueError, 'slice step cannot be 0' if @step == 0
end

Instance Method Details

#optimizeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



34
35
36
37
38
39
40
# File 'lib/jmespath/nodes/slice.rb', line 34

def optimize
  if (@step.nil? || @step == 1) && @start && @stop && @start > 0 && @stop > @start
    SimpleSlice.new(@start, @stop)
  else
    self
  end
end

#visit(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jmespath/nodes/slice.rb', line 13

def visit(value)
  if (value = value.respond_to?(:to_str) ? value.to_str : value.respond_to?(:to_ary) ? value.to_ary : nil)
    start, stop, step = adjust_slice(value.size, @start, @stop, @step)
    result = []
    if step > 0
      i = start
      while i < stop
        result << value[i]
        i += step
      end
    else
      i = start
      while i > stop
        result << value[i]
        i += step
      end
    end
    value.respond_to?(:to_str) ? result.join : result
  end
end