Class: RailsWizard::Recipe

Inherits:
Object
  • Object
show all
Extended by:
Comparable
Defined in:
lib/rails_wizard/recipe.rb

Constant Summary collapse

ATTRIBUTES =
%w(key args category name description template config exclusive tags run_before run_after requires)
DEFAULT_ATTRIBUTES =
{
  :category => 'other',
  :args => [],
  :tags => [],
  :run_after => [],
  :run_before => [],
  :requires => []
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.<=>(another) ⇒ Object



11
12
13
14
15
# File 'lib/rails_wizard/recipe.rb', line 11

def self.<=>(another)
  return -1 if another.run_after.include?(self.key) || self.run_before.include?(another.key)
  return 1 if another.run_before.include?(self.key) || self.run_after.include?(another.key)
  self.key <=> another.key
end

.attributesObject

The attributes hash containing any set values for



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

def self.attributes
  @attributes ||= DEFAULT_ATTRIBUTES.dup
end

.attributes=(hash) ⇒ Object



70
71
72
# File 'lib/rails_wizard/recipe.rb', line 70

def self.attributes=(hash)
  attributes.merge! hash
end

.compileObject



83
84
85
# File 'lib/rails_wizard/recipe.rb', line 83

def self.compile
  "# >#{"[ #{name} ]".center(75,'-')}<\n\n# #{description}\nsay_recipe '#{name}'\n\n#{template}\n"
end

.configObject



74
75
76
77
# File 'lib/rails_wizard/recipe.rb', line 74

def self.config
  return nil unless attributes[:config]
  RailsWizard::Config.new(attributes[:config])
end

.from_mongo(key) ⇒ Object



97
98
99
100
# File 'lib/rails_wizard/recipe.rb', line 97

def self.from_mongo(key)
  return key if key.respond_to?(:superclass) && key.superclass == RailsWizard::Recipe
  RailsWizard::Recipes[key]
end

.generate(key, template_or_file, attributes = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rails_wizard/recipe.rb', line 27

def self.generate(key, template_or_file, attributes = {})
  if template_or_file.respond_to?(:read)
    file = template_or_file.read
    parts = file.split(/^__END__$/)
    raise ArgumentError, "The recipe file must have YAML matter after an __END__" unless parts.size == 2
    template = parts.first.strip
    attributes = YAML.load(parts.last).inject({}) do |h,(k,v)|
      h[k.to_sym] = v
      h
    end.merge!(attributes)
  else
    template = template_or_file
  end
 
  recipe_class = Class.new(RailsWizard::Recipe) 
  recipe_class.attributes = attributes
  recipe_class.template = template
  recipe_class.key = key

  recipe_class
end

.get_bindingObject



102
103
104
# File 'lib/rails_wizard/recipe.rb', line 102

def self.get_binding
  binding
end

.to_mongo(value) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/rails_wizard/recipe.rb', line 88

def self.to_mongo(value)
  case value
    when Class
      value.key
    when String
      value
  end
end

Instance Method Details

#attributesObject



79
80
81
# File 'lib/rails_wizard/recipe.rb', line 79

def attributes
  self.class.attributes
end

#compileObject



86
# File 'lib/rails_wizard/recipe.rb', line 86

def compile; self.class.compile end