Class: Janeway::AST::ArraySliceSelector

Inherits:
Selector show all
Defined in:
lib/janeway/ast/array_slice_selector.rb

Overview

An array slice selects a series of elements from an array.

It accepts a start and end positions, and a step value that define the range to select. All of these are optional.

ArraySliceSelector needs to store "default" arguments differently from "explicit" arguments, since they're interpreted differently.

Examples:

$[1:3]
$[5:]
$[1:5:2]
$[5:1:-2]
$[::-1]
$[:]

Instance Attribute Summary collapse

Attributes inherited from Expression

#next, #value

Instance Method Summary collapse

Methods inherited from Expression

#chain_of?, #indented, #literal?, #singular_query?, #type, type_name

Constructor Details

#initialize(start = nil, end_ = nil, step = nil) ⇒ ArraySliceSelector

Returns a new instance of ArraySliceSelector.

Parameters:

  • start (Integer, nil) (defaults to: nil)
  • end_ (Integer, nil) (defaults to: nil)
  • step (Integer, nil) (defaults to: nil)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/janeway/ast/array_slice_selector.rb', line 33

def initialize(start = nil, end_ = nil, step = nil)
  super(nil)

  # Check arguments
  [start, end_, step].each do |arg|
    unless [NilClass, Integer].include?(arg.class)
      raise Error, "Array slice selector index must be integer or nothing, got #{arg.inspect}"
    end
    next unless arg

    # Check integer size limits
    raise Error, "Array slice selector value too small: #{arg.inspect}" if arg < INTEGER_MIN
    raise Error, "Array slice selector value too large: #{arg.inspect}" if arg > INTEGER_MAX
  end

  # Nil values are kept to indicate that the default value should be used.
  # The interpreter selects the actual values.
  @start = start
  @end = end_
  @step = step
end

Instance Attribute Details

#endObject (readonly)

Raw start / end / step values from the query source. Any of them may be nil, meaning "use the default for the current step direction" — the interpreter resolves that.



28
29
30
# File 'lib/janeway/ast/array_slice_selector.rb', line 28

def end
  @end
end

#startObject (readonly)

Raw start / end / step values from the query source. Any of them may be nil, meaning "use the default for the current step direction" — the interpreter resolves that.



28
29
30
# File 'lib/janeway/ast/array_slice_selector.rb', line 28

def start
  @start
end

#stepObject (readonly)

Raw start / end / step values from the query source. Any of them may be nil, meaning "use the default for the current step direction" — the interpreter resolves that.



28
29
30
# File 'lib/janeway/ast/array_slice_selector.rb', line 28

def step
  @step
end

Instance Method Details

#to_s(brackets: true, **_ignored) ⇒ String

Parameters:

  • brackets (Boolean) (defaults to: true)

    add surrounding brackets if true

Returns:

  • (String)


57
58
59
60
61
62
63
64
65
# File 'lib/janeway/ast/array_slice_selector.rb', line 57

def to_s(brackets: true, **_ignored)
  index_str =
    if @step
      "#{@start}:#{@end}:#{@step}"
    else
      "#{@start}:#{@end}"
    end
  brackets ? "[#{index_str}]" : index_str
end

#tree(level) ⇒ Array

Parameters:

  • level (Integer)

Returns:

  • (Array)


69
70
71
# File 'lib/janeway/ast/array_slice_selector.rb', line 69

def tree(level)
  [indented(level, to_s)]
end