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.
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/hiptest-publisher/options_parser.rb', line 365 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.
363 364 365 |
# File 'lib/hiptest-publisher/options_parser.rb', line 363 def fallback_template @fallback_template end |
#forced_templates ⇒ Object (readonly)
Returns the value of attribute forced_templates.
363 364 365 |
# File 'lib/hiptest-publisher/options_parser.rb', line 363 def forced_templates @forced_templates end |
#overriden_templates ⇒ Object (readonly)
Returns the value of attribute overriden_templates.
363 364 365 |
# File 'lib/hiptest-publisher/options_parser.rb', line 363 def overriden_templates @overriden_templates end |
#template_dirs ⇒ Object (readonly)
Returns the value of attribute template_dirs.
363 364 365 |
# File 'lib/hiptest-publisher/options_parser.rb', line 363 def template_dirs @template_dirs end |
Instance Method Details
#dirs ⇒ Object
381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/hiptest-publisher/options_parser.rb', line 381 def dirs @dirs ||= begin search_dirs = [] # search in overriden template base dir first search_dirs << overriden_templates if overriden_templates template_dirs.each {|template_dir| # search template paths in overriden_templates search_dirs << "#{overriden_templates}/#{template_dir}" if overriden_templates # search template paths in hiptest_publisher search_dirs << "#{hiptest_publisher_path}/lib/templates/#{template_dir}" } search_dirs end end |
#get_compiled_handlebars(template_name) ⇒ Object
396 397 398 399 |
# File 'lib/hiptest-publisher/options_parser.rb', line 396 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
401 402 403 404 405 406 407 408 409 |
# File 'lib/hiptest-publisher/options_parser.rb', line 401 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
411 412 413 414 415 416 |
# File 'lib/hiptest-publisher/options_parser.rb', line 411 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
418 419 420 421 422 423 424 425 426 427 |
# File 'lib/hiptest-publisher/options_parser.rb', line 418 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 |