Class: Cadenza::Context

Inherits:
Object
  • Object
show all
Includes:
Blocks, Filters, FunctionalVariables, Loaders, Stack
Defined in:
lib/cadenza/context.rb,
lib/cadenza/context/stack.rb,
lib/cadenza/context/blocks.rb,
lib/cadenza/context/filters.rb,
lib/cadenza/context/loaders.rb,
lib/cadenza/context/functional_variables.rb

Overview

The Context class is an essential class in Cadenza that contains all the data necessary to render a template to it’s output. The context holds all defined variable names (see Stack#stack), filters (see Filters#filters), functional variables (see FunctionalVariables#functional_variables), generic blocks (see Blocks#blocks), loaders (see Loaders#loaders) and configuration data as well as all the methods you should need to define and evaluate those.

Defined Under Namespace

Modules: Blocks, Filters, FunctionalVariables, Loaders, Stack

Instance Attribute Summary

Attributes included from Loaders

#loaders, #whiny_template_loading

Attributes included from FunctionalVariables

#functional_variables

Attributes included from Blocks

#blocks

Attributes included from Filters

#filters

Attributes included from Stack

#stack

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Loaders

#add_load_path, #add_loader, #clear_loaders, #load_source, #load_source!, #load_template, #load_template!

Methods included from FunctionalVariables

#alias_functional_variable, #define_functional_variable, #evaluate_functional_variable, #lookup_functional_variable

Methods included from Blocks

#alias_block, #define_block, #evaluate_block, #lookup_block

Methods included from Filters

#alias_filter, #define_filter, #evaluate_filter, #lookup_filter

Methods included from Stack

#assign, #lookup, #pop, #push

Constructor Details

#initialize(initial_scope = {}) ⇒ Context

creates a new context object with an empty stack, filter list, functional variable list, block list, loaders list and default configuration options.

When created you can push an optional scope onto as the initial stack

Parameters:

  • initial_scope (Hash) (defaults to: {})

    the initial scope for the context



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

def initialize(initial_scope={})
   push initial_scope
end

Class Method Details

.lookup_on_object(identifier, object) ⇒ Object

looks up the given identifier name on the given ruby object using Cadenza’s internal logic for doing so.

Array objects allow identifiers in the form of numbers to retrieve the index specified. Example: alphabet.0 returns “a”

Hash objects allow identifiers to be fetched as keys of that hash.

Any object which is a subclass of Cadenza::ContextObject will have a value looked up according to the logic defined in Cadenza::ContextObject#invoke_context_method

Parameters:

  • identifier (Symbol|String)

    the name of the value to look up on this object

  • object (Object)

    the object to look up the value on

Returns:

  • (Object)

    the result of the lookup



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cadenza/context.rb', line 30

def self.lookup_on_object(identifier, object)
   sym_identifier = identifier.to_sym

   # allow looking up array indexes with dot notation, example: alphabet.0 => "a"
   if object.respond_to?(:[]) && object.is_a?(Array) && identifier =~ /\A\d+\z/
      return object[identifier.to_i]
   end

   # otherwise if it's a hash look up the string or symbolized key
   if object.respond_to?(:[]) && object.is_a?(Hash) && (object.has_key?(identifier) || object.has_key?(sym_identifier))
      return object[identifier] || object[sym_identifier]
   end

   # if the identifier is a callable method then call that
   return object.send(:invoke_context_method, identifier) if object.is_a?(Cadenza::ContextObject)
   
   nil
end

Instance Method Details

#cloneContext

creates a new instance of the context with the stack, loaders, filters, functional variables and blocks shallow copied.

Returns:



63
64
65
66
67
68
69
70
71
72
# File 'lib/cadenza/context.rb', line 63

def clone
   copy = super
   copy.instance_variable_set("@stack", stack.dup)
   copy.instance_variable_set("@loaders", loaders.dup)
   copy.instance_variable_set("@filters", filters.dup)
   copy.instance_variable_set("@functional_variables", functional_variables.dup)
   copy.instance_variable_set("@blocks", blocks.dup)
   
   copy
end