diff options
author | Adam Jacob <adam@hjksolutions.com> | 2008-03-10 00:38:43 -0700 |
---|---|---|
committer | Adam Jacob <adam@hjksolutions.com> | 2008-03-10 00:38:43 -0700 |
commit | 3dd77b260393bae93bb27677e3f8a45f407981c1 (patch) | |
tree | d017427ddcd5733caf7c0ceeec8ea72abbb107fa /lib/chef/resource.rb | |
parent | b4beed548959ca63365ca3d799c59a155047b5ff (diff) | |
download | chef-3dd77b260393bae93bb27677e3f8a45f407981c1.tar.gz |
Added rcov coverage, lots of tests, definitions, node support
Diffstat (limited to 'lib/chef/resource.rb')
-rw-r--r-- | lib/chef/resource.rb | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb new file mode 100644 index 0000000000..2d7b90ed7f --- /dev/null +++ b/lib/chef/resource.rb @@ -0,0 +1,123 @@ +# +# 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", "check_helper") +require 'yaml' + +class Chef + class Resource + + include Chef::Mixin::CheckHelper + + attr_accessor :tag, :actions, :config, :params + attr_reader :name, :noop, :resource_name, :collection, :notifies, :subscribes + + def initialize(name, collection=nil, config=nil) + @name = name + if collection + @collection = collection + else + @collection = Chef::ResourceCollection.new() + end + if config + @config = config + else + @config = Chef::Config.new() + end + @tag = [ name.to_s ] + @noop = nil + @before = nil + @actions = Hash.new + @params = Hash.new + end + + def name(name=nil) + set_if_args(@name, name) do + raise ArgumentError, "name must be a string!" unless name.kind_of?(String) + @name = name + end + end + + def noop(tf=nil) + set_if_args(@noop, tf) do + raise ArgumentError, "noop must be true or false!" unless tf == true || tf == false + @noop = tf + end + end + + def notifies(action, resources, timing=:delayed) + timing = check_timing(timing) + rarray = resources.kind_of?(Array) ? resources : [ resources ] + rarray.each do |resource| + action_sym = action.to_sym + if @actions.has_key?(action_sym) + @actions[action_sym][timing] << resource + else + @actions[action_sym] = Hash.new + @actions[action_sym][:delayed] = Array.new + @actions[action_sym][:immediate] = Array.new + @actions[action_sym][timing] << resource + end + end + true + end + + def resources(*args) + @collection.resources(*args) + end + + def subscribes(action, resources, timing=:delayed) + timing = check_timing(timing) + rarray = resources.kind_of?(Array) ? resources : [ resources ] + rarray.each do |resource| + action_sym = action.to_sym + if resource.actions.has_key?(action_sym) + resource.actions[action_sym][timing] << self + else + resource.actions[action_sym] = Hash.new + resource.actions[action_sym][:delayed] = Array.new + resource.actions[action_sym][:immediate] = Array.new + resource.actions[action_sym][timing] << self + end + end + true + end + + def is(*args) + return *args + end + + def to_s + "#{@resource_name}[#{@name}]" + end + + private + + def check_timing(timing) + unless timing == :delayed || timing == :immediate || timing == :immediately + raise ArgumentError, "Timing must be :delayed or :immediate(ly), you said #{timing}" + end + if timing == :immediately + timing = :immediate + end + timing + end + end +end
\ No newline at end of file |