Sha256: 2c5f29f9c5f82574af5083b034efc3774b6dd1132715ac02109af00756881eb3

Contents?: true

Size: 1000 Bytes

Versions: 6

Compression:

Stored size: 1000 Bytes

Contents

# encoding: UTF-8
module MarkMapper
  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

6 entries across 6 versions & 1 rubygems

Version Path
mark_mapper-0.0.6 lib/mark_mapper/plugins/associations/one_embedded_proxy.rb
mark_mapper-0.0.5 lib/mark_mapper/plugins/associations/one_embedded_proxy.rb
mark_mapper-0.0.4 lib/mark_mapper/plugins/associations/one_embedded_proxy.rb
mark_mapper-0.0.3 lib/mark_mapper/plugins/associations/one_embedded_proxy.rb
mark_mapper-0.0.2 lib/mark_mapper/plugins/associations/one_embedded_proxy.rb
mark_mapper-0.0.1 lib/mark_mapper/plugins/associations/one_embedded_proxy.rb