Class: DiceBag::DefaultTemplateFile
- Inherits:
-
Object
- Object
- DiceBag::DefaultTemplateFile
- Includes:
- DiceBagFile
- Defined in:
- lib/dice_bag/default_template_file.rb
Instance Attribute Summary
Attributes included from DiceBagFile
Instance Method Summary collapse
- #create_file ⇒ Object
-
#initialize(name, location = nil) ⇒ DefaultTemplateFile
constructor
A new instance of DefaultTemplateFile.
- #read_template(template) ⇒ Object
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_file ⇒ Object
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 |