motion/managed_object.rb in cdq-0.1.8 vs motion/managed_object.rb in cdq-0.1.9
- old
+ new
@@ -34,16 +34,10 @@
klass.defineRelationshipMethod(name)
end
end
end
- # Shortcut to look up the entity description for this class
- #
- def entity_description
- cdq.models.current.entitiesByName[name]
- end
-
# Creates a CDQ scope, but also defines a method on the class that returns the
# query directly.
#
def scope(name, query = nil, &block)
cdq.scope(name, query, &block)
@@ -66,14 +60,25 @@
#
def method_missing(name, *args, &block)
cdq.send(name, *args, &block)
end
- def responds_to?(name)
+ def respond_to?(name)
super || cdq.respond_to?(name)
end
+ def destroy_all
+ self.all.array.each do |instance|
+ instance.destroy
+ end
+ end
+
+ def destroy_all!
+ destroy_all
+ cdq.save
+ end
+
end
# Register this object for destruction with the current context. Will not
# actually be removed until the context is saved.
#
@@ -83,16 +88,72 @@
def inspect
description
end
+ def log(log_type = nil)
+ out = "\nOID: "
+ out << oid
+ out << "\n"
+
+ atts = entity.attributesByName
+ rels = entity.relationshipsByName
+
+ width = (atts.keys.map(&:length) + rels.keys.map(&:length)).max || 0
+
+ atts.each do |name, desc|
+ out << " #{name.ljust(width)} : "
+ out << send(name).inspect[0,95 - width]
+ out << "\n"
+ end
+
+ rels.each do |name, desc|
+ rel = CDQRelationshipQuery.new(self, name, nil, context: managedObjectContext)
+ if desc.isToMany
+ out << " #{name.ljust(width)} : "
+ out << rel.count.to_s
+ out << ' (count)'
+ else
+ out << " #{name.ljust(width)} : "
+ out << (rel.first && rel.first.oid || "nil")
+ end
+ end
+ out << "\n"
+
+ if log_type == :string
+ out
+ else
+ NSLog out
+ end
+ end
+
+ def ordered_set?(name)
+ # isOrdered is returning 0/1 instead of documented BOOL
+ ordered = entity.relationshipsByName[name].isOrdered
+ return true if ordered == true || ordered == 1
+ return false if ordered == false || ordered == 0
+ end
+
+ def set_to_extend(name)
+ if ordered_set?(name)
+ mutableOrderedSetValueForKey(name)
+ else
+ mutableSetValueForKey(name)
+ end
+ end
+
+ def oid
+ objectID.URIRepresentation.absoluteString.inspect
+ end
+
protected
# Called from method that's dynamically added from
# +[CoreDataManagedObjectBase defineRelationshipMethod:]
def relationshipByName(name)
willAccessValueForKey(name)
- set = CDQRelationshipQuery.extend_set(primitiveValueForKey(name), self, name)
+ set = CDQRelationshipQuery.extend_set(set_to_extend(name), self, name)
didAccessValueForKey(name)
set
end
+
end