Class: Chef::ResourceDefinition

Inherits:
Object
  • Object
show all
Includes:
Mixin::FromFile, Mixin::ParamsValidate
Defined in:
lib/chef/resource_definition.rb

Instance Attribute Summary collapse

Attributes included from Mixin::FromFile

#source_file

Instance Method Summary collapse

Methods included from Mixin::ParamsValidate

#lazy, #set_or_return, #validate

Methods included from Mixin::FromFile

#class_from_file, #from_file

Constructor Details

#initialize(node = nil) ⇒ ResourceDefinition

Returns a new instance of ResourceDefinition.



30
31
32
33
34
35
# File 'lib/chef/resource_definition.rb', line 30

def initialize(node = nil)
  @name = nil
  @params = {}
  @recipe = nil
  @node = node
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object

When we do the resource definition, we’re really just setting new values for the parameters we prototyped at the top. This method missing is as simple as it gets.



62
63
64
# File 'lib/chef/resource_definition.rb', line 62

def method_missing(symbol, *args)
  @params[symbol] = args.length == 1 ? args[0] : args
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



28
29
30
# File 'lib/chef/resource_definition.rb', line 28

def name
  @name
end

#nodeObject

Returns the value of attribute node.



28
29
30
# File 'lib/chef/resource_definition.rb', line 28

def node
  @node
end

#paramsObject

Returns the value of attribute params.



28
29
30
# File 'lib/chef/resource_definition.rb', line 28

def params
  @params
end

#recipeObject

Returns the value of attribute recipe.



28
29
30
# File 'lib/chef/resource_definition.rb', line 28

def recipe
  @recipe
end

Instance Method Details

#define(resource_name, prototype_params = nil, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/chef/resource_definition.rb', line 37

def define(resource_name, prototype_params = nil, &block)
  unless resource_name.is_a?(Symbol)
    raise ArgumentError, "You must use a symbol when defining a new resource!"
  end

  @name = resource_name
  if prototype_params
    unless prototype_params.is_a?(Hash)
      raise ArgumentError, "You must pass a hash as the prototype parameters for a definition."
    end

    @params = prototype_params
  end
  if Kernel.block_given?
    @recipe = block
  else
    raise ArgumentError, "You must pass a block to a definition."
  end
  Chef::DSL::Definitions.add_definition(name)
  true
end

#to_sObject



66
67
68
# File 'lib/chef/resource_definition.rb', line 66

def to_s
  (name).to_s
end