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:

  • (#children)


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

def initialize root
  @root = root
end

Instance Method Details

#each {|| ... } ⇒ Object

Yield Parameters:



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

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.

Yield Parameters:



75
76
77
78
79
80
# File 'lib/accessibility/enumerators.rb', line 75

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