class JMESPath::Nodes::Slice
Public Class Methods
new(start, stop, step)
click to toggle source
# 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
Public Instance Methods
optimize()
click to toggle source
# 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)
click to toggle source
# 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
Private Instance Methods
adjust_endpoint(length, endpoint, step)
click to toggle source
# File lib/jmespath/nodes/slice.rb, line 64 def adjust_endpoint(length, endpoint, step) if endpoint < 0 endpoint += length endpoint = step < 0 ? -1 : 0 if endpoint < 0 endpoint elsif endpoint >= length step < 0 ? length - 1 : length else endpoint end end
adjust_slice(length, start, stop, step)
click to toggle source
# File lib/jmespath/nodes/slice.rb, line 45 def adjust_slice(length, start, stop, step) if step.nil? step = 1 end if start.nil? start = step < 0 ? length - 1 : 0 else start = adjust_endpoint(length, start, step) end if stop.nil? stop = step < 0 ? -1 : length else stop = adjust_endpoint(length, stop, step) end [start, stop, step] end