Class: Ree::TemplateDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/ree/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.



4
5
6
# File 'lib/ree/templates/template_detector.rb', line 4

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

Instance Method Details

#detect_template_folder(template_name) ⇒ Object

Raises:



8
9
10
11
12
13
14
15
16
# File 'lib/ree/templates/template_detector.rb', line 8

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 Ree::Error.new('Template does not exist') if template_path.nil?

  template_path
end

#gem_template_folder(template_name) ⇒ Object



18
19
20
# File 'lib/ree/templates/template_detector.rb', line 18

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

#project_template_folder(template_name) ⇒ Object



22
23
24
# File 'lib/ree/templates/template_detector.rb', line 22

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:



26
27
28
29
30
31
32
33
34
# File 'lib/ree/templates/template_detector.rb', line 26

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.exist?(file)}

    raise Ree::Error.new('Template does not exist') if file_path.nil?

  file_path
end