Sha256: c559d8ed68641c2c240a29f5f3c648113b961063df333f2b29e926a54683bf03

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 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
end

# $Id: collection_merger.rb 2350 2007-03-24 21:24:29Z luke $

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
puppet-0.22.4 lib/puppet/util/rails/collection_merger.rb