diff options
author | John Keiser <jkeiser@opscode.com> | 2014-04-29 15:20:03 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2014-08-20 17:05:24 -0700 |
commit | 6f2bbbb26a6efcb9362cc16a689654291b8ca67b (patch) | |
tree | 1265c0139b4922d61cd9396ff6fd55adbb25ca91 /lib/chef/resource_collection.rb | |
parent | cebeaf0419fd9ea6f4b6c3e089e359daf4b5ae46 (diff) | |
download | chef-6f2bbbb26a6efcb9362cc16a689654291b8ca67b.tar.gz |
Add insert_at to resource_collection
Diffstat (limited to 'lib/chef/resource_collection.rb')
-rw-r--r-- | lib/chef/resource_collection.rb | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/lib/chef/resource_collection.rb b/lib/chef/resource_collection.rb index 2cbd61cb0c..c745d48f1a 100644 --- a/lib/chef/resource_collection.rb +++ b/lib/chef/resource_collection.rb @@ -67,24 +67,33 @@ class Chef alias_method :push, :<< 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_at(@insert_after_idx + 1, resource) @insert_after_idx += 1 else + is_chef_resource(resource) @resources << resource @resources_by_name[resource.to_s] = @resources.length - 1 end end + def insert_at(insert_at_index, *resources) + resources.each do |resource| + is_chef_resource(resource) + end + @resources.insert(insert_at_index, *resources) + # update name -> location mappings and register new resource + @resources_by_name.each_key do |key| + @resources_by_name[key] += resources.size if @resources_by_name[key] >= insert_at_index + end + resources.each_with_index do |resource, i| + @resources_by_name[resource.to_s] = insert_at_index + i + end + end + def each @resources.each do |resource| yield resource @@ -249,7 +258,7 @@ class Chef def is_chef_resource(arg) unless arg.kind_of?(Chef::Resource) - raise ArgumentError, "Members must be Chef::Resource's" + raise ArgumentError, "Cannot insert a #{arg.class} into a resource collection: must be a Chef::Resource or descendant thereof" end true end |