motion/cdq/relationship_query.rb in cdq-0.1.8 vs motion/cdq/relationship_query.rb in cdq-0.1.9
- old
+ new
@@ -4,12 +4,19 @@
class CDQRelationshipQuery < CDQTargetedQuery
def initialize(owner, name, set = nil, opts = {})
@owner = owner
@relationship_name = name
- @set = set || @owner.send(name)
+ @set = set
relationship = owner.entity.relationshipsByName[name]
+ if relationship.isToMany
+ if @owner.ordered_set?(name)
+ @set ||= @owner.mutableOrderedSetValueForKey(name)
+ else
+ @set ||= @owner.mutableSetValueForKey(name)
+ end
+ end
@inverse_rel = relationship.inverseRelationship
entity_description = relationship.destinationEntity
target_class = constantize(entity_description.managedObjectClassName)
super(entity_description, target_class, opts)
if @inverse_rel.isToMany
@@ -28,19 +35,20 @@
end
# Add an existing object to the relationship
#
def add(obj)
- if @inverse_rel.isToMany
- obj.send(@inverse_rel.name).addObject(@owner)
- else
- obj.send("#{@inverse_rel.name}=", @owner)
- end
@set.addObject obj
end
alias_method :<<, :add
+ # Remove objects from the relationship
+ #
+ def remove(obj)
+ @set.removeObject obj
+ end
+
def self.extend_set(set, owner, name)
set.extend SetExt
set.extend Enumerable
set.__query__ = self.new(owner, name, set)
set
@@ -83,9 +91,13 @@
@__query__.create(opts)
end
def new(opts = {})
@__query__.new(opts)
+ end
+
+ def remove(opts = {})
+ @__query__.remove(opts)
end
def where(*args)
@__query__.where(*args)
end