Class: BetterHtml::AST::Iterator
- Inherits:
-
Object
- Object
- BetterHtml::AST::Iterator
- Defined in:
- lib/better_html/ast/iterator.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(types, &block) ⇒ Iterator
constructor
A new instance of Iterator.
- #traverse(node) ⇒ Object
- #traverse_all(nodes) ⇒ Object
Constructor Details
#initialize(types, &block) ⇒ Iterator
Returns a new instance of Iterator.
18 19 20 21 |
# File 'lib/better_html/ast/iterator.rb', line 18 def initialize(types, &block) @types = types.nil? ? nil : Array.wrap(types) @block = block end |
Class Method Details
.descendants(root_node, type) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/better_html/ast/iterator.rb', line 10 def descendants(root_node, type) Enumerator.new do |yielder| t = new(type) { |node| yielder << node } t.traverse(root_node) end end |
Instance Method Details
#traverse(node) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/better_html/ast/iterator.rb', line 23 def traverse(node) return unless node.is_a?(::AST::Node) @block.call(node) if @types.nil? || @types.include?(node.type) traverse_all(node) end |
#traverse_all(nodes) ⇒ Object
30 31 32 33 34 |
# File 'lib/better_html/ast/iterator.rb', line 30 def traverse_all(nodes) nodes.to_a.each do |node| traverse(node) if node.is_a?(::AST::Node) end end |