Class: Rdm::Templates::TemplateDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/rdm/templates/template_detector.rb

Constant Summary collapse

DEFAULT_TEMPLATES_DIRECTORY =
File.expand_path(__dir__)

Instance Method Summary collapse

Constructor Details

#initialize(project_path) ⇒ TemplateDetector

Returns a new instance of TemplateDetector.



6
7
8
# File 'lib/rdm/templates/template_detector.rb', line 6

def initialize(project_path)
  @all_templates_directory ||= File.join(project_path, ".rdm", "templates")
end

Instance Method Details

#detect_template_folder(template_name) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/rdm/templates/template_detector.rb', line 11

def detect_template_folder(template_name)
  template_path = [@all_templates_directory, DEFAULT_TEMPLATES_DIRECTORY]
    .map    {|dir| File.join(dir, template_name.to_s)}
    .detect  {|dir| Dir.exist?(dir)}

  raise Rdm::Errors::TemplateDoesNotExist if template_path.nil?

  template_path
end

#gem_template_folder(template_name) ⇒ Object



21
22
23
# File 'lib/rdm/templates/template_detector.rb', line 21

def gem_template_folder(template_name)
  File.join(DEFAULT_TEMPLATES_DIRECTORY, template_name.to_s)
end

#project_template_folder(template_name) ⇒ Object



25
26
27
# File 'lib/rdm/templates/template_detector.rb', line 25

def project_template_folder(template_name)
  File.join(@all_templates_directory, template_name.to_s)
end

#template_file_path(template_name, relative_path) ⇒ Object

Raises:

  • (Rdm::Errors::TemplateFileDoesNotExists)


29
30
31
32
33
34
35
36
37
# File 'lib/rdm/templates/template_detector.rb', line 29

def template_file_path(template_name, relative_path)
  file_path = [detect_template_folder(template_name), DEFAULT_TEMPLATES_DIRECTORY]
    .map {|folder| File.join(folder, relative_path)}
    .detect {|file| File.exists?(file)}

  raise Rdm::Errors::TemplateFileDoesNotExists if file_path.nil?

  file_path
end