Class: Accessibility::Enumerators::BreadthFirst
- Inherits:
-
Object
- Object
- Accessibility::Enumerators::BreadthFirst
- Includes:
- Enumerable
- Defined in:
- lib/accessibility/enumerators.rb
Overview
Enumerator for visiting each element in a UI hierarchy in breadth first order.
Instance Method Summary collapse
-
#each {|| ... } ⇒ Object
Semi-lazily iterate through the tree.
-
#find {|| ... } ⇒ Object
Override
Enumerable#find
for performance reasons. -
#initialize(root) ⇒ BreadthFirst
constructor
A new instance of BreadthFirst.
Constructor Details
#initialize(root) ⇒ BreadthFirst
Returns a new instance of BreadthFirst.
12 13 14 |
# File 'lib/accessibility/enumerators.rb', line 12 def initialize root @root = root end |
Instance Method Details
#each {|| ... } ⇒ Object
Semi-lazily iterate through the tree.
21 22 23 24 25 26 27 28 |
# File 'lib/accessibility/enumerators.rb', line 21 def each queue = [@root] until queue.empty? kids = queue.shift.children kids.each do |x| yield x end queue.concat kids end end |
#find {|| ... } ⇒ Object
Note:
Explicitly defined so that escaping at the first found
element actually works. Since only a single break
is
called when an item is found it does not fully escape the
built in implementation. Technically, we need to do this
with other 'escape-early' iteraters, but they aren't
being used...yet.
Override Enumerable#find
for performance reasons.
42 43 44 |
# File 'lib/accessibility/enumerators.rb', line 42 def find each { |x| return x if yield x } end |