Class: Skellington::Template
- Inherits:
-
Object
- Object
- Skellington::Template
- Defined in:
- lib/skellington/template.rb
Instance Method Summary collapse
- #common_templates ⇒ Object
-
#initialize(name, generator) ⇒ Template
constructor
A new instance of Template.
- #outpath ⇒ Object
- #path ⇒ Object
- #templates_dir ⇒ Object
- #to_s ⇒ Object
- #write ⇒ Object
Constructor Details
#initialize(name, generator) ⇒ Template
Returns a new instance of Template.
3 4 5 6 |
# File 'lib/skellington/template.rb', line 3 def initialize name, generator @name = name @generator = generator end |
Instance Method Details
#common_templates ⇒ Object
17 18 19 |
# File 'lib/skellington/template.rb', line 17 def common_templates File.join File.dirname(__FILE__), '..', 'common', 'templates' end |
#outpath ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/skellington/template.rb', line 8 def outpath @outpath ||= begin subs = @generator.files[@name]['outpath'].split '/' @outpath = "#{@generator.path}/#{@generator.send(subs[1].to_sym)}/#{@name.gsub(subs[0], @generator.send(subs[1].to_sym))}" rescue NoMethodError @outpath = "#{@generator.path}/#{@generator.wormname}/#{@name}" end end |
#path ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/skellington/template.rb', line 34 def path [ "#{templates_dir}/#{@name}", "#{common_templates}/#{@generator.files.dig(@name, 'common')}", "#{common_templates}/#{@name}" ].each do |maybe_path| return File.read(File.open(maybe_path)) if File.file? maybe_path end '' end |
#templates_dir ⇒ Object
21 22 23 |
# File 'lib/skellington/template.rb', line 21 def templates_dir File.join File.dirname(__FILE__), '..', @generator.framework, 'templates' end |
#to_s ⇒ Object
46 47 48 |
# File 'lib/skellington/template.rb', line 46 def to_s Erubis::Eruby.new(path).evaluate(gen: @generator) end |
#write ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/skellington/template.rb', line 25 def write print "Generating #{Skellington.unslash outpath}..." FileUtils.mkdir_p File.dirname outpath File.open outpath, 'w' do |f| f.write self end puts 'done' end |