Class: DiceBag::DefaultTemplateFile

Inherits:
Object
  • Object
show all
Includes:
DiceBagFile
Defined in:
lib/dice_bag/default_template_file.rb

Instance Attribute Summary

Attributes included from DiceBagFile

#file, #filename

Instance Method Summary collapse

Methods included from DiceBagFile

#assert_existence, #should_write?, #write

Constructor Details

#initialize(name, location = nil) ⇒ DefaultTemplateFile

Returns a new instance of DefaultTemplateFile.


12
13
14
15
16
17
18
19
20
# File 'lib/dice_bag/default_template_file.rb', line 12

def initialize(name, location=nil)
  #if called from command line with only a name we search in all our templates for the file
  if (File.dirname(name) == '.')
    name = AvailableTemplates.template_filename_for(name)
  end
  @filename = File.basename(name)
  @file = name
  @template_location = location
end

Instance Method Details

#create_fileObject


22
23
24
25
26
27
28
29
30
# File 'lib/dice_bag/default_template_file.rb', line 22

def create_file
  contents = read_template(@file)
  template_file = File.join(Project.root, @template_location, @filename)
  File.open(template_file, 'w') do |file|
    file.puts(contents)
  end
  puts "new template file generated in #{template_file}.
        execute 'rake config:all' to get the corresponding configuration file."
end

#read_template(template) ⇒ Object


32
33
34
35
36
# File 'lib/dice_bag/default_template_file.rb', line 32

def read_template(template)
  # Some templates need the name of the project. We put a placeholder
  # PROJECT_NAME there, it gets substituted by the real name of the project here
  File.readlines(template).join.gsub("PROJECT_NAME", Project.name)
end