Sha256: 272d51e65077e0d1a292fcc943507b65541e535feab3d7c9c20cfa7be422bde3

Contents?: true

Size: 979 Bytes

Versions: 4

Compression:

Stored size: 979 Bytes

Contents

# encoding: UTF-8
module MongoMapper
  module Plugins
    module Associations
      class OneEmbeddedProxy < Proxy
        def build(attributes={})
          @target = klass.new(attributes)
          assign_references(@target)
          loaded
          @target
        end

        def replace(doc)
          if doc.respond_to?(:attributes)
            @target = klass.load(doc.attributes, true)
          else
            @target = klass.load(doc, true)
          end
          assign_references(@target)
          loaded
          @target
        end

        def save_to_collection(options={})
          @target.persist(options) if @target
        end

      protected

        def find_target
          if @value
            klass.load(@value, true).tap do |child|
              assign_references(child)
            end
          end
        end

        def assign_references(doc)
          doc._parent_document = proxy_owner if doc
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mongo_mapper-0.15.1 lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb
mongo_mapper-0.15.0 lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb
mongo_mapper-0.14.0 lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb
mongo_mapper-0.14.0.rc1 lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb