mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
42 lines
773 B
Crystal
42 lines
773 B
Crystal
module Spectator
|
|
class ExampleIterator
|
|
include Iterator(Example)
|
|
|
|
@stack : Array(Iterator(ExampleComponent))
|
|
|
|
def initialize(@group : Iterable(ExampleComponent))
|
|
iter = @group.each.as(Iterator(ExampleComponent))
|
|
@stack = [iter]
|
|
end
|
|
|
|
def next
|
|
until @stack.empty?
|
|
item = advance
|
|
return item if item.is_a?(Example)
|
|
end
|
|
stop
|
|
end
|
|
|
|
def rewind
|
|
iter = @group.each.as(Iterator(ExampleComponent))
|
|
@stack = [iter]
|
|
self
|
|
end
|
|
|
|
private def top
|
|
@stack.last
|
|
end
|
|
|
|
private def advance
|
|
item = top.next
|
|
case(item)
|
|
when ExampleGroup
|
|
@stack.push(item.each)
|
|
when Iterator::Stop
|
|
@stack.pop
|
|
else
|
|
item
|
|
end
|
|
end
|
|
end
|
|
end
|