Sha256: 93387c8baf195c33b4fe12480b2050434f71e5302dab1f82160c211b52a06189

Contents?: true

Size: 1.24 KB

Versions: 7

Compression:

Stored size: 1.24 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")
      if attributes.delete("versions")
        attributes["version"] = 1
      end
      @new_record = true
      identify
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
mongoid-multi-db-3.0.0 lib/mongoid/copyable.rb
mongoid-2.3.5 lib/mongoid/copyable.rb
mongoid-2.3.4 lib/mongoid/copyable.rb
mongoid-2.3.3 lib/mongoid/copyable.rb
mongoid-2.3.2 lib/mongoid/copyable.rb
mongoid-2.3.1 lib/mongoid/copyable.rb
mongoid-2.3.0 lib/mongoid/copyable.rb