Class: Gjp::TemplateManager
- Inherits:
-
Object
- Object
- Gjp::TemplateManager
- Includes:
- Logging
- Defined in:
- lib/gjp/template_manager.rb
Overview
operates on files in template/
Instance Attribute Summary collapse
-
#template_path ⇒ Object
readonly
Returns the value of attribute template_path.
Instance Method Summary collapse
-
#copy(template_name, destination_dir) ⇒ Object
copies a template file in a directory.
-
#generate(template_name, object_binding, destination_path = nil) ⇒ Object
generates content from an ERB template and an object binding if destination_path is given, write it to that file, otherwise just return it.
-
#initialize ⇒ TemplateManager
constructor
A new instance of TemplateManager.
Methods included from Logging
Constructor Details
#initialize ⇒ TemplateManager
Returns a new instance of TemplateManager.
12 13 14 |
# File 'lib/gjp/template_manager.rb', line 12 def initialize @template_path = File.join(File.dirname(__FILE__), "..", "template") end |
Instance Attribute Details
#template_path ⇒ Object (readonly)
Returns the value of attribute template_path.
10 11 12 |
# File 'lib/gjp/template_manager.rb', line 10 def template_path @template_path end |
Instance Method Details
#copy(template_name, destination_dir) ⇒ Object
copies a template file in a directory
17 18 19 |
# File 'lib/gjp/template_manager.rb', line 17 def copy(template_name, destination_dir) FileUtils.cp_r(File.join(template_path, template_name), destination_dir) end |
#generate(template_name, object_binding, destination_path = nil) ⇒ Object
generates content from an ERB template and an object binding if destination_path is given, write it to that file, otherwise just return it
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/gjp/template_manager.rb', line 24 def generate(template_name, object_binding, destination_path = nil) erb = ERB.new File.read(File.join(template_path, template_name)), nil, "<>" new_content = erb.result(object_binding) unless destination_path.nil? File.open(destination_path, "w") { |io| io.write new_content } end new_content end |