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)



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

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

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.



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.

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

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