Class: Chef::ResourceCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/chef/resource_collection.rb,
lib/chef/resource_collection/stepable_iterator.rb

Defined Under Namespace

Classes: StepableIterator

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResourceCollection

Returns a new instance of ResourceCollection.



29
30
31
32
33
# File 'lib/chef/resource_collection.rb', line 29

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

Instance Attribute Details

#iteratorObject (readonly)

Returns the value of attribute iterator.



27
28
29
# File 'lib/chef/resource_collection.rb', line 27

def iterator
  @iterator
end

Class Method Details

.json_create(o) ⇒ Object



166
167
168
169
170
171
172
# File 'lib/chef/resource_collection.rb', line 166

def self.json_create(o)
  collection = self.new()
  o["instance_vars"].each do |k,v|
    collection.instance_variable_set(k.to_sym, v)
  end
  collection
end

Instance Method Details

#<<(*args) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/chef/resource_collection.rb', line 49

def <<(*args)
  args.flatten.each do |a|
    is_chef_resource(a)
    @resources << a
    @resources_by_name[a.to_s] = @resources.length - 1 
  end
end

#[](index) ⇒ Object



39
40
41
# File 'lib/chef/resource_collection.rb', line 39

def [](index)
  @resources[index]
end

#[]=(index, arg) ⇒ Object



43
44
45
46
47
# File 'lib/chef/resource_collection.rb', line 43

def []=(index, arg)
  is_chef_resource(arg)
  @resources[index] = arg 
  @resources_by_name[arg.to_s] = index
end

#all_resourcesObject



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

def all_resources
  @resources
end

#eachObject



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

def each
  @resources.each do |resource|
    yield resource
  end
end

#each_indexObject



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

def each_index
  @resources.each_index do |i|
    yield i
  end
end

#execute_each_resource(&resource_exec_block) ⇒ Object



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

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

#find(*args) ⇒ Object Also known as: resources

Find existing resources by searching the list of existing resources. Possible forms are:

find(:file => “foobar”) find(:file => [ “foobar”, “baz” ]) find(“file”, “file”) find(“file”)

Returns the matching resource, or an Array of matching resources.

Raises an ArgumentError if you feed it bad lookup information Raises a Runtime Error if it can’t find the resources you are looking for.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/chef/resource_collection.rb', line 132

def find(*args)
  results = Array.new
  args.each do |arg|
    case arg
    when Hash
      results << find_resource_by_hash(arg)
    when String
      results << find_resource_by_string(arg)
    else
      msg = "arguments to #{self.class.name}#find should be of the form :resource => 'name' or resource[name]"
      raise Chef::Exceptions::InvalidResourceSpecification, msg
    end
  end
  flat_results = results.flatten
  flat_results.length == 1 ? flat_results[0] : flat_results
end

#insert(resource) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/chef/resource_collection.rb', line 57

def insert(resource)
  is_chef_resource(resource)
  if @insert_after_idx
    # in the middle of executing a run, so any resources inserted now should
    # be placed after the most recent addition done by the currently executing
    # resource
    @resources.insert(@insert_after_idx + 1, resource)
    # update name -> location mappings and register new resource
    @resources_by_name.each_key do |key|
      @resources_by_name[key] += 1 if @resources_by_name[key] > @insert_after_idx
    end
    @resources_by_name[resource.to_s] = @insert_after_idx + 1
    @insert_after_idx += 1
  else  
    @resources << resource
    @resources_by_name[resource.to_s] = @resources.length - 1
  end
end

#lookup(resource) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/chef/resource_collection.rb', line 104

def lookup(resource)
  lookup_by = nil
  if resource.kind_of?(Chef::Resource)
    lookup_by = resource.to_s
  elsif resource.kind_of?(String)
    lookup_by = resource
  else
    raise ArgumentError, "Must pass a Chef::Resource or String to lookup"
  end
  res = @resources_by_name[lookup_by]
  unless res
    raise Chef::Exceptions::ResourceNotFound, "Cannot find a resource matching #{lookup_by} (did you define it first?)"
  end
  @resources[res]
end

#push(*args) ⇒ Object



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

def push(*args)
  args.flatten.each do |arg|
    is_chef_resource(arg)
    @resources.push(arg)
    @resources_by_name[arg.to_s] = @resources.length - 1
  end
end

#to_json(*a) ⇒ Object

Serialize this object as a hash



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/chef/resource_collection.rb', line 154

def to_json(*a)
  instance_vars = Hash.new
  self.instance_variables.each do |iv|
    instance_vars[iv] = self.instance_variable_get(iv)
  end
  results = {
    'json_class' => self.class.name,
    'instance_vars' => instance_vars
  }
  results.to_json(*a)
end