Implement example iterator

This commit is contained in:
Michael Miller 2018-11-13 10:06:43 -07:00
parent 66dcc21383
commit b5a18ad324

View file

@ -0,0 +1,42 @@
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