summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlamont-granquist <lamont@scriptkiddie.org>2014-08-21 09:40:32 -0700
committerlamont-granquist <lamont@scriptkiddie.org>2014-08-21 09:40:32 -0700
commitb49767dd1ed9c778f94bef597e52a61d48491b50 (patch)
tree47bef43648ad740dfa267489a431190d79ead5ec
parentcebeaf0419fd9ea6f4b6c3e089e359daf4b5ae46 (diff)
parent8ab8a610e060cc2126e6a1bc3413e9e06bc295d9 (diff)
downloadchef-b49767dd1ed9c778f94bef597e52a61d48491b50.tar.gz
Merge pull request #1873 from opscode/lcg/insert_at
Lcg/insert at
-rw-r--r--CHANGELOG.md1
-rw-r--r--lib/chef/resource_collection.rb25
-rw-r--r--spec/unit/resource_collection_spec.rb33
3 files changed, 51 insertions, 8 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fa046050eb..833dd0a055 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,7 @@
## Unreleased: 12.0.0
+* added Chef::ResourceCollection#insert_at API to the ResourceCollection
* [**Grzesiek Kolodziejczyk**](https://github.com/grk):
Use thread-safe OpenSSL::Digest instead of Digest
* knife cookbook site download/list/search/share/show/unshare now uses
diff --git a/lib/chef/resource_collection.rb b/lib/chef/resource_collection.rb
index 2cbd61cb0c..cc14a03962 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 subclass of Chef::Resource"
end
true
end
diff --git a/spec/unit/resource_collection_spec.rb b/spec/unit/resource_collection_spec.rb
index cf62f5ff40..eddd92e098 100644
--- a/spec/unit/resource_collection_spec.rb
+++ b/spec/unit/resource_collection_spec.rb
@@ -88,6 +88,39 @@ describe Chef::ResourceCollection do
end
end
+ describe "insert_at" do
+ it "should accept only Chef::Resources" do
+ lambda { @rc.insert_at(0, @resource, @resource) }.should_not raise_error
+ lambda { @rc.insert_at(0, "string") }.should raise_error
+ lambda { @rc.insert_at(0, @resource, "string") }.should raise_error
+ end
+
+ it "should toss an error if it receives a bad index" do
+ @rc.insert_at(10, @resource)
+ end
+
+ it "should insert resources at the beginning when asked" do
+ @rc.insert(Chef::Resource::ZenMaster.new('1'))
+ @rc.insert(Chef::Resource::ZenMaster.new('2'))
+ @rc.insert_at(0, Chef::Resource::ZenMaster.new('X'))
+ @rc.all_resources.map { |r| r.name }.should == [ 'X', '1', '2' ]
+ end
+
+ it "should insert resources in the middle when asked" do
+ @rc.insert(Chef::Resource::ZenMaster.new('1'))
+ @rc.insert(Chef::Resource::ZenMaster.new('2'))
+ @rc.insert_at(1, Chef::Resource::ZenMaster.new('X'))
+ @rc.all_resources.map { |r| r.name }.should == [ '1', 'X', '2' ]
+ end
+
+ it "should insert resources at the end when asked" do
+ @rc.insert(Chef::Resource::ZenMaster.new('1'))
+ @rc.insert(Chef::Resource::ZenMaster.new('2'))
+ @rc.insert_at(2, Chef::Resource::ZenMaster.new('X'))
+ @rc.all_resources.map { |r| r.name }.should == [ '1', '2', 'X' ]
+ end
+ end
+
describe "each" do
it "should allow you to iterate over every resource in the collection" do
load_up_resources