Sha256: 683f0ea454c0897926633f8058d68da2f2577ad63fdb6a135c3887f0008cd37c

Contents?: true

Size: 983 Bytes

Versions: 9

Compression:

Stored size: 983 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)
          else
            @target = klass.load(doc)
          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).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

9 entries across 9 versions & 3 rubygems

Version Path
mongo_mapper-0.13.0.beta1 lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb
mongo_mapper-0.12.0 lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb
lookout-mongo_mapper-0.11.3 lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb
mongo_mapper-0.11.2 lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb
jamieorc-mongo_mapper-0.11.1.1 lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb
mongo_mapper-0.11.1 lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb
mongo_mapper-0.11.0 lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb
mongo_mapper-0.10.1 lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb
mongo_mapper-0.10.0 lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb