Class: Chef::ResourceDefinition

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixin::ParamsValidate

#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 = Hash.new
  @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 paramaters we prototyped at the top. This method missing is as simple as it gets.



59
60
61
# File 'lib/chef/resource_definition.rb', line 59

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
# File 'lib/chef/resource_definition.rb', line 37

def define(resource_name, prototype_params=nil, &block)
  unless resource_name.kind_of?(Symbol)
    raise ArgumentError, "You must use a symbol when defining a new resource!"
  end
  @name = resource_name
  if prototype_params
    unless prototype_params.kind_of?(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
  true
end

#to_sObject



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

def to_s
  "#{name.to_s}"
end