Module: Scaffoldish::DSL::Template

Included in:
Scaffold
Defined in:
lib/scaffoldish/dsl/template.rb

Instance Method Summary collapse

Instance Method Details

#chunk(template_path, output_path, data) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/scaffoldish/dsl/template.rb', line 34

def chunk(template_path, output_path, data)
  template = File.read(File.join(Application.instance.templates_root, template_path))
  renderer = ERB.new(template)

  result = renderer.result(data.instance_eval { binding })

  puts "Edit #{output_path}:"
  puts result
  puts ""
end

#generate(template_path, output_path, data) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/scaffoldish/dsl/template.rb', line 13

def generate(template_path, output_path, data)
  template = File.read(File.join(Application.instance.templates_root, template_path))
  renderer = ERB.new(template)

  result = renderer.result(data.instance_eval { binding })

  absolute_output_path = File.join(Application.instance.project_root, output_path)

  if File.exist?(absolute_output_path)
    puts("Can't generate #{absolute_output_path}, it already exists. Skip it")
    return
  end

  dir = File.dirname(absolute_output_path)
  FileUtils.mkpath(dir)

  File.open(absolute_output_path, 'w') do |file|
    file.write(result)
  end
end