Class: RailsWizard::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_wizard/template.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(recipes, defaults = {}) ⇒ Template

Returns a new instance of Template.



5
6
7
8
# File 'lib/rails_wizard/template.rb', line 5

def initialize(recipes, defaults={})
  @recipes = recipes.map{|r| RailsWizard::Recipe.from_mongo(r)}
  @defaults = defaults
end

Instance Attribute Details

#defaultsObject (readonly)

Returns the value of attribute defaults.



3
4
5
# File 'lib/rails_wizard/template.rb', line 3

def defaults
  @defaults
end

#recipesObject (readonly)

Returns the value of attribute recipes.



3
4
5
# File 'lib/rails_wizard/template.rb', line 3

def recipes
  @recipes
end

Class Method Details

.render(template_name, binding = nil) ⇒ Object



14
15
16
17
# File 'lib/rails_wizard/template.rb', line 14

def self.render(template_name, binding = nil)
  erb = ERB.new(File.open(template_root + '/' + template_name + '.erb').read)
  erb.result(binding)
end

.template_rootObject



10
11
12
# File 'lib/rails_wizard/template.rb', line 10

def self.template_root
  File.dirname(__FILE__) + '/../../templates'
end

Instance Method Details

#argsObject



66
67
68
# File 'lib/rails_wizard/template.rb', line 66

def args
  recipes.map(&:args).uniq
end

#compileObject



62
63
64
# File 'lib/rails_wizard/template.rb', line 62

def compile
  render 'layout', binding
end

#custom_codeObject



71
# File 'lib/rails_wizard/template.rb', line 71

def custom_code; nil end

#custom_code?Boolean

Returns:

  • (Boolean)


70
# File 'lib/rails_wizard/template.rb', line 70

def custom_code?; false end

#recipe_classesObject



40
41
42
# File 'lib/rails_wizard/template.rb', line 40

def recipe_classes
  @recipe_classes ||= recipes.map{|r| RailsWizard::Recipe.from_mongo(r)}
end

#recipes_with_dependenciesObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rails_wizard/template.rb', line 44

def recipes_with_dependencies
  @recipes_with_dependencies ||= recipe_classes
  
  added_more = false
  for recipe in recipe_classes
    recipe.requires.each do |requirement|
      requirement = RailsWizard::Recipe.from_mongo(requirement)
      count = @recipes_with_dependencies.size
      (@recipes_with_dependencies << requirement).uniq!
      unless @recipes_with_dependencies.size == count
        added_more = true
      end
    end
  end

  added_more ? recipes_with_dependencies : @recipes_with_dependencies
end

#render(template_name, binding = nil) ⇒ Object



18
# File 'lib/rails_wizard/template.rb', line 18

def render(template_name, binding = nil); self.class.render(template_name, binding) end

#resolve_recipesObject

Sort the recipes list taking ‘run_after’ directives into account.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rails_wizard/template.rb', line 22

def resolve_recipes
  @resolve_recipes ||= begin
    list = recipes_with_dependencies

    for i in 0...list.size
      after_keys = list[i+1..-1].map { |r| r.key }

      if (list[i].run_after & after_keys).any?
        list.push list.slice!(i)
        redo
      end
    end

    list.each {|recipe| recipe.defaults = defaults[recipe.key] }
    list
  end
end