mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Implement example iterator
This commit is contained in:
parent
66dcc21383
commit
b5a18ad324
1 changed files with 42 additions and 0 deletions
42
src/spectator/example_iterator.cr
Normal file
42
src/spectator/example_iterator.cr
Normal 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
|
Loading…
Reference in a new issue