Sha256: 5415c9aeee1d32d4df34bbb20072eb440ba0520ee4661f540523bad910958b7e

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

module MongoMapper
  module Associations
    class ManyEmbeddedProxy < Proxy
      def replace(v)
        @_values = v.map { |e| e.kind_of?(EmbeddedDocument) ? e.attributes : e }
        reset
      end
      
      def build(opts={})
        owner = @owner
        child = @association.klass.new(opts)
        child.class_eval do
          define_method(owner.class.name.underscore) do
            owner
          end
        end
        child._parent_document = owner
        self << child
        child
      end
      
      def find(opts)
        case opts
        when :all
          self
        when String
          if load_target
            child = @target.detect {|item| item.id == opts}
            if child
              owner = @owner
              child.class_eval do
                define_method(owner.class.name.underscore) do
                  owner
                end
              end
            end
            child
          end
        end
      end

      def <<(*docs)
        if load_target
          parent = @owner._parent_document || @owner
          docs.each do |doc|
            doc._parent_document = parent
            @target << doc
          end
        end
      end
      alias_method :push, :<<
      alias_method :concat, :<<

      protected
        def find_target
          (@_values || []).map do |e|
            @association.klass.new(e)
          end
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hashrocket-mongomapper-0.3.8 lib/mongomapper/associations/many_embedded_proxy.rb