Class: Hexp::Node::Selection
- Inherits:
-
Object
- Object
- Hexp::Node::Selection
- Includes:
- Enumerable
- Defined in:
- lib/hexp/node/selection.rb
Overview
Direct Known Subclasses
Instance Method Summary collapse
-
#attr(name, value) ⇒ Hexp::Node
Set an attribute on all matching nodes.
-
#each {|| ... } ⇒ Object
Yield each matching node.
-
#initialize(node, block) ⇒ Selection
constructor
private
Initialize a selection with the root node, and the selection block used as the filtering criterion.
-
#rewrite {|| ... } ⇒ Hexp::Node
Replace matching nodes.
-
#wrap(tag, attributes = {}) ⇒ Hexp::Node
Wrap each matching node in a specific node.
Constructor Details
#initialize(node, block) ⇒ Selection
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initialize a selection with the root node, and the selection block used as the filtering criterion
33 34 35 |
# File 'lib/hexp/node/selection.rb', line 33 def initialize(node, block) @node, @select_block = node, block end |
Instance Method Details
#attr(name, value) ⇒ Hexp::Node
Set an attribute on all matching nodes
67 68 69 70 71 |
# File 'lib/hexp/node/selection.rb', line 67 def attr(name, value) rewrite do |node| node.attr(name, value) end end |
#each {|| ... } ⇒ Object
Yield each matching node
95 96 97 98 99 100 101 102 |
# File 'lib/hexp/node/selection.rb', line 95 def each(&block) return to_enum(:each) unless block_given? @node.children.each do |child| child.select(&@select_block).each(&block) end yield @node if @select_block.(@node) end |
#rewrite {|| ... } ⇒ Hexp::Node
Replace matching nodes
Analogues to the main Hexp::Node#rewrite operation.
46 47 48 49 50 51 52 53 54 |
# File 'lib/hexp/node/selection.rb', line 46 def rewrite(&block) @node.rewrite do |node, parent| if @select_block.(node) block.(node, parent) else node end end end |
#wrap(tag, attributes = {}) ⇒ Hexp::Node
Wrap each matching node in a specific node
84 85 86 87 88 |
# File 'lib/hexp/node/selection.rb', line 84 def wrap(tag, attributes = {}) rewrite do |node| H[tag, attributes, [node]] end end |