diff options
author | Adam Jacob <adam@hjksolutions.com> | 2008-04-07 18:14:25 -0700 |
---|---|---|
committer | Adam Jacob <adam@hjksolutions.com> | 2008-04-07 18:14:25 -0700 |
commit | 434f25ba07b5c0c50baa1e15b14a945bba3c3c3b (patch) | |
tree | 03085c689549a62d546d87e6484dde8756da2d23 /lib/chef/recipe.rb | |
parent | f543b509ba61dd347512e8a9e3153a49a2a8cb6b (diff) | |
download | chef-434f25ba07b5c0c50baa1e15b14a945bba3c3c3b.tar.gz |
Adding the Params::Validate mixin, refactored Chef::Config to be a singleton, Implemented require_recipe
Diffstat (limited to 'lib/chef/recipe.rb')
-rw-r--r-- | lib/chef/recipe.rb | 49 |
1 files changed, 34 insertions, 15 deletions
diff --git a/lib/chef/recipe.rb b/lib/chef/recipe.rb index 81fb979de2..af1cbd2d77 100644 --- a/lib/chef/recipe.rb +++ b/lib/chef/recipe.rb @@ -26,30 +26,47 @@ class Chef include Chef::Mixin::FromFile attr_accessor :cookbook_name, :recipe_name, :recipe, :node, :collection, - :definitions, :config, :params + :definitions, :params, :cookbook_loader - def initialize(cookbook_name, recipe_name, node, collection=nil, definitions=nil, config=nil) + def initialize(cookbook_name, recipe_name, node, collection=nil, definitions=nil, cookbook_loader=nil) @cookbook_name = cookbook_name @recipe_name = recipe_name @node = node + if collection @collection = collection else @collection = Chef::ResourceCollection.new() end - if config - @config = config - else - @config = Chef::Config.new() - end + if definitions @definitions = definitions else @definitions = Hash.new end + + if cookbook_loader + @cookbook_loader = cookbook_loader + else + @cookbook_loader = Chef::CookbookLoader.new() + end + @params = Hash.new end + def require_recipe(*args) + args.flatten.each do |recipe| + rmatch = recipe.match(/(.+?)::(.+)/) + if rmatch + cookbook = @cookbook_loader[rmatch[1]] + cookbook.load_recipe(rmatch[2], @node, @collection, @definitions, @cookbook_loader) + else + cookbook = @cookbook_loader[recipe] + cookbook.load_recipe("default", @node, @collection, @definitions, @cookbook_loader) + end + end + end + def resources(*args) @collection.resources(*args) end @@ -58,26 +75,28 @@ class Chef resource = nil # If we have a definition that matches, we want to use that instead. This should # let you do some really crazy over-riding of "native" types, if you really want - # to. + # to. if @definitions.has_key?(method_symbol) new_def = @definitions[method_symbol].dup new_def.instance_eval(&block) - new_recipe = Chef::Recipe.new(@cookbook_name, @recipe_name, @node, @collection, @definitions, @config) + new_recipe = Chef::Recipe.new(@cookbook_name, @recipe_name, @node, @collection, @definitions, @cookbook_loader) new_recipe.params = new_def.params new_recipe.instance_eval(&new_def.recipe) else method_name = method_symbol.to_s # Otherwise, we're rocking the regular resource call route. rname = nil - case method_name - when /^(.+)_(.+)$/ - rname = "Chef::Resource::#{$1.capitalize}#{$2.capitalize}" - when /^(.+)$/ - rname = "Chef::Resource::#{$1.capitalize}" + mn = method_name.match(/^(.+)_(.+)$/) + if mn + rname = "Chef::Resource::#{mn[1].capitalize}#{mn[2].capitalize}" + else + short_match = method_name.match(/^(.+)$/) + if short_match + rname = "Chef::Resource::#{short_match[1].capitalize}" + end end begin args << @collection - args << @config resource = eval(rname).new(*args) resource.params = @params resource.instance_eval(&block) |