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:

  • (#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.

Yield Parameters:



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/accessibility/enumerators.rb', line 20

def each
  # @todo mutate the array less, perhaps use an index instead
  #       of #shift, then the array only grows
  queue = [@root]
  until queue.empty?
    queue.shift.children.each do |x|
      queue << x
      yield x
    end
  end
end

#findObject

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.



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

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