Sha256: 640f80c97a71295119423cd962aaff44b86f7d5dbf7766a166abe2eeade5a8b6

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

require 'ripple/associations/proxy'
require 'ripple/associations/many'
require 'ripple/associations/linked'

module Ripple
  module Associations
    class ManyLinkedProxy < Proxy
      include Many
      include Linked

      def count
        # avoid having to load all documents by using our keys set instead
        keys.size
      end

      def <<(value)
        if loaded?
          new_target = @target.concat(Array(value))
          replace new_target
        else
          @reflection.verify_type!([value], @owner)
          @owner.robject.links << value.to_link(@reflection.link_tag)
          appended_documents << value
          @keys = nil
        end

        self
      end

      def delete(value)
        load_target
        @target.delete(value)
        replace @target
        self
      end

      def reset
        @appended_documents = nil
        super
      end

      def loaded_documents
        (super + appended_documents).uniq
      end

      protected

      def find_target
        robjs = robjects

        robjs.delete_if do |robj|
          appended_documents.any? do |doc|
            doc.key == robj.key &&
            doc.class.bucket_name == robj.bucket.name
          end
        end

        appended_documents + robjs.map {|robj| klass.send(:instantiate, robj) }
      end

      def appended_documents
        @appended_documents ||= []
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
seomoz-ripple-1.0.0.pre lib/ripple/associations/many_linked_proxy.rb
ripple-1.0.0.beta lib/ripple/associations/many_linked_proxy.rb