Sha256: 3eb0770b53471f1b816dbb8b1d6e5b12c55bf161bb7cedab2a1fc268db130af5

Contents?: true

Size: 1.1 KB

Versions: 11

Compression:

Stored size: 1.1 KB

Contents

# Refer to the comments in SingleCopyEnforcer to understand why we need
# this class.
#
# Also note that the way this class is implemented, all records being copied
# are kept in memory.  For copying extremely large record trees, memory
# could be an issue, in which case this algorithm may need refactoring.
#
module Copyable
  class CopyRegistry

    class << self

      def register(original_record, new_record)
        @registry ||= {}
        key = make_hash(record: original_record)
        @registry[key] = new_record
      end

      def already_copied?(options)
        fetch_copy(options).present?
      end

      def fetch_copy(options)
        @registry ||= {}
        key = make_hash(options)
        @registry[key]
      end

      def clear
        @registry = {}
      end

    private

      def make_hash(options)
        if options[:record]
          id = options[:record].id
          klass = options[:record].class
        else
          id = options[:id]
          klass = options[:class]
        end
        raise "Record has no id" if id.nil?
        "#{klass.name}-#{id}"
      end

    end

  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
copyable-0.3.5 lib/copyable/copy_registry.rb
copyable-0.3.3 lib/copyable/copy_registry.rb
copyable-0.3.2 lib/copyable/copy_registry.rb
copyable-0.3.1 lib/copyable/copy_registry.rb
copyable-0.3.0 lib/copyable/copy_registry.rb
copyable-0.2.0 lib/copyable/copy_registry.rb
copyable-0.1.2 lib/copyable/copy_registry.rb
copyable-0.1.1 lib/copyable/copy_registry.rb
copyable-0.1.0 lib/copyable/copy_registry.rb
copyable-0.0.2 lib/copyable/copy_registry.rb
copyable-0.0.1 lib/copyable/copy_registry.rb