Class: Templater
- Inherits:
-
Object
- Object
- Templater
- 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
-
#string ⇒ Object
readonly
Returns the value of attribute string.
Instance Method Summary collapse
-
#initialize(string, opt = {}) ⇒ Templater
constructor
A new instance of Templater.
-
#inspect ⇒ Object
:nodoc:.
- #result(delegator = nil, variables = {}, &on_error) ⇒ Object
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
#string ⇒ Object (readonly)
Returns the value of attribute string.
55 56 57 |
# File 'lib/templater.rb', line 55 def string @string end |
Instance Method Details
#inspect ⇒ Object
: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.+" for #{self.inspect} with #{variables.inspect}", e.backtrace end |