Class: Arfy::MigrationBuilder::TemplateHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/arfy/migration_builder/template_handler.rb

Instance Method Summary collapse

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

#renderObject



42
43
44
# File 'lib/arfy/migration_builder/template_handler.rb', line 42

def render
  template.result(ERBBinder.new(@vars).send(:bind))
end

#templateObject



38
39
40
# File 'lib/arfy/migration_builder/template_handler.rb', line 38

def template
  ERB.new(template_body, 0, "<>")
end

#template_bodyObject



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