Class: Chef::ResourceCollection
- Inherits:
-
Object
- Object
- Chef::ResourceCollection
- Includes:
- Enumerable
- Defined in:
- lib/chef/resource_collection.rb
Class Method Summary collapse
Instance Method Summary collapse
- #<<(*args) ⇒ Object
- #[](index) ⇒ Object
- #[]=(index, arg) ⇒ Object
- #each ⇒ Object
- #each_index ⇒ Object
- #execute_each_resource ⇒ Object
-
#initialize ⇒ ResourceCollection
constructor
A new instance of ResourceCollection.
- #insert(resource) ⇒ Object
- #lookup(resource) ⇒ Object
- #push(*args) ⇒ Object
-
#resources(*args) ⇒ Object
Find existing resources by searching the list of existing resources.
-
#to_json(*a) ⇒ Object
Serialize this object as a hash.
Constructor Details
#initialize ⇒ ResourceCollection
Returns a new instance of ResourceCollection.
26 27 28 29 30 |
# File 'lib/chef/resource_collection.rb', line 26 def initialize @resources = Array.new @resources_by_name = Hash.new @insert_after_idx = nil end |
Class Method Details
.json_create(o) ⇒ Object
153 154 155 156 157 158 159 |
# File 'lib/chef/resource_collection.rb', line 153 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
42 43 44 45 46 47 48 |
# File 'lib/chef/resource_collection.rb', line 42 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
32 33 34 |
# File 'lib/chef/resource_collection.rb', line 32 def [](index) @resources[index] end |
#[]=(index, arg) ⇒ Object
36 37 38 39 40 |
# File 'lib/chef/resource_collection.rb', line 36 def []=(index, arg) is_chef_resource(arg) @resources[index] = arg @resources_by_name[arg.to_s] = index end |
#each ⇒ Object
77 78 79 80 81 |
# File 'lib/chef/resource_collection.rb', line 77 def each @resources.each do |r| yield r end end |
#each_index ⇒ Object
90 91 92 93 94 |
# File 'lib/chef/resource_collection.rb', line 90 def each_index @resources.each_index do |i| yield i end end |
#execute_each_resource ⇒ Object
83 84 85 86 87 88 |
# File 'lib/chef/resource_collection.rb', line 83 def execute_each_resource @resources.each_with_index do |r, idx| @insert_after_idx = idx yield r end end |
#insert(resource) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/chef/resource_collection.rb', line 50 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
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/chef/resource_collection.rb', line 96 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 ArgumentError, "Cannot find a resource matching #{lookup_by} (did you define it first?)" end @resources[res] end |
#push(*args) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/chef/resource_collection.rb', line 69 def push(*args) args.flatten.each do |a| is_chef_resource(a) @resources.push(a) @resources_by_name[a.to_s] = @resources.length - 1 end end |
#resources(*args) ⇒ Object
Find existing resources by searching the list of existing resources. Possible forms are:
resources(:file => “foobar”) resources(:file => [ “foobar”, “baz” ]) resources(“file”, “file”) resources(“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.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/chef/resource_collection.rb', line 124 def resources(*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 raise ArgumentError, "resources takes arguments as a hash or strings!" end end flat_results = results.flatten flat_results.length == 1 ? flat_results[0] : flat_results end |
#to_json(*a) ⇒ Object
Serialize this object as a hash
141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/chef/resource_collection.rb', line 141 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 |