Class: Twig::Runtime::LoopContext
- Inherits:
-
Object
- Object
- Twig::Runtime::LoopContext
- Defined in:
- lib/twig/runtime/loop_context.rb
Instance Attribute Summary collapse
-
#parent ⇒ Object?
readonly
The parent context.
Instance Method Summary collapse
-
#call(iterator) ⇒ Enumerator
Recursion function.
- #changed(value) ⇒ Object
-
#cycle(value, *values) ⇒ Object
The current value in the cycle.
-
#depth ⇒ Integer
The depth starting from 1.
-
#depth0 ⇒ Integer
The depth starting from 0.
-
#initialize(loop, parent, blocks, recurse_func, depth) ⇒ LoopContext
constructor
A new instance of LoopContext.
- #next ⇒ Object
- #previous ⇒ Object
Constructor Details
#initialize(loop, parent, blocks, recurse_func, depth) ⇒ LoopContext
Returns a new instance of LoopContext.
19 20 21 22 23 24 25 |
# File 'lib/twig/runtime/loop_context.rb', line 19 def initialize(loop, parent, blocks, recurse_func, depth) @loop = loop @parent = parent @blocks = blocks @recurse_func = recurse_func @depth = depth end |
Instance Attribute Details
#parent ⇒ Object? (readonly)
Returns The parent context.
15 16 17 |
# File 'lib/twig/runtime/loop_context.rb', line 15 def parent @parent end |
Instance Method Details
#call(iterator) ⇒ Enumerator
Recursion function
38 39 40 41 42 43 44 45 46 |
# File 'lib/twig/runtime/loop_context.rb', line 38 def call(iterator) if @depth > 50 raise 'Nesting level too deep.' end @parent.buffer_and_return do @recurse_func.call(LoopIterator.new(iterator), @parent, @blocks, @recurse_func, @depth + 1) end end |
#changed(value) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/twig/runtime/loop_context.rb', line 48 def changed(value) if !defined?(@last_changed) || value != @last_changed @last_changed = value true else false end end |
#cycle(value, *values) ⇒ Object
Returns The current value in the cycle.
30 31 32 33 |
# File 'lib/twig/runtime/loop_context.rb', line 30 def cycle(value, *values) values.unshift(value) values[index0 % values.length] end |
#depth ⇒ Integer
Returns The depth starting from 1.
72 73 74 |
# File 'lib/twig/runtime/loop_context.rb', line 72 def depth @depth + 1 end |
#depth0 ⇒ Integer
Returns The depth starting from 0.
67 68 69 |
# File 'lib/twig/runtime/loop_context.rb', line 67 def depth0 @depth end |
#next ⇒ Object
62 63 64 |
# File 'lib/twig/runtime/loop_context.rb', line 62 def next @loop.next&.dig(1) end |
#previous ⇒ Object
58 59 60 |
# File 'lib/twig/runtime/loop_context.rb', line 58 def previous @loop.previous&.dig(1) end |