Class: Arfy::MigrationBuilder::TemplateHandler
- Inherits:
-
Object
- Object
- Arfy::MigrationBuilder::TemplateHandler
- Defined in:
- lib/arfy/migration_builder/template_handler.rb
Instance Method Summary collapse
-
#initialize(template_body, vars) ⇒ TemplateHandler
constructor
A new instance of TemplateHandler.
- #read_template_file(template_path) ⇒ Object
- #render ⇒ Object
- #template ⇒ Object
- #template_body ⇒ Object
Constructor Details
#initialize(template_body, vars) ⇒ TemplateHandler
Returns a new instance of TemplateHandler.
12 13 14 15 16 17 18 19 |
# File 'lib/arfy/migration_builder/template_handler.rb', line 12 def initialize(template_body, vars) @vars = vars || {} if(File.exists?(template_body)) @template_path = template_body if File.exists?(template_body) else @template_body = template_body end end |
Instance Method Details
#read_template_file(template_path) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/arfy/migration_builder/template_handler.rb', line 28 def read_template_file(template_path) template_body = '' File.open(template_path, 'r') do |file| while line = file.gets template_body << line end end template_body end |
#render ⇒ Object
42 43 44 |
# File 'lib/arfy/migration_builder/template_handler.rb', line 42 def render template.result(ERBBinder.new(@vars).send(:bind)) end |
#template ⇒ Object
38 39 40 |
# File 'lib/arfy/migration_builder/template_handler.rb', line 38 def template ERB.new(template_body, 0, "<>") end |
#template_body ⇒ Object
21 22 23 24 25 26 |
# File 'lib/arfy/migration_builder/template_handler.rb', line 21 def template_body if @template_body.nil? @template_body = read_template_file(@template_path) end @template_body end |