Class: Gjp::TemplateManager

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/gjp/template_manager.rb

Overview

operates on files in template/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log

Constructor Details

#initializeTemplateManager

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_pathObject (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