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
- .all ⇒ Object
- .load_all ⇒ Object
- .load_from_directory(dir) ⇒ Object
- .load_one_from_path(path) ⇒ Object
Instance Method Summary collapse
- #generate(output_path) ⇒ Object
-
#initialize(path) ⇒ Template
constructor
A new instance of Template.
Constructor Details
#initialize(path) ⇒ Template
Returns a new instance of Template.
29 30 31 32 33 34 35 |
# File 'lib/singer/template.rb', line 29 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.
27 28 29 |
# File 'lib/singer/template.rb', line 27 def files @files end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
27 28 29 |
# File 'lib/singer/template.rb', line 27 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 |
.load_one_from_path(path) ⇒ Object
22 23 24 25 |
# File 'lib/singer/template.rb', line 22 def self.load_one_from_path(path) Singer.configuration.template_name = File.basename(path) all[Singer.configuration.template_name] = new(path) end |
Instance Method Details
#generate(output_path) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/singer/template.rb', line 37 def generate(output_path) files.each do |file| Singer.configuration.template_file_name_original = file target_file = Paths.output_path(output_path, file) Singer.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 |