class Chef::ResourceCollection
Attributes
resource_list[R]
resource_set[R]
run_context[RW]
Public Class Methods
from_hash(o)
click to toggle source
# File lib/chef/resource_collection.rb, line 121 def self.from_hash(o) collection = new() { "@resource_list" => "ResourceList", "@resource_set" => "ResourceSet" }.each_pair do |name, klass| obj = Chef::ResourceCollection.const_get(klass).from_hash(o["instance_vars"].delete(name)) collection.instance_variable_set(name.to_sym, obj) end collection.instance_variable_set(:@run_context, o["instance_vars"].delete("@run_context")) collection end
new(run_context = nil)
click to toggle source
# File lib/chef/resource_collection.rb, line 40 def initialize(run_context = nil) @run_context = run_context @resource_set = ResourceSet.new @resource_list = ResourceList.new end
Public Instance Methods
[]=(index, resource)
click to toggle source
@deprecated
# File lib/chef/resource_collection.rb, line 69 def []=(index, resource) Chef::Log.warn("`[]=` is deprecated, use `insert` (which only inserts at the end)") resource_list[index] = resource resource_set.insert_as(resource) end
delete(key)
click to toggle source
# File lib/chef/resource_collection.rb, line 62 def delete(key) res = resource_set.delete(key) resource_list.delete(res.to_s) res end
find(*args)
click to toggle source
# File lib/chef/resource_collection.rb, line 113 def find(*args) if run_context.nil? find_local(*args) else find_recursive(run_context, *args) end end
find_local(*args)
click to toggle source
# File lib/chef/resource_collection.rb, line 101 def find_local(*args) resource_set.find(*args) end
insert(resource, opts = {})
click to toggle source
@param resource [Chef::Resource] The resource to insert @param resource_type [String,Symbol] If known, the resource type used in the recipe, Eg `package`, `execute` @param instance_name [String] If known, the recource name as used in the recipe, IE `vim` in `package 'vim'` This method is meant to be the 1 insert method necessary in the future. It should support all known use cases
for writing into the ResourceCollection.
# File lib/chef/resource_collection.rb, line 51 def insert(resource, opts = {}) resource_type ||= opts[:resource_type] # Would rather use Ruby 2.x syntax, but oh well instance_name ||= opts[:instance_name] resource_list.insert(resource) if !(resource_type.nil? && instance_name.nil?) resource_set.insert_as(resource, resource_type, instance_name) else resource_set.insert_as(resource) end end
Also aliased as: <<
lookup(key)
click to toggle source
# File lib/chef/resource_collection.rb, line 105 def lookup(key) if run_context.nil? lookup_local(key) else lookup_recursive(run_context, key) end end
lookup_local(key)
click to toggle source
# File lib/chef/resource_collection.rb, line 97 def lookup_local(key) resource_set.lookup(key) end
push(*resources)
click to toggle source
@deprecated
# File lib/chef/resource_collection.rb, line 76 def push(*resources) Chef::Log.warn("`push` is deprecated, use `insert`") resources.flatten.each do |res| insert(res) end self end
Private Instance Methods
find_recursive(rc, *args)
click to toggle source
# File lib/chef/resource_collection.rb, line 140 def find_recursive(rc, *args) rc.resource_collection.resource_set.find(*args) rescue Chef::Exceptions::ResourceNotFound raise if rc.parent_run_context.nil? find_recursive(rc.parent_run_context, *args) end
lookup_recursive(rc, key)
click to toggle source
# File lib/chef/resource_collection.rb, line 133 def lookup_recursive(rc, key) rc.resource_collection.resource_set.lookup(key) rescue Chef::Exceptions::ResourceNotFound raise if rc.parent_run_context.nil? lookup_recursive(rc.parent_run_context, key) end