Class: Hexp::Node::Rewriter

Inherits:
Object
  • Object
show all
Includes:
Hexp
Defined in:
lib/hexp/node/rewriter.rb

Overview

Create a new Hexp node based on an existing node

Rewriting in this case means iterating over the whole Hexp tree, and for each element providing zero or more elements to replace it with.

Constant Summary

Constants included from Hexp

Error, ROOT, VERSION

Instance Method Summary collapse

Methods included from Hexp

Array, build, included, parse

Constructor Details

#initialize(node, block) ⇒ Rewriter

Initialize a rewriter with the node to operate on, and the action

Parameters:

  • node (Hexp::Node)

    The root node of the tree to be altered

  • block (Proc)

    The action to perform on each node



19
20
21
# File 'lib/hexp/node/rewriter.rb', line 19

def initialize(node, block)
  @node, @block = node, block
end

Instance Method Details

#to_hexpHexp::Node

Implicit Hexp conversion protocol

A Hexp::Node::Rewriter is lazy, only when one of the DSL methods is used, does the rewriting happen.

Returns:



31
32
33
34
35
36
37
# File 'lib/hexp/node/rewriter.rb', line 31

def to_hexp
  @hexp ||= H[
    @node.tag,
    @node.attributes,
    @block ? rewrite_children : @node.children
  ]
end