Class: Singer::Template
- Inherits:
-
Object
- Object
- Singer::Template
- Defined in:
- lib/singer/template.rb
Overview
describes one type of code that Singer can generate
Instance Attribute Summary collapse
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #generate(output_path) ⇒ Object
-
#initialize(path) ⇒ Template
constructor
A new instance of Template.
Constructor Details
#initialize(path) ⇒ Template
24 25 26 27 28 29 30 |
# File 'lib/singer/template.rb', line 24 def initialize(path) @path = path Dir.chdir(path) do # we could use only Dir[base: path], but all the code uses relative paths entries = Dir.glob('**/*', flags: File::FNM_DOTMATCH) @files = entries.select{ File.file?(_1) } end end |
Instance Attribute Details
#files ⇒ Object (readonly)
Returns the value of attribute files.
22 23 24 |
# File 'lib/singer/template.rb', line 22 def files @files end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
22 23 24 |
# File 'lib/singer/template.rb', line 22 def path @path end |
Class Method Details
.all ⇒ Object
6 7 8 |
# File 'lib/singer/template.rb', line 6 def self.all @all ||= load_all end |
.load_all ⇒ Object
10 11 12 13 14 |
# File 'lib/singer/template.rb', line 10 def self.load_all [Paths.templates_from_gem, Paths.templates_from_user] .map{ load_from_directory(_1) } .reduce(&:merge) end |
.load_from_directory(dir) ⇒ Object
16 17 18 19 20 |
# File 'lib/singer/template.rb', line 16 def self.load_from_directory(dir) Dir[File.join(dir, '*')].to_h do |template_dir| [File.basename(template_dir), new(template_dir)] end end |
Instance Method Details
#generate(output_path) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/singer/template.rb', line 32 def generate(output_path) files.each do |file| CONFIGURATION.template_file_name_original = file target_file = Paths.output_path(output_path, file) CONFIGURATION.template_file_name_actual = target_file FileUtils.mkdir_p(File.dirname(target_file)) source_file = File.join(path, file) TemplatingMethods.send(:erb, source_file, target_file) end end |