summaryrefslogtreecommitdiff
path: root/lib/chef/resource_definition.rb
blob: 4bc048188f8fa378f26307a19361b8c0a0e0e7a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
#
# Author:: Adam Jacob (<adam@hjksolutions.com>)
# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
# License:: GNU General Public License version 2 or later
# 
# This program and entire repository is free software; you can
# redistribute it and/or modify it under the terms of the GNU 
# General Public License as published by the Free Software 
# Foundation; either version 2 of the License, or any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#

require File.join(File.dirname(__FILE__), "mixin", "from_file")
require File.join(File.dirname(__FILE__), "mixin", "params_validate")


class Chef
  class ResourceDefinition
    
    include Chef::Mixin::FromFile
    include Chef::Mixin::ParamsValidate
    
    attr_accessor :name, :params, :recipe
    
    def initialize
      @name = nil
      @params = Hash.new
      @recipe = nil
    end
    
    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
    
    # 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.
    def method_missing(symbol, *args)
      @params[symbol] = args.length == 1 ? args[0] : args
    end
    
  end
end