Module: Cadenza::Context::FunctionalVariables

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#functional_variablesHash (readonly)

Returns the functional variable names mapped to their implementing procs.

Returns:

  • (Hash)

    the functional variable names mapped to their implementing procs



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

def functional_variables
   @functional_variables ||= {}
end

Instance Method Details

#alias_functional_variable(original_name, alias_name) ⇒ Object

creates an alias of the given functional variable name under a different name

Parameters:

  • original_name (Symbol)

    the original name of the functional variable

  • alias_name (Symbol)

    the new name of the functional variable

Returns:

  • nil

Raises:



40
41
42
# File 'lib/cadenza/context/functional_variables.rb', line 40

def alias_functional_variable(original_name, alias_name)
   define_functional_variable alias_name, &lookup_functional_variable(original_name)
end

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

defines a functional variable proc with the given name

Parameters:

  • name (Symbol)

    the name for the template to use for this variable

Yields:

  • (Context, *args)

    the block will receive the context object and a variable number of arguments passed to the variable.

Returns:

  • nil



29
30
31
32
# File 'lib/cadenza/context/functional_variables.rb', line 29

def define_functional_variable(name, &block)
   functional_variables[name.to_sym] = block
   nil
end

#evaluate_functional_variable(name, params = []) ⇒ Object

calls the defined functional variable proc with the given parameters and returns the result.

Parameters:

  • name (Symbol)

    the name of the functional variable to evaluate

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

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

Returns:

  • (Object)

    the result of evaluating the functional variable

Raises:



52
53
54
# File 'lib/cadenza/context/functional_variables.rb', line 52

def evaluate_functional_variable(name, params=[])
   lookup_functional_variable(name).call([self] + params)
end

#lookup_functional_variable(name) ⇒ Proc

looks up the functional variable by name

Parameters:

  • name (Symbol)

    the name of the functional variable to look up

Returns:

  • (Proc)

    the functional variable implementation

Raises:



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

def lookup_functional_variable(name)
   functional_variables.fetch(name.to_sym) { raise FunctionalVariableNotDefinedError.new("undefined functional variable '#{name}'") }
end