Class: Gem::Commands::NewCommand::ErbTemplate
- Defined in:
- lib/gem/commands/new_command/erb_template.rb
Overview
Indexing
Author: Stefan Rusterholz Contact: [email protected] Version: 0.1.0
About
A helper class for ERB, allows constructs like the one in the Synopsis to enable simple use of variables/methods in templates.
Synopsis
tmpl = Templater.new("Hello <%= name %>!")
tmpl.result(self, :name => 'world') # => 'Hello World!'
Defined Under Namespace
Classes: Variables
Constant Summary collapse
- Opt =
Option defaults
{ :safe_level => nil, :trim_mode => '%<>', :eoutvar => '_erbout' }
- Binder =
binding method
Object.instance_method(:binding)
- InstanceEvaler =
Object.instance_method(:instance_eval)
- Raiser =
A proc for &on_error in SilverPlatter::Variables::new or SilverPlatter::Templater#result. Raises the error further on.
proc { |e| raise }
- Teller =
A proc for &on_error in SilverPlatter::Variables.new or SilverPlatter::Templater#result. Inserts <<error_class: error_message>> in the place where the error occurred.
proc { |e| "<<#{e.class}: #{e}>>" }
Instance Attribute Summary collapse
-
#string ⇒ Object
readonly
The template string.
Class Method Summary collapse
-
.file(path, opt = nil) ⇒ Object
Like Templater.new, but instead of a template string, the path to the file containing the template.
- .replace(template, variables, &on_error) ⇒ Object
Instance Method Summary collapse
-
#initialize(string, opt = {}) ⇒ ErbTemplate
constructor
Arguments * string: The template string, it becomes frozen * opt: Option hash, keys: * :filename: The filename used for the evaluation (useful for error messages) * :safe_level: see ERB.new * :trim_mode: see ERB.new * :eoutvar: see ERB.new.
-
#inspect ⇒ Object
:nodoc:.
-
#result(delegate = nil, variables = {}, on_error_name = nil, &on_error) ⇒ Object
See Templater::Variables.new Returns the evaluated template.
- #result_with(opt, &block) ⇒ Object
Constructor Details
#initialize(string, opt = {}) ⇒ ErbTemplate
Arguments
-
string: The template string, it becomes frozen
-
opt: Option hash, keys:
-
:filename: The filename used for the evaluation (useful for error messages)
-
:safe_level: see ERB.new
-
:trim_mode: see ERB.new
-
:eoutvar: see ERB.new
-
155 156 157 158 159 160 161 162 |
# File 'lib/gem/commands/new_command/erb_template.rb', line 155 def initialize(string, opt={}) opt, string = string, nil if string.kind_of?(Hash) opt = Opt.merge(opt) file = opt.delete(:filename) @string = string.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)
The template string
136 137 138 |
# File 'lib/gem/commands/new_command/erb_template.rb', line 136 def string @string end |
Class Method Details
.file(path, opt = nil) ⇒ Object
Like Templater.new, but instead of a template string, the path to the file containing the template. Sets :filename.
140 141 142 |
# File 'lib/gem/commands/new_command/erb_template.rb', line 140 def self.file(path, opt=nil) new(File.read(path), (opt || {}).merge(:filename => path)) end |
.replace(template, variables, &on_error) ⇒ Object
144 145 146 |
# File 'lib/gem/commands/new_command/erb_template.rb', line 144 def self.replace(template, variables, &on_error) new(template).result(nil, variables, &on_error) end |
Instance Method Details
#inspect ⇒ Object
:nodoc:
186 187 188 189 190 191 |
# File 'lib/gem/commands/new_command/erb_template.rb', line 186 def inspect # :nodoc: sprintf "#<%s:0x%x string=%s>", self.class, object_id << 1, @string.inspect end |
#result(delegate = nil, variables = {}, on_error_name = nil, &on_error) ⇒ Object
See Templater::Variables.new Returns the evaluated template. Default &on_error is the Templater::Raiser proc.
167 168 169 170 171 172 173 174 |
# File 'lib/gem/commands/new_command/erb_template.rb', line 167 def result(delegate=nil, variables={}, on_error_name=nil, &on_error) variables ||= {} on_error ||= Raiser variables = Variables.new(delegate, variables, on_error_name, &on_error) @erb.result(variables.__binding__) rescue NameError => e raise NameError, e.+" for #{self.inspect} with #{variables.inspect}", e.backtrace end |
#result_with(opt, &block) ⇒ Object
176 177 178 179 180 181 182 183 184 |
# File 'lib/gem/commands/new_command/erb_template.rb', line 176 def result_with(opt, &block) variables = opt.delete(:variables) || {} on_error = opt.delete(:on_error) || Raiser on_error_name = opt.delete(:on_error_name) || Raiser variables = Variables.new(delegate, variables, on_error_name, block, &on_error) @erb.result(variables.__binding__) rescue NameError => e raise NameError, e.+" for #{self.inspect} with #{variables.inspect}", e.backtrace end |