lib/ripple/associations/many_linked_proxy.rb in ripple-0.9.5 vs lib/ripple/associations/many_linked_proxy.rb in ripple-1.0.0.beta
- old
+ new
@@ -1,45 +1,66 @@
-# Copyright 2010 Sean Cribbs, Sonian Inc., and Basho Technologies, Inc.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
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)
- load_target
- new_target = @target.concat(Array(value))
- replace new_target
+ 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
- robjects.map {|robj| klass.send(:instantiate, robj) }
+ 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