Class: Eql::TemplateLoader
- Inherits:
-
Object
- Object
- Eql::TemplateLoader
- Extended by:
- Forwardable
- Defined in:
- lib/eql/template_loader.rb
Overview
Loader class loads templates and caches them.
Instance Attribute Summary collapse
- #builder ⇒ Eql::Builder readonly
Class Method Summary collapse
-
.cache ⇒ Hash{String => String}
Templates cache.
Instance Method Summary collapse
-
#initialize(builder) ⇒ TemplateLoader
constructor
A new instance of TemplateLoader.
-
#load_file(path) ⇒ String
private
Load file’s content.
-
#load_template(name) ⇒ String
Load builder template.
-
#resolve_path(file) ⇒ String
private
Resolve file’s path.
-
#template_path(file) ⇒ Array<String>
private
File template to find.
Constructor Details
#initialize(builder) ⇒ TemplateLoader
Returns a new instance of TemplateLoader.
16 17 18 |
# File 'lib/eql/template_loader.rb', line 16 def initialize(builder) @builder = builder end |
Instance Attribute Details
#builder ⇒ Eql::Builder (readonly)
11 12 13 |
# File 'lib/eql/template_loader.rb', line 11 def builder @builder end |
Class Method Details
.cache ⇒ Hash{String => String}
Templates cache
88 89 90 |
# File 'lib/eql/template_loader.rb', line 88 def self.cache @cache ||= {} end |
Instance Method Details
#load_file(path) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Load file’s content
42 43 44 |
# File 'lib/eql/template_loader.rb', line 42 def load_file(path) File.read(path) end |
#load_template(name) ⇒ String
Load builder template
27 28 29 30 31 |
# File 'lib/eql/template_loader.rb', line 27 def load_template(name) path = resolve_path(name) return load_file(path) unless Eql.config.cache_templates? cache[path] ||= load_file(path) end |
#resolve_path(file) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Resolve file’s path
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/eql/template_loader.rb', line 72 def resolve_path(file) paths = template_path(file) paths.each do |path| filepath = Dir.glob(path).first return filepath if filepath.present? end raise "Unable to find query template with #{paths.inspect} location" end |
#template_path(file) ⇒ Array<String>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
File template to find
55 56 57 58 59 |
# File 'lib/eql/template_loader.rb', line 55 def template_path(file) @builder.path.map do |path| [File.join(path, file.to_s), @builder.adapter.extension].join end end |