Class: Chef::ResourceCollection::ResourceList

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

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

#initializeResourceList

Returns a new instance of ResourceList


42
43
44
45
# File 'lib/chef/resource_collection/resource_list.rb', line 42

def initialize
  @resources = Array.new
  @insert_after_idx = nil
end

Instance Attribute Details

#iteratorObject (readonly)

Returns the value of attribute iterator


33
34
35
# File 'lib/chef/resource_collection/resource_list.rb', line 33

def iterator
  @iterator
end

Class Method Details

.from_hash(o) ⇒ Object


98
99
100
101
102
103
# File 'lib/chef/resource_collection/resource_list.rb', line 98

def self.from_hash(o)
  collection = new()
  resources = o["instance_vars"]["@resources"].map { |r| Chef::Resource.from_hash(r) }
  collection.instance_variable_set(:@resources, resources)
  collection
end

Instance Method Details

#[]=(index, resource) ⇒ Object

Deprecated.
  • can be removed when it is removed from resource_collection.rb

81
82
83
# File 'lib/chef/resource_collection/resource_list.rb', line 81

def []=(index, resource)
  @resources[index] = resource
end

#all_resourcesObject


85
86
87
# File 'lib/chef/resource_collection/resource_list.rb', line 85

def all_resources
  @resources
end

#delete(key) ⇒ Object

Raises:

  • (ArgumentError)

70
71
72
73
74
75
76
77
78
# File 'lib/chef/resource_collection/resource_list.rb', line 70

def delete(key)
  raise ArgumentError, "Must pass a Chef::Resource or String to delete" unless key.is_a?(String) || key.is_a?(Chef::Resource)
  key = key.to_s
  ret = @resources.reject! { |r| r.to_s == key }
  if ret.nil?
    raise Chef::Exceptions::ResourceNotFound, "Cannot find a resource matching #{key} (did you define it first?)"
  end
  ret
end

#execute_each_resourceObject

FIXME: yard with @yield


90
91
92
93
94
95
96
# File 'lib/chef/resource_collection/resource_list.rb', line 90

def execute_each_resource
  @iterator = ResourceCollection::StepableIterator.for_collection(@resources)
  @iterator.each_with_index do |resource, idx|
    @insert_after_idx = idx
    yield resource
  end
end

#insert(resource) ⇒ Object

If @insert_after_idx is nil, we are not currently executing a converge so the Resource is appended to the end of the list. If @insert_after_idx is NOT nil, we ARE currently executing a converge so the resource is inserted into the middle of the list after the last resource that was converged. If it is called multiple times (when an LWRP contains multiple resources) it keeps track of that. See this example ResourceList: [File1, LWRP1, File2] # The iterator starts and points to File1. It is executed and @insert_after_idx=0 [File1, LWRP1, File2] # The iterator moves to LWRP1. It is executed and @insert_after_idx=1 [File1, LWRP1, Service1, File2] # The LWRP execution inserts Service1 and @insert_after_idx=2 [File1, LWRP1, Service1, Service2, File2] # The LWRP inserts Service2 and @insert_after_idx=3. The LWRP finishes executing [File1, LWRP1, Service1, Service2, File2] # The iterator moves to Service1 since it is the next non-executed resource. The execute_each_resource call below resets @insert_after_idx=2 If Service1 was another LWRP, it would insert its resources between Service1 and Service2. The iterator keeps track of executed resources and @insert_after_idx keeps track of where the next resource to insert should be.

Parameters:


61
62
63
64
65
66
67
68
# File 'lib/chef/resource_collection/resource_list.rb', line 61

def insert(resource)
  is_chef_resource!(resource)
  if @insert_after_idx
    @resources.insert(@insert_after_idx += 1, resource)
  else
    @resources << resource
  end
end