Class: JSONP3::SliceSelector
- Defined in:
- lib/json_p3/selector.rb
Overview
The slice selector selects a range of elements from an array.
Instance Attribute Summary collapse
-
#start ⇒ Object
readonly
Returns the value of attribute start.
-
#step ⇒ Object
readonly
Returns the value of attribute step.
-
#stop ⇒ Object
readonly
Returns the value of attribute stop.
Attributes inherited from Selector
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#initialize(env, token, start, stop, step) ⇒ SliceSelector
constructor
A new instance of SliceSelector.
- #resolve(node) ⇒ Object
- #to_s ⇒ Object
Methods inherited from Selector
Constructor Details
#initialize(env, token, start, stop, step) ⇒ SliceSelector
Returns a new instance of SliceSelector.
168 169 170 171 172 173 |
# File 'lib/json_p3/selector.rb', line 168 def initialize(env, token, start, stop, step) super(env, token) @start = start @stop = stop @step = step || 1 end |
Instance Attribute Details
#start ⇒ Object (readonly)
Returns the value of attribute start.
166 167 168 |
# File 'lib/json_p3/selector.rb', line 166 def start @start end |
#step ⇒ Object (readonly)
Returns the value of attribute step.
166 167 168 |
# File 'lib/json_p3/selector.rb', line 166 def step @step end |
#stop ⇒ Object (readonly)
Returns the value of attribute stop.
166 167 168 |
# File 'lib/json_p3/selector.rb', line 166 def stop @stop end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
208 209 210 211 212 213 214 |
# File 'lib/json_p3/selector.rb', line 208 def ==(other) self.class == other.class && @start == other.start && @stop == other.stop && @step == other.step && @token == other.token end |
#hash ⇒ Object
218 219 220 |
# File 'lib/json_p3/selector.rb', line 218 def hash [@start, @stop, @step, @token].hash end |
#resolve(node) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/json_p3/selector.rb', line 175 def resolve(node) return [] unless node.value.is_a?(Array) length = node.value.length return [] if length.zero? || @step.zero? norm_start = normalized_start(length) norm_stop = normalized_stop(length) nodes = [] if @step.positive? for i in (norm_start...norm_stop).step(@step) # rubocop:disable Style/For nodes << node.new_child(node.value[i], i) end else i = norm_start while i > norm_stop nodes << node.new_child(node.value[i], i) i += @step end end nodes end |
#to_s ⇒ Object
201 202 203 204 205 206 |
# File 'lib/json_p3/selector.rb', line 201 def to_s start = @start || "" stop = @stop || "" step = @step || 1 "#{start}:#{stop}:#{step}" end |