diff options
| author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2007-03-24 21:24:29 +0000 |
|---|---|---|
| committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2007-03-24 21:24:29 +0000 |
| commit | 2c3abbeef6298683dd199f2a5e663caaaa88dffa (patch) | |
| tree | e20950ee2d67a4e4aa8d6b267814aecfe2b5570d /lib/puppet/util/rails | |
| parent | 33f4a66a4ead50a570bd5eeeb97087829f84e132 (diff) | |
| download | puppet-2c3abbeef6298683dd199f2a5e663caaaa88dffa.tar.gz puppet-2c3abbeef6298683dd199f2a5e663caaaa88dffa.tar.xz puppet-2c3abbeef6298683dd199f2a5e663caaaa88dffa.zip | |
Refactoring some of the rails code. The speed is now pretty good, but the tagging stuff does not seem to be working and is certainly working very ineffficiently. Blake says he is going to take a look at that.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@2350 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'lib/puppet/util/rails')
| -rw-r--r-- | lib/puppet/util/rails/collection_merger.rb | 43 |
1 files changed, 30 insertions, 13 deletions
diff --git a/lib/puppet/util/rails/collection_merger.rb b/lib/puppet/util/rails/collection_merger.rb index 7afd76f9c..69e0309c9 100644 --- a/lib/puppet/util/rails/collection_merger.rb +++ b/lib/puppet/util/rails/collection_merger.rb @@ -1,24 +1,41 @@ module Puppet::Util::CollectionMerger # Merge new values with the old list. This is only necessary # because deletion seems to mess things up on unsaved objects. - def collection_merge(collection, list) - remove = send(collection).dup - - list.each do |value| - object = yield(value) - if remove.include?(object) - remove.delete(object) + def collection_merge(collection, args) + remove = [] + list = args[:existing] || send(collection) + hash = args[:updates] + list.each do |object| + name = object.name + if existing = hash[name] + hash.delete(name) + if existing.respond_to?(:to_rails) + existing.to_rails(self, object) + elsif args.include?(:modify) + args[:modify].call(object, name, existing) + else + raise ArgumentError, "Must pass :modify or the new objects must respond to :to_rails" + end + else + remove << object end end - unless remove.empty? - # We have to save the current state else the deletion somehow deletes - # our new values. - save - remove.each do |r| - send(collection).delete(r) + # Make a new rails object for the rest of them + hash.each do |name, object| + if object.respond_to?(:to_rails) + object.to_rails(self) + elsif args.include?(:create) + args[:create].call(name, object) + else + raise ArgumentError, "Must pass :create or the new objects must respond to :to_rails" end end + + # Now remove anything necessary. + remove.each do |object| + send(collection).delete(object) + end end end |
