Class: Skellington::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/skellington/template.rb

Instance Method Summary collapse

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_templatesObject



17
18
19
# File 'lib/skellington/template.rb', line 17

def common_templates
  File.join File.dirname(__FILE__), '..', 'common', 'templates'
end

#outpathObject



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

#pathObject



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_dirObject



21
22
23
# File 'lib/skellington/template.rb', line 21

def templates_dir
  File.join File.dirname(__FILE__), '..', @generator.framework, 'templates'
end

#to_sObject



46
47
48
# File 'lib/skellington/template.rb', line 46

def to_s
  Erubis::Eruby.new(path).evaluate(gen: @generator)
end

#writeObject



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