Class: Twig::Runtime::LoopContext

Inherits:
Object
  • Object
show all
Defined in:
lib/twig/runtime/loop_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#parentObject? (readonly)

Returns The parent context.

Returns:

  • (Object, nil)

    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

Parameters:

  • iterator (Enumerable)

    The iterator

Returns:

  • (Enumerator)

    An enumerator for recursive iteration



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.

Parameters:

  • value (Object)

    The first value in the cycle

  • values (Array<Object>)

    The rest of the values in the cycle

Returns:

  • (Object)

    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

#depthInteger

Returns The depth starting from 1.

Returns:

  • (Integer)

    The depth starting from 1



72
73
74
# File 'lib/twig/runtime/loop_context.rb', line 72

def depth
  @depth + 1
end

#depth0Integer

Returns The depth starting from 0.

Returns:

  • (Integer)

    The depth starting from 0



67
68
69
# File 'lib/twig/runtime/loop_context.rb', line 67

def depth0
  @depth
end

#nextObject



62
63
64
# File 'lib/twig/runtime/loop_context.rb', line 62

def next
  @loop.next&.dig(1)
end

#previousObject



58
59
60
# File 'lib/twig/runtime/loop_context.rb', line 58

def previous
  @loop.previous&.dig(1)
end