Sha256: 9dfe41d92affc944c42c0a1e4769219145bc9eecc5b406ea5ffd24f5dc50e312

Contents?: true

Size: 1.96 KB

Versions: 10

Compression:

Stored size: 1.96 KB

Contents

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, 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

        # 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
    
    def ar_hash_merge(db_hash, mem_hash, args)
        (db_hash.keys | mem_hash.keys).each do |key|
            if (db_hash[key] && mem_hash[key])
                # in both, update value
                args[:modify].call(db_hash[key], mem_hash[key])
            elsif (db_hash[key])
                # in db, not memory, delete from database
                args[:delete].call(db_hash[key])
            else
                # in mem, not in db, insert into the database
                args[:create].call(key, mem_hash[key])
            end
        end
    end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
puppet-0.24.9 lib/puppet/util/rails/collection_merger.rb
puppet-0.24.0 lib/puppet/util/rails/collection_merger.rb
puppet-0.24.3 lib/puppet/util/rails/collection_merger.rb
puppet-0.24.1 lib/puppet/util/rails/collection_merger.rb
puppet-0.24.2 lib/puppet/util/rails/collection_merger.rb
puppet-0.24.4 lib/puppet/util/rails/collection_merger.rb
puppet-0.24.5 lib/puppet/util/rails/collection_merger.rb
puppet-0.24.6 lib/puppet/util/rails/collection_merger.rb
puppet-0.24.7 lib/puppet/util/rails/collection_merger.rb
puppet-0.24.8 lib/puppet/util/rails/collection_merger.rb