Module: Cadenza::Context::Blocks

Included in:
Cadenza::Context
Defined in:
lib/cadenza/context/blocks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#blocksHash (readonly)

Returns the block names mapped to their implementing procs.

Returns:

  • (Hash)

    the block names mapped to their implementing procs



10
11
12
# File 'lib/cadenza/context/blocks.rb', line 10

def blocks
   @blocks ||= {}
end

Instance Method Details

#alias_block(original_name, alias_name) ⇒ Object

creates an alias of the given block name under a different name

Parameters:

  • original_name (Symbol)

    the original name of the block

  • alias_name (Symbol)

    the new name of the block

Returns:

  • nil

Raises:



42
43
44
# File 'lib/cadenza/context/blocks.rb', line 42

def alias_block(original_name, alias_name)
   define_block alias_name, &lookup_block(original_name)
end

#define_block(name) {|Context, Array, *args| ... } ⇒ Object

defines a generic block proc with the given name

Parameters:

  • name (Symbol)

    the name for the template to use for this block

Yields:

  • (Context, Array, *args)

    the block will receive the context object, a list of Node objects (it’s children), and a variable number of aarguments passed to the block.

Returns:

  • nil



31
32
33
34
# File 'lib/cadenza/context/blocks.rb', line 31

def define_block(name, &block)
   blocks[name.to_sym] = block
   nil
end

#evaluate_block(name, nodes, parameters) ⇒ String

calls the defined generic block proc with the given name and children nodes.

Parameters:

  • name (Symbol)

    the name of the block to evaluate

  • nodes (Array)

    the child nodes of the block

  • params (Array, [])

    a list of parameters to pass to the block when calling it.

Returns:

  • (String)

    the result of evaluating the block

Raises:



55
56
57
# File 'lib/cadenza/context/blocks.rb', line 55

def evaluate_block(name, nodes, parameters)
   lookup_block(name).call(self, nodes, parameters)
end

#lookup_block(name) ⇒ Proc

looks up the block by name

Parameters:

  • name (Symbol)

    the name of the block to look up

Returns:

  • (Proc)

    the block implementation

Raises:



19
20
21
# File 'lib/cadenza/context/blocks.rb', line 19

def lookup_block(name)
   blocks.fetch(name.to_sym) { raise BlockNotDefinedError.new("undefined block '#{name}'") }
end