Class: Templater

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

Defined Under Namespace

Classes: Variables

Constant Summary collapse

Raiser =
proc { |e|
	raise
}
Teller =
proc { |e|
	"<<#{e.class}: #{e}>>"
}
Opt =

Option defaults

{
	:safe_level => nil,
	:trim_mode  => '%<>',
	:eoutvar    => '_erbout'
}
Binder =

binding method

Object.instance_method(:binding)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, opt = {}) ⇒ Templater

Returns a new instance of Templater.



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/templater.rb', line 56

def initialize(string, opt={})
	opt, string   = string, nil if string.kind_of?(Hash)
	opt           = Opt.merge(opt)
	file          = nil
	if !string || File.exist?(string) then
		string ||= opt[:filename]
		file     = string
		string   = File.read(string)
	end
	@string       = string.dup.freeze
	@erb          = ERB.new(@string, *opt.values_at(:safe_level, :trim_mode, :eoutvar))
	@erb.filename = file if file
end

Instance Attribute Details

#stringObject (readonly)

Returns the value of attribute string.



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

def string
  @string
end

Instance Method Details

#inspectObject

 :nodoc:



79
80
81
82
83
84
85
# File 'lib/templater.rb', line 79

def inspect # :nodoc:
	"#<%s:0x%x string=%s>" %  [
		self.class,
		object_id << 1,
		@string.inspect
	]
end

#result(delegator = nil, variables = {}, &on_error) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/templater.rb', line 70

def result(delegator=nil, variables={}, &on_error)
	variables ||= {}
	on_error  ||= Raiser
	variables = Variables.new(delegator, variables, &on_error) unless variables.kind_of?(Variables)
	@erb.result(Binder.bind(variables).call)
rescue NameError => e
	raise NameError, e.message+" for #{self.inspect} with #{variables.inspect}", e.backtrace
end