Class: Chef::ResourceCollection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
ResourceCollectionSerialization
Defined in:
lib/chef/resource_collection.rb,
lib/chef/resource_collection/resource_set.rb,
lib/chef/resource_collection/resource_list.rb,
lib/chef/resource_collection/stepable_iterator.rb,
lib/chef/resource_collection/resource_collection_serialization.rb

Defined Under Namespace

Modules: ResourceCollectionSerialization Classes: ResourceList, ResourceSet, StepableIterator

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ResourceCollectionSerialization

included, #is_chef_resource!, #to_hash, #to_json

Constructor Details

#initialize(run_context = nil) ⇒ ResourceCollection

Returns a new instance of ResourceCollection


40
41
42
43
44
# 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

Instance Attribute Details

#run_contextObject

Returns the value of attribute run_context


36
37
38
# File 'lib/chef/resource_collection.rb', line 36

def run_context
  @run_context
end

Class Method Details

.from_hash(o) ⇒ Object


120
121
122
123
124
125
126
127
128
# File 'lib/chef/resource_collection.rb', line 120

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

Instance Method Details

#[]=(index, resource) ⇒ Object

Deprecated.

68
69
70
71
72
# File 'lib/chef/resource_collection.rb', line 68

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) ⇒ Object


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

def delete(key)
  resource_list.delete(key)
  resource_set.delete(key)
end

#find(*args) ⇒ Object


112
113
114
115
116
117
118
# File 'lib/chef/resource_collection.rb', line 112

def find(*args)
  if run_context.nil?
    find_local(*args)
  else
    find_recursive(run_context, *args)
  end
end

#find_local(*args) ⇒ Object


100
101
102
# File 'lib/chef/resource_collection.rb', line 100

def find_local(*args)
  resource_set.find(*args)
end

#insert(resource, opts = {}) ⇒ Object Also known as: <<

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.

Parameters:

  • resource (Chef::Resource)

    The resource to insert

  • resource_type (String, Symbol)

    If known, the resource type used in the recipe, Eg package, execute

  • instance_name (String)

    If known, the recource name as used in the recipe, IE vim in package 'vim'


51
52
53
54
55
56
57
58
59
60
# 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

#lookup(key) ⇒ Object


104
105
106
107
108
109
110
# File 'lib/chef/resource_collection.rb', line 104

def lookup(key)
  if run_context.nil?
    lookup_local(key)
  else
    lookup_recursive(run_context, key)
  end
end

#lookup_local(key) ⇒ Object


96
97
98
# File 'lib/chef/resource_collection.rb', line 96

def lookup_local(key)
  resource_set.lookup(key)
end

#push(*resources) ⇒ Object

Deprecated.

75
76
77
78
79
80
81
# File 'lib/chef/resource_collection.rb', line 75

def push(*resources)
  Chef::Log.warn("`push` is deprecated, use `insert`")
  resources.flatten.each do |res|
    insert(res)
  end
  self
end