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?, #hash_like?

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.



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

def initialize(start, stop, step)
  @start = start
  @stop = stop
  @step = step
  raise Errors::InvalidValueError.new('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.



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

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.



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

def visit(value)
  if String === value || Array === value
    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
    String === value ? result.join : result
  else
    nil
  end
end