Class: Cadenza::DocumentNode

Inherits:
Object
  • Object
show all
Includes:
TreeNode
Defined in:
lib/cadenza/nodes/document_node.rb

Overview

The DocumentNode is intended to be the root node of any parsed AST in Cadenza. In addition to holding the primary children it also holds data that affects the entire document, such as block definitions and the name of any extended template.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TreeNode

#to_tree

Constructor Details

#initialize(children = []) ⇒ DocumentNode

creates a new Cadenza::DocumentNode with the optional children nodes attached to it.

Parameters:

  • children (Array) (defaults to: [])

    any child nodes to initially assign to this node.


23
24
25
26
# File 'lib/cadenza/nodes/document_node.rb', line 23

def initialize(children=[])
  @children = children
  @blocks = {}
end

Instance Attribute Details

#blocksHash

Returns a mapping of any blocks defined in this document where the key is the name of the block and the value is the BlockNode itself.

Returns:

  • (Hash)

    a mapping of any blocks defined in this document where the key is the name of the block and the value is the BlockNode itself


18
19
20
# File 'lib/cadenza/nodes/document_node.rb', line 18

def blocks
  @blocks
end

#childrenArray

Returns any child nodes belonging to this one.

Returns:

  • (Array)

    any child nodes belonging to this one


13
14
15
# File 'lib/cadenza/nodes/document_node.rb', line 13

def children
  @children
end

#extendsString

Returns the name of the template this document will inherit from.

Returns:

  • (String)

    the name of the template this document will inherit from


10
11
12
# File 'lib/cadenza/nodes/document_node.rb', line 10

def extends
  @extends
end

Instance Method Details

#==(rhs) ⇒ Boolean

Returns if the given Cadenza::DocumentNode is equivalent by value to this one.

Parameters:

Returns:


30
31
32
33
34
# File 'lib/cadenza/nodes/document_node.rb', line 30

def ==(rhs)
  @children == rhs.children &&
  @extends == rhs.extends &&
  @blocks == rhs.blocks
end

#add_block(block) ⇒ Object

adds the given BlockNode to this document replacing any existing definition of the same name.

Parameters:


39
40
41
# File 'lib/cadenza/nodes/document_node.rb', line 39

def add_block(block)
  @blocks[block.name] = block
end

#implied_globalsArray

returns a list of any global variable names implied by examining the children of this node.

Returns:

  • (Array)

46
47
48
# File 'lib/cadenza/nodes/document_node.rb', line 46

def implied_globals
  @children.map(&:implied_globals).flatten.uniq
end