Class: Templater::Variables

Inherits:
OpenStruct show all
Defined in:
lib/templater.rb

Overview

Indexing

Author: Stefan Rusterholz Contact: [email protected]> Version: 0.0.1 Date: 2007-10-12

About

Used by Templater to resolve variables.

Synopsis

tmpl = Variables.new(delegator, :variable => "content") { |exception|
  do_something_with_exception
}

Instance Method Summary collapse

Methods inherited from OpenStruct

#[], #[]=, #__hash__, #to_hash, with

Constructor Details

#initialize(delegator = nil, variables = {}, &on_error) ⇒ Variables

Arguments

  • delegator: All method calls and undefined variables are delegated to this object as method call.

  • variables: A hash with variables in it, keys must be Symbols.

  • &on_error: The block is yielded in case of an exception with the exception as argument



67
68
69
70
71
# File 'lib/templater.rb', line 67

def initialize(delegator=nil, variables={}, &on_error)
	super(variables)
	@delegator = delegator
	@on_error  = on_error || RaiseExceptions
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *a, &b) ⇒ Object

 :nodoc:



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/templater.rb', line 73

def method_missing(m, *a, &b) # :nodoc:
	if @table.key?(m) || m.to_s =~ /=$/ then
		super
	elsif @delegator && @delegator.respond_to?(m) then
		@delegator.send(m, *a, &b) if @delegator
	else
		raise NameError, "undefined local variable or method `#{m}'"
	end
rescue Exception => e
	@on_error.call(e)
end

Instance Method Details

#inspectObject

 :nodoc:



85
86
87
# File 'lib/templater.rb', line 85

def inspect # :nodoc:
	@table.inspect
end