Sha256: 6e83673ba8653fbfd1bc5e35a3c40c31d0258b093d2bf364d738d41a6bdf0b91

Contents?: true

Size: 1.19 KB

Versions: 19

Compression:

Stored size: 1.19 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:

  # This module contains the behaviour of Mongoid's clone/dup of documents.
  module Copyable
    extend ActiveSupport::Concern

    COPYABLES = [
      :@accessed,
      :@attributes,
      :@metadata,
      :@modifications,
      :@previous_modifications
    ]

    protected

    # Clone or dup the current +Document+. This will return all attributes with
    # the exception of the document's id and versions, and will reset all the
    # instance variables.
    #
    # This clone also includes embedded documents.
    #
    # @example Clone the document.
    #   document.clone
    #
    # @example Dup the document.
    #   document.dup
    #
    # @param [ Document ] other The document getting cloned.
    #
    # @return [ Document ] The new document.
    def initialize_copy(other)
      @attributes = other.as_document
      instance_variables.each { |name| remove_instance_variable(name) }
      COPYABLES.each do |name|
        value = other.instance_variable_get(name)
        instance_variable_set(name, value ? value.dup : nil)
      end
      attributes.delete("_id")
      attributes.delete("versions")
      @new_record = true
      identify
    end
  end
end

Version data entries

19 entries across 19 versions & 2 rubygems

Version Path
mongoid-2.2.6 lib/mongoid/copyable.rb
mongoid-2.2.5 lib/mongoid/copyable.rb
mongoid-2.2.4 lib/mongoid/copyable.rb
mongoid-2.2.3 lib/mongoid/copyable.rb
mongoid-2.2.2 lib/mongoid/copyable.rb
mongoid-2.2.1 lib/mongoid/copyable.rb
mongoid-2.2.0 lib/mongoid/copyable.rb
mongoid-2.1.9 lib/mongoid/copyable.rb
mongoid-2.1.8 lib/mongoid/copyable.rb
mongoid-2.1.7 lib/mongoid/copyable.rb
mongoid-2.1.6 lib/mongoid/copyable.rb
mongoid-2.1.5 lib/mongoid/copyable.rb
mongoid-2.1.4 lib/mongoid/copyable.rb
mongoid-2.1.3 lib/mongoid/copyable.rb
mongoid-2.1.2 lib/mongoid/copyable.rb
mongoid-2.1.1 lib/mongoid/copyable.rb
mongoid-2.1.0 lib/mongoid/copyable.rb
mongoid-braxton-2.0.2 lib/mongoid/copyable.rb
mongoid-2.0.2 lib/mongoid/copyable.rb