Module: Chef::Mixin::RecipeDefinitionDSLCore

Includes:
ConvertToClassName
Included in:
Provider, Recipe
Defined in:
lib/chef/mixin/recipe_definition_dsl_core.rb

Instance Method Summary collapse

Methods included from ConvertToClassName

#convert_to_class_name, #filename_to_qualified_string

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol, *args, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/chef/mixin/recipe_definition_dsl_core.rb', line 30

def method_missing(method_symbol, *args, &block)
  # 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. 
  if @definitions.has_key?(method_symbol)
    # This dupes the high level object, but we still need to dup the params
    new_def = @definitions[method_symbol].dup
    new_def.params = new_def.params.dup
    new_def.node = @node
    # This sets up the parameter overrides
    new_def.instance_eval(&block) if block
    new_recipe = Chef::Recipe.new(@cookbook_name, @recipe_name, @node, @collection, @definitions, @cookbook_loader)
    new_recipe.params = new_def.params
    new_recipe.params[:name] = args[0]
    new_recipe.instance_eval(&new_def.recipe)
  else
    # Otherwise, we're rocking the regular resource call route.
    method_name = method_symbol.to_s
    rname = convert_to_class_name(method_name)
    
    # If we have a resource like this one, we want to steal its state
    resource = begin
                 args << @collection
                 args << @node
                 Chef::Resource.const_get(rname).new(*args)
               rescue NameError => e
                 if e.to_s =~ /Chef::Resource/
                   raise NameError, "Cannot find #{rname} for #{method_name}\nOriginal exception: #{e.class}: #{e.message}"
                 else
                   raise e
                 end
               end
    resource.load_prior_resource
    resource.cookbook_name = @cookbook_name
    resource.recipe_name = @recipe_name
    resource.params = @params
    # Determine whether this resource is being created in the context of an enclosing Provider
    resource.enclosing_provider = self.is_a?(Chef::Provider) ? self : nil
    resource.instance_eval(&block) if block

    @collection.insert(resource)
    resource
  end
end