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_language_configs ⇒ Object
readonly
Returns the value of attribute overriden_language_configs.
-
#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, overriden_language_configs: 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, overriden_language_configs: nil, indentation: ' ', forced_templates: nil, fallback_template: nil) ⇒ TemplateFinder
Returns a new instance of TemplateFinder.
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
# File 'lib/hiptest-publisher/options_parser.rb', line 399 def initialize( template_dirs: nil, overriden_templates: nil, overriden_language_configs: 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.
397 398 399 |
# File 'lib/hiptest-publisher/options_parser.rb', line 397 def fallback_template @fallback_template end |
#forced_templates ⇒ Object (readonly)
Returns the value of attribute forced_templates.
397 398 399 |
# File 'lib/hiptest-publisher/options_parser.rb', line 397 def forced_templates @forced_templates end |
#overriden_language_configs ⇒ Object (readonly)
Returns the value of attribute overriden_language_configs.
397 398 399 |
# File 'lib/hiptest-publisher/options_parser.rb', line 397 def overriden_language_configs @overriden_language_configs end |
#overriden_templates ⇒ Object (readonly)
Returns the value of attribute overriden_templates.
397 398 399 |
# File 'lib/hiptest-publisher/options_parser.rb', line 397 def overriden_templates @overriden_templates end |
#template_dirs ⇒ Object (readonly)
Returns the value of attribute template_dirs.
397 398 399 |
# File 'lib/hiptest-publisher/options_parser.rb', line 397 def template_dirs @template_dirs end |
Instance Method Details
#dirs ⇒ Object
416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'lib/hiptest-publisher/options_parser.rb', line 416 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
431 432 433 434 |
# File 'lib/hiptest-publisher/options_parser.rb', line 431 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
436 437 438 439 440 441 442 443 444 |
# File 'lib/hiptest-publisher/options_parser.rb', line 436 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
446 447 448 449 450 451 |
# File 'lib/hiptest-publisher/options_parser.rb', line 446 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(I18n.t('errors.template_not_found', template_name: template_name, dirs: dirs)) end |
#register_partials ⇒ Object
453 454 455 456 457 458 459 460 461 462 |
# File 'lib/hiptest-publisher/options_parser.rb', line 453 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 |