Class: DiceBag::AvailableTemplates

Inherits:
Object
  • Object
show all
Defined in:
lib/dice_bag/available_templates.rb

Direct Known Subclasses

CommonTemplatesBag

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all(gem_names = []) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dice_bag/available_templates.rb', line 41

def all(gem_names = [])
  # all the classes than inherit from us in the ruby runtime
  available_templates = []

  template_checkers.each do |template_checker|
    checker = template_checker.new
    next if !gem_names.empty? && !checker_within_given_gems?(checker, gem_names)

    location = checker.templates_location
    checker.templates.each do |template, save_as|
      available_templates.push(DefaultTemplateFile.new(template, location, save_as))
    end
  end
  available_templates
end

.checker_within_given_gems?(checker, gem_names) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
# File 'lib/dice_bag/available_templates.rb', line 24

def checker_within_given_gems?(checker, gem_names)
  checker_file = checker.method(:templates).source_location[0]
  gem_specs.each do |name, location|
    return true if checker_file.start_with?(location) && gem_names.include?(name)
  end

  false
end

.gem_specsObject



16
17
18
19
20
21
22
# File 'lib/dice_bag/available_templates.rb', line 16

def gem_specs
  @gem_specs ||= begin
    Gem::Specification.sort.each_with_object({}) do |spec, hsh|
      hsh[spec.name] = spec.full_gem_path
    end
  end
end

.inherited(base) ⇒ Object



37
38
39
# File 'lib/dice_bag/available_templates.rb', line 37

def inherited(base)
  template_checkers << base
end

.template_checkersObject



33
34
35
# File 'lib/dice_bag/available_templates.rb', line 33

def template_checkers
  @template_checkers ||= []
end

.template_filename_for(filename) ⇒ Object



57
58
59
# File 'lib/dice_bag/available_templates.rb', line 57

def template_filename_for(filename)
  all.find { |template| template.filename.include?(filename) }
end

Instance Method Details

#templates_locationObject

By default the final location for any template will be the config directory. If any template ‘plugin’ wants to overwrite the directory where its template will be written it needs to overwrite this method and return a string with the new location as relative path inside the project.



11
12
13
# File 'lib/dice_bag/available_templates.rb', line 11

def templates_location
  "config"
end