Class: TemplateFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/hiptest-publisher/options_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_dirs: nil, overriden_templates: nil, indentation: ' ', forced_templates: nil, fallback_template: nil) ⇒ TemplateFinder

Returns a new instance of TemplateFinder.



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/hiptest-publisher/options_parser.rb', line 377

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_templateObject (readonly)

Returns the value of attribute fallback_template.



375
376
377
# File 'lib/hiptest-publisher/options_parser.rb', line 375

def fallback_template
  @fallback_template
end

#forced_templatesObject (readonly)

Returns the value of attribute forced_templates.



375
376
377
# File 'lib/hiptest-publisher/options_parser.rb', line 375

def forced_templates
  @forced_templates
end

#overriden_templatesObject (readonly)

Returns the value of attribute overriden_templates.



375
376
377
# File 'lib/hiptest-publisher/options_parser.rb', line 375

def overriden_templates
  @overriden_templates
end

#template_dirsObject (readonly)

Returns the value of attribute template_dirs.



375
376
377
# File 'lib/hiptest-publisher/options_parser.rb', line 375

def template_dirs
  @template_dirs
end

Instance Method Details

#dirsObject



393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/hiptest-publisher/options_parser.rb', line 393

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



408
409
410
411
# File 'lib/hiptest-publisher/options_parser.rb', line 408

def get_compiled_handlebars(template_name)
  template_path = get_template_path(template_name)
  @compiled_handlebars[template_path] ||= handlebars.compile(File.read(template_path))
end

#get_template_by_name(name) ⇒ Object



413
414
415
416
417
418
419
420
421
# File 'lib/hiptest-publisher/options_parser.rb', line 413

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



423
424
425
426
427
428
# File 'lib/hiptest-publisher/options_parser.rb', line 423

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_partialsObject



430
431
432
433
434
435
436
437
438
439
# File 'lib/hiptest-publisher/options_parser.rb', line 430

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