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



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

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)


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

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



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

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

.inherited(base) ⇒ Object



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

def inherited(base)
  template_checkers << base
end

.template_checkersObject



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

def template_checkers
  @template_checkers ||= []
end

.template_filename_for(filename) ⇒ Object



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

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.



13
14
15
# File 'lib/dice_bag/available_templates.rb', line 13

def templates_location
  "config"
end