Class: Liquid::RangeSlice
- Inherits:
-
Object
- Object
- Liquid::RangeSlice
- Defined in:
- lib/liquid/range_slice.rb
Instance Attribute Summary collapse
-
#length ⇒ Object
readonly
Returns the value of attribute length.
Instance Method Summary collapse
- #each ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(range, from, to, resource_limits) ⇒ RangeSlice
constructor
A new instance of RangeSlice.
- #reverse! ⇒ Object
Constructor Details
#initialize(range, from, to, resource_limits) ⇒ RangeSlice
Returns a new instance of RangeSlice.
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/liquid/range_slice.rb', line 7 def initialize(range, from, to, resource_limits) range_length = range.end - range.begin range_length += 1 unless range.exclude_end? range_length = 0 if range_length.negative? start = [from, 0].max finish = [to || range_length, range_length].min @first = range.begin + start @length = [finish - start, 0].max @direction = 1 @resource_limits = resource_limits end |
Instance Attribute Details
#length ⇒ Object (readonly)
Returns the value of attribute length.
5 6 7 |
# File 'lib/liquid/range_slice.rb', line 5 def length @length end |
Instance Method Details
#each ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/liquid/range_slice.rb', line 25 def each return enum_for(:each) unless block_given? value = @first @length.times do @resource_limits.increment_render_score(1) yield value value += @direction end end |
#empty? ⇒ Boolean
21 22 23 |
# File 'lib/liquid/range_slice.rb', line 21 def empty? @length.zero? end |
#reverse! ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/liquid/range_slice.rb', line 36 def reverse! unless empty? @first += @direction * (@length - 1) @direction = -@direction end self end |