Class: Twig::Runtime::LoopIterator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seq) ⇒ LoopIterator

Returns a new instance of LoopIterator.



8
9
10
11
12
13
# File 'lib/twig/runtime/loop_iterator.rb', line 8

def initialize(seq)
  @seq = Runtime::EnumerableHash.from(seq).to_enum
  @index0 = 0
  @previous = nil
  @next = nil
end

Instance Attribute Details

#index0Object (readonly)

Returns the value of attribute index0.



6
7
8
# File 'lib/twig/runtime/loop_iterator.rb', line 6

def index0
  @index0
end

#nextObject (readonly)

Returns the value of attribute next.



6
7
8
# File 'lib/twig/runtime/loop_iterator.rb', line 6

def next
  @next
end

#previousObject (readonly)

Returns the value of attribute previous.



6
7
8
# File 'lib/twig/runtime/loop_iterator.rb', line 6

def previous
  @previous
end

Instance Method Details

#eachObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/twig/runtime/loop_iterator.rb', line 15

def each(&)
  @index0 = 0

  loop do
    current = @seq.next

    begin
      @next = @seq.peek
    rescue StopIteration
      @next = nil
    end

    yield current[0], current[1]
    @index0 += 1
    @previous = current
  rescue StopIteration
    break
  end
end

#firstObject



35
36
37
# File 'lib/twig/runtime/loop_iterator.rb', line 35

def first
  @index0.zero?
end

#indexObject



47
48
49
# File 'lib/twig/runtime/loop_iterator.rb', line 47

def index
  @index0 + 1
end

#lastObject



39
40
41
# File 'lib/twig/runtime/loop_iterator.rb', line 39

def last
  revindex0.zero? || length.zero?
end

#lengthObject



43
44
45
# File 'lib/twig/runtime/loop_iterator.rb', line 43

def length
  @length ||= @seq.count
end

#revindexObject



55
56
57
# File 'lib/twig/runtime/loop_iterator.rb', line 55

def revindex
  revindex0 + 1
end

#revindex0Object



51
52
53
# File 'lib/twig/runtime/loop_iterator.rb', line 51

def revindex0
  [0, length - index].max
end