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


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

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|
      available_templates.push( DefaultTemplateFile.new(template, location) )
    end
  end
  available_templates
end

.checker_within_given_gems?(checker, gem_names) ⇒ Boolean

Returns:

  • (Boolean)

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

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


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

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


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

def inherited(base)
  template_checkers << base
end

.template_checkersObject


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

def template_checkers
  @template_checkers ||= []
end

.template_filename_for(filename) ⇒ Object


58
59
60
61
62
63
64
# File 'lib/dice_bag/available_templates.rb', line 58

def template_filename_for(filename)
  self.all.each do |template|
    if template.filename.include? filename
      return template.file
    end
  end
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