Class: Janeway::AST::ArraySliceSelector
- Inherits:
-
Selector
- Object
- Expression
- Selector
- Janeway::AST::ArraySliceSelector
- 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.
Instance Attribute Summary collapse
-
#end ⇒ Object
readonly
Raw start / end / step values from the query source.
-
#start ⇒ Object
readonly
Raw start / end / step values from the query source.
-
#step ⇒ Object
readonly
Raw start / end / step values from the query source.
Attributes inherited from Expression
Instance Method Summary collapse
-
#initialize(start = nil, end_ = nil, step = nil) ⇒ ArraySliceSelector
constructor
A new instance of ArraySliceSelector.
- #to_s(brackets: true, **_ignored) ⇒ String
- #tree(level) ⇒ Array
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.
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
#end ⇒ Object (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 |
#start ⇒ Object (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 |
#step ⇒ Object (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
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
69 70 71 |
# File 'lib/janeway/ast/array_slice_selector.rb', line 69 def tree(level) [indented(level, to_s)] end |