Sha256: 902da7fda3a6e413462bf3e2d6309ff0b510eb02e0fa8f2d677be42b7afff182

Contents?: true

Size: 1.04 KB

Versions: 8

Compression:

Stored size: 1.04 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  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.
    #
    # Example:
    #
    # <tt>document.clone</tt>
    # <tt>document.dup</tt>
    #
    # Options:
    #
    # other: The document getting cloned.
    #
    # Returns:
    #
    # A new document with all the attributes except id and versions
    def initialize_copy(other)
      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

8 entries across 8 versions & 2 rubygems

Version Path
mongoid-2.0.0.rc.7 lib/mongoid/copyable.rb
stonegao-mongoid-2.0.0.rc.6 lib/mongoid/copyable.rb
mongoid-2.0.0.rc.6 lib/mongoid/copyable.rb
mongoid-2.0.0.rc.5 lib/mongoid/copyable.rb
mongoid-2.0.0.rc.4 lib/mongoid/copyable.rb
mongoid-2.0.0.rc.3 lib/mongoid/copyable.rb
mongoid-2.0.0.rc.2 lib/mongoid/copyable.rb
mongoid-2.0.0.rc.1 lib/mongoid/copyable.rb