Class: RailsWizard::Template
- Inherits:
-
Object
- Object
- RailsWizard::Template
- Defined in:
- lib/rails_wizard/template.rb
Instance Attribute Summary collapse
-
#defaults ⇒ Object
readonly
Returns the value of attribute defaults.
-
#recipes ⇒ Object
readonly
Returns the value of attribute recipes.
Class Method Summary collapse
Instance Method Summary collapse
- #args ⇒ Object
- #compile ⇒ Object
- #custom_code ⇒ Object
- #custom_code? ⇒ Boolean
-
#initialize(recipes, defaults = {}) ⇒ Template
constructor
A new instance of Template.
- #recipe_classes ⇒ Object
- #recipes_with_dependencies ⇒ Object
- #render(template_name, binding = nil) ⇒ Object
-
#resolve_recipes ⇒ Object
Sort the recipes list taking ‘run_after’ directives into account.
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
#defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
3 4 5 |
# File 'lib/rails_wizard/template.rb', line 3 def defaults @defaults end |
#recipes ⇒ Object (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_root ⇒ Object
10 11 12 |
# File 'lib/rails_wizard/template.rb', line 10 def self.template_root File.dirname(__FILE__) + '/../../templates' end |
Instance Method Details
#args ⇒ Object
66 67 68 |
# File 'lib/rails_wizard/template.rb', line 66 def args recipes.map(&:args).uniq end |
#compile ⇒ Object
62 63 64 |
# File 'lib/rails_wizard/template.rb', line 62 def compile render 'layout', binding end |
#custom_code ⇒ Object
71 |
# File 'lib/rails_wizard/template.rb', line 71 def custom_code; nil end |
#custom_code? ⇒ Boolean
70 |
# File 'lib/rails_wizard/template.rb', line 70 def custom_code?; false end |
#recipe_classes ⇒ Object
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_dependencies ⇒ Object
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_recipes ⇒ Object
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 |