Class: Accessibility::Enumerators::DepthFirst

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/accessibility/enumerators.rb

Overview

Enumerator for visitng each element in a UI hierarchy in depth first order.

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ DepthFirst

Returns a new instance of DepthFirst

Parameters:

  • root (#children)


55
56
57
# File 'lib/accessibility/enumerators.rb', line 55

def initialize root
  @root = root
end

Instance Method Details

#each {|| ... } ⇒ Object

Yields:

  • An element in the UI hierarchy

Yield Parameters:



61
62
63
64
65
66
67
68
69
# File 'lib/accessibility/enumerators.rb', line 61

def each
  stack = @root.children
  until stack.empty?
    current = stack.shift
    yield current
    # needed to reverse, child ordering matters in practice
    stack.unshift *current.children
  end
end

#each_with_level {|, | ... } ⇒ Object

Walk the UI element tree and yield both the element and the level that the element is at relative to the root.

Yields:

  • An element in the UI hierarchy

Yield Parameters:



78
79
80
81
82
83
# File 'lib/accessibility/enumerators.rb', line 78

def each_with_level &block
  # @todo A bit of a hack that I would like to fix one day...
  @root.children.each do |element|
    recursive_each_with_level element, 1, block
  end
end