Class: TemplateFinder
- Inherits:
-
Object
- Object
- TemplateFinder
- Defined in:
- lib/hiptest-publisher/options_parser.rb
Instance Attribute Summary collapse
-
#fallback_template ⇒ Object
readonly
Returns the value of attribute fallback_template.
-
#forced_templates ⇒ Object
readonly
Returns the value of attribute forced_templates.
-
#overriden_templates ⇒ Object
readonly
Returns the value of attribute overriden_templates.
-
#template_dirs ⇒ Object
readonly
Returns the value of attribute template_dirs.
Instance Method Summary collapse
- #dirs ⇒ Object
- #get_compiled_handlebars(template_name) ⇒ Object
- #get_template_by_name(name) ⇒ Object
- #get_template_path(template_name) ⇒ Object
-
#initialize(template_dirs: nil, overriden_templates: nil, indentation: ' ', forced_templates: nil, fallback_template: nil) ⇒ TemplateFinder
constructor
A new instance of TemplateFinder.
- #register_partials ⇒ Object
Constructor Details
#initialize(template_dirs: nil, overriden_templates: nil, indentation: ' ', forced_templates: nil, fallback_template: nil) ⇒ TemplateFinder
Returns a new instance of TemplateFinder.
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/hiptest-publisher/options_parser.rb', line 272 def initialize( template_dirs: nil, overriden_templates: nil, indentation: ' ', forced_templates: nil, fallback_template: nil, **) @template_dirs = template_dirs || [] @overriden_templates = overriden_templates @compiled_handlebars = {} @template_path_by_name = {} @forced_templates = forced_templates || {} @fallback_template = fallback_template @context = {indentation: indentation} end |
Instance Attribute Details
#fallback_template ⇒ Object (readonly)
Returns the value of attribute fallback_template.
270 271 272 |
# File 'lib/hiptest-publisher/options_parser.rb', line 270 def fallback_template @fallback_template end |
#forced_templates ⇒ Object (readonly)
Returns the value of attribute forced_templates.
270 271 272 |
# File 'lib/hiptest-publisher/options_parser.rb', line 270 def forced_templates @forced_templates end |
#overriden_templates ⇒ Object (readonly)
Returns the value of attribute overriden_templates.
270 271 272 |
# File 'lib/hiptest-publisher/options_parser.rb', line 270 def overriden_templates @overriden_templates end |
#template_dirs ⇒ Object (readonly)
Returns the value of attribute template_dirs.
270 271 272 |
# File 'lib/hiptest-publisher/options_parser.rb', line 270 def template_dirs @template_dirs end |
Instance Method Details
#dirs ⇒ Object
288 289 290 291 292 293 294 295 296 |
# File 'lib/hiptest-publisher/options_parser.rb', line 288 def dirs @dirs ||= begin search_dirs = template_dirs.map { |path| "#{hiptest_publisher_path}/lib/templates/#{path}" } search_dirs.unshift(overriden_templates) if overriden_templates search_dirs end end |
#get_compiled_handlebars(template_name) ⇒ Object
298 299 300 301 |
# File 'lib/hiptest-publisher/options_parser.rb', line 298 def (template_name) template_path = get_template_path(template_name) @compiled_handlebars[template_path] ||= .compile(File.read(template_path)) end |
#get_template_by_name(name) ⇒ Object
303 304 305 306 307 308 309 310 311 |
# File 'lib/hiptest-publisher/options_parser.rb', line 303 def get_template_by_name(name) return if name.nil? name = forced_templates.fetch(name, name) dirs.each do |path| template_path = File.join(path, "#{name}.hbs") return template_path if File.file?(template_path) end nil end |
#get_template_path(template_name) ⇒ Object
313 314 315 316 317 318 |
# File 'lib/hiptest-publisher/options_parser.rb', line 313 def get_template_path(template_name) unless @template_path_by_name.has_key?(template_name) @template_path_by_name[template_name] = get_template_by_name(template_name) || get_template_by_name(@fallback_template) end @template_path_by_name[template_name] or raise ArgumentError.new("no template with name #{template_name} in dirs #{dirs}") end |
#register_partials ⇒ Object
320 321 322 323 324 325 326 327 328 329 |
# File 'lib/hiptest-publisher/options_parser.rb', line 320 def register_partials dirs.reverse_each do |path| next unless File.directory?(path) Dir.entries(path).select do |file_name| file_path = File.join(path, file_name) next unless File.file?(file_path) && file_name.start_with?('_') @handlebars.register_partial(file_name[1..-5], File.read(file_path)) end end end |