Class: Cadenza::ContextObject

Inherits:
Object
  • Object
show all
Defined in:
lib/cadenza/context_object.rb

Overview

ContextObject is a bare bones class which should become the superclass of any object you wish to push onto the Cadenza::Context::Stack#stack of the context you render your template with.

Normal ruby objects (with the exception of Hash and Array) will not allow any value to be looked up upon it when pushed onto the Cadenza::Context::Stack#stack unless they are subclasses of ContextObject. See Cadenza::Context.lookup_on_object for specific details on how this is done.

Instance Method Summary collapse

Instance Method Details

#after_method(method) ⇒ Object

Note:

this method is a private method, but has been documented for your understanding.

Callback method which is called after the given method name is called by #invoke_context_method

This method can be overridden by subclasses for whatever purpose they wish, ex. statistics gathering

Parameters:

  • method (String|Symbol)

    the name of the method that was called



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

def after_method(method)
   # no implementation, used by subclasses
end

#before_method(method) ⇒ Object

Note:

this method is a private method, but has been documented for your understanding.

Callback method which is called before the given method name is called by #invoke_context_method

This method can be overridden by subclasses for whatever purpose they wish, ex. statistics gathering

Parameters:

  • method (String|Symbol)

    the name of the method that was called



25
26
27
# File 'lib/cadenza/context_object.rb', line 25

def before_method(method)
   # no implementation, used by subclasses
end

#context_methodsArray

Note:

this method is a private method, but has been documented for your understanding.

Returns a list of methods which are in the public visibility scope for this class

Returns:

  • (Array)

    a list of methods which are in the public visibility scope for this class



95
96
97
# File 'lib/cadenza/context_object.rb', line 95

def context_methods
   (self.class.public_instance_methods - ContextObject.public_instance_methods).map(&:to_s)
end

#invoke_context_method(method) ⇒ Object

Note:

this method is a private method, but has been documented for your understanding.

Looks up and calls the given method on this object and returns it's value.

Only methods in the public visibility scope (see #context_methods) can be called by this method.

If the method can not be found on this object then #missing_context_method will be called instead. You can use this to provide a “method_missing” for your context object.

Around the call to the method on this object the #before_method and #after_method methods will be called with the name of the method. You may wish to use this to perform live benchmarking or other statistics gathering.

Parameters:

  • method (Symbol|String)

    the name of the method to call

Returns:

  • (Object)

    the value returned from the method call



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cadenza/context_object.rb', line 76

def invoke_context_method(method)
   send(:before_method, method)
   
   if context_methods.include?(method.to_s)
      result = send(method)
   else
      result = send(:missing_context_method, method)
   end

   send(:after_method, method)

   result
end

#missing_context_method(method) ⇒ Object

Note:

this method is a private method, but has been documented for your understanding.

Method called by #invoke_context_method when the given method could not be found in the class's public method list.

The return value of this method is returned by #invoke_context_method

Parameters:

  • method (String|Symbol)

    the name of the method that was called



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

def missing_context_method(method)
   # no implementation, used by subclasses
end