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.



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/hiptest-publisher/options_parser.rb', line 271

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.



269
270
271
# File 'lib/hiptest-publisher/options_parser.rb', line 269

def fallback_template
  @fallback_template
end

#forced_templatesObject (readonly)

Returns the value of attribute forced_templates.



269
270
271
# File 'lib/hiptest-publisher/options_parser.rb', line 269

def forced_templates
  @forced_templates
end

#overriden_templatesObject (readonly)

Returns the value of attribute overriden_templates.



269
270
271
# File 'lib/hiptest-publisher/options_parser.rb', line 269

def overriden_templates
  @overriden_templates
end

#template_dirsObject (readonly)

Returns the value of attribute template_dirs.



269
270
271
# File 'lib/hiptest-publisher/options_parser.rb', line 269

def template_dirs
  @template_dirs
end

Instance Method Details

#dirsObject



287
288
289
290
291
292
293
294
295
# File 'lib/hiptest-publisher/options_parser.rb', line 287

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



297
298
299
300
# File 'lib/hiptest-publisher/options_parser.rb', line 297

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



302
303
304
305
306
307
308
309
310
# File 'lib/hiptest-publisher/options_parser.rb', line 302

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



312
313
314
315
316
317
# File 'lib/hiptest-publisher/options_parser.rb', line 312

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_partialsObject



319
320
321
322
323
324
325
326
327
328
# File 'lib/hiptest-publisher/options_parser.rb', line 319

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