Sha256: 5bec7351966f4527e56a99686c237d0246056f06db1e0eee8b3a08feed692d05

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

module Mongoid
  module Cloneable
    class DocumentCloner
      attr_accessor :document, :cloned_document, :options

      def initialize(document, cloned_document)
        self.document = document
        self.cloned_document = cloned_document
        self.options  = document.class.cloneable || {}

        clone_attributes
        clone_relations
      end

      private

      def included_keys
        [*options[:include]].map(&:to_s)
      end

      def excluded_keys
        [*options[:exclude]].map(&:to_s)
      end

      def included_relations
        cloned_document.relations.slice(*included_keys).values
      end

      def excluded_relations
        cloned_document.relations.slice(*excluded_keys).values
      end

      def clone_attributes
        attrs = cloned_document.attributes.except('_id', '_type')

        if included_keys.present?
          attrs.except(*included_keys).each do |attr, value|
            cloned_document[attr] = nil
          end
        end

        attrs.slice(*excluded_keys).each do |attr, value|
          cloned_document[attr] = nil
        end
      end

      def clone_relations
        included_relations.each do |relation|
          Mongoid::Cloneable::RelationshipCloner.clone(document, cloned_document, relation)
        end

        excluded_relations.each do |relation|
          Mongoid::Cloneable::RelationshipCloner.clear(cloned_document, relation)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongoid_cloneable-0.0.1 lib/mongoid/cloneable/document_cloner.rb