Sha256: 38193257f605baf11e5cd5bb81c6db15abed0b0a1954f3e751beff3970dfe07b

Contents?: true

Size: 1.02 KB

Versions: 7

Compression:

Stored size: 1.02 KB

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.instance_of?(klass)
            @target = doc
          elsif 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

7 entries across 7 versions & 1 rubygems

Version Path
mongo_mapper-0.17.0 lib/mongo_mapper/plugins/associations/proxy/one_embedded_proxy.rb
mongo_mapper-0.16.0 lib/mongo_mapper/plugins/associations/proxy/one_embedded_proxy.rb
mongo_mapper-0.15.6 lib/mongo_mapper/plugins/associations/proxy/one_embedded_proxy.rb
mongo_mapper-0.15.5 lib/mongo_mapper/plugins/associations/proxy/one_embedded_proxy.rb
mongo_mapper-0.15.4 lib/mongo_mapper/plugins/associations/proxy/one_embedded_proxy.rb
mongo_mapper-0.15.3 lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb
mongo_mapper-0.15.2 lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb