Sha256: bda97a4017f052079202f4508769599c80f2a1e5ab0e8064864116cd956eb9c4

Contents?: true

Size: 987 Bytes

Versions: 6

Compression:

Stored size: 987 Bytes

Contents

# encoding: UTF-8
module MarkMapper
  module Plugins
    module Associations
      class EmbeddedCollection < Collection
        def build(attributes={})
          doc = klass.new(attributes)
          assign_references(doc)
          self << doc
          doc
        end

        def find(id)
          load_target
          target.detect { |item| item.id.to_s == id || item.id == id }
        end

        def count
          load_target
          target.size
        end

        def <<(*docs)
          load_target
          docs.each do |doc|
            assign_references(doc)
            target << doc
          end
        end
        alias_method :push, :<<
        alias_method :concat, :<<

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

        private
          def assign_references(*docs)
            docs.each { |doc| doc._parent_document = proxy_owner }
          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/embedded_collection.rb
mark_mapper-0.0.5 lib/mark_mapper/plugins/associations/embedded_collection.rb
mark_mapper-0.0.4 lib/mark_mapper/plugins/associations/embedded_collection.rb
mark_mapper-0.0.3 lib/mark_mapper/plugins/associations/embedded_collection.rb
mark_mapper-0.0.2 lib/mark_mapper/plugins/associations/embedded_collection.rb
mark_mapper-0.0.1 lib/mark_mapper/plugins/associations/embedded_collection.rb