Class: Accessibility::Enumerators::BreadthFirst

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(root) ⇒ BreadthFirst

Returns a new instance of BreadthFirst.

Parameters:

  • root (#children)


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.

Yields:

  • An element in the UI hierarchy

Yield Parameters:



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.

Yields:

  • An element in the UI hierarchy

Yield Parameters:



42
43
44
# File 'lib/accessibility/enumerators.rb', line 42

def find
  each { |x| return x if yield x }
end