motion/cdq/relationship_query.rb in cdq-1.0.2 vs motion/cdq/relationship_query.rb in cdq-1.0.3

- old
+ new

@@ -2,13 +2,13 @@ module CDQ class CDQRelationshipQuery < CDQTargetedQuery def initialize(owner, name, set = nil, opts = {}) - @owner = owner + @owner = owner ? WeakRef.new(owner) : nil @relationship_name = name - @set = set + @set = set ? WeakRef.new(set) : nil relationship = owner.entity.relationshipsByName[name] if relationship.isToMany if @owner.ordered_set?(name) @set ||= @owner.mutableOrderedSetValueForKey(name) else @@ -18,16 +18,20 @@ @inverse_rel = relationship.inverseRelationship entity_description = relationship.destinationEntity target_class = constantize(entity_description.managedObjectClassName) super(entity_description, target_class, opts) if @inverse_rel.isToMany - @predicate = self.where(@inverse_rel.name.to_sym).contains(@owner).predicate + @predicate = self.where(@inverse_rel.name.to_sym).contains(owner).predicate else @predicate = self.where(@inverse_rel.name.to_sym => @owner).predicate end end + def dealloc + super + end + # Creates a new managed object within the target relationship # def new(opts = {}) super(opts).tap do |obj| add(obj) @@ -45,10 +49,14 @@ # def remove(obj) @set.removeObject obj end + def remove_all + @set.removeAllObjects + end + def self.extend_set(set, owner, name) set.extend SetExt set.extend Enumerable set.__query__ = self.new(owner, name, set) set @@ -70,11 +78,15 @@ # NSOrderedSet, which is actually exactly what we want. def array self.allObjects end - def first - array.first + def first(n = 1) + n == 1 ? array.first : array.first(n) + end + + def last(n = 1) + n == 1 ? array.last : array.last(n) end # duplicating a lot of common methods because it's way faster than using method_missing # def each(*args, &block)