Class: Templater::Actions::Template
- Defined in:
- lib/templater/actions/template.rb
Instance Attribute Summary
Attributes inherited from Action
#destination, #generator, #name, #options, #source
Instance Method Summary collapse
-
#exists? ⇒ Boolean
Checks if the destination file already exists.
-
#identical? ⇒ Boolean
Checks if the content of the file at the destination is identical to the rendered result.
-
#initialize(generator, name, source, destination, options = {}) ⇒ Template
constructor
Builds a new template.
-
#invoke! ⇒ Object
Renders the template and copies it to the destination.
-
#render ⇒ Object
Renders the template using ERB and returns the result as a String.
-
#revoke! ⇒ Object
removes the destination file.
Methods inherited from Action
Constructor Details
#initialize(generator, name, source, destination, options = {}) ⇒ Template
Builds a new template.
Parameters
- generator<Object>
-
Context for rendering
- name<Symbol>
-
The name of this template
- source<String>
-
Full path to the source of this template
- destination<String>
-
Full path to the destination of this template
- options<HashSymbol=>Symbol
-
Options, including callbacks.
13 14 15 16 17 18 19 |
# File 'lib/templater/actions/template.rb', line 13 def initialize(generator, name, source, destination, ={}) self.generator = generator self.name = name self.source = source self.destination = destination self. = end |
Instance Method Details
#exists? ⇒ Boolean
Checks if the destination file already exists.
Returns
- Boolean
-
true if the file exists, false otherwise.
34 35 36 |
# File 'lib/templater/actions/template.rb', line 34 def exists? ::File.exists?(destination) end |
#identical? ⇒ Boolean
Checks if the content of the file at the destination is identical to the rendered result.
Returns
- Boolean
-
true if it is identical, false otherwise.
42 43 44 |
# File 'lib/templater/actions/template.rb', line 42 def identical? ::File.read(destination) == render if ::File.exists?(destination) end |
#invoke! ⇒ Object
Renders the template and copies it to the destination.
47 48 49 50 51 52 |
# File 'lib/templater/actions/template.rb', line 47 def invoke! callback(:before) ::FileUtils.mkdir_p(::File.dirname(destination)) ::File.open(destination, 'w') {|f| f.write render } callback(:after) end |
#render ⇒ Object
Renders the template using ERB and returns the result as a String.
Returns
- String
-
The rendered template.
25 26 27 28 |
# File 'lib/templater/actions/template.rb', line 25 def render context = generator.instance_eval 'binding' ERB.new(::File.read(source), nil, '-').result(context) end |
#revoke! ⇒ Object
removes the destination file
55 56 57 |
# File 'lib/templater/actions/template.rb', line 55 def revoke! ::FileUtils.rm(destination, :force => true) end |