delete can now take a results object as an argument

This commit is contained in:
Ari Lazier 2015-09-02 21:31:29 -07:00
parent 3e0a1572f4
commit df05636e7a
3 changed files with 26 additions and 12 deletions

View File

@ -19,6 +19,7 @@
#import "RJSRealm.hpp" #import "RJSRealm.hpp"
#import "RJSObject.hpp" #import "RJSObject.hpp"
#import "RJSResults.hpp" #import "RJSResults.hpp"
#import "RJSArray.hpp"
#import "RJSSchema.hpp" #import "RJSSchema.hpp"
#import "shared_realm.hpp" #import "shared_realm.hpp"
@ -234,12 +235,18 @@ JSValueRef RealmDelete(JSContextRef ctx, JSObjectRef function, JSObjectRef thisO
try { try {
RJSValidateArgumentCount(argumentCount, 1); RJSValidateArgumentCount(argumentCount, 1);
if (RJSIsValueArray(ctx, arguments[0])) { if (RJSIsValueArray(ctx, arguments[0]) ||
JSValueIsObjectOfClass(ctx, arguments[0], RJSResultsClass()) ||
JSValueIsObjectOfClass(ctx, arguments[0], RJSArrayClass()))
{
JSObjectRef array = RJSValidatedValueToObject(ctx, arguments[0]); JSObjectRef array = RJSValidatedValueToObject(ctx, arguments[0]);
size_t length = RJSValidatedArrayLength(ctx, array); size_t length = RJSValidatedArrayLength(ctx, array);
for (size_t i = 0; i < length; i++) { for (long i = length-1; i >= 0; i--) {
JSValueRef object = RJSValidatedObjectAtIndex(ctx, array, (unsigned int)i); JSValueRef object = RJSValidatedObjectAtIndex(ctx, array, (unsigned int)i);
RealmDelete(ctx, function, thisObject, 1, &object, jsException); RealmDelete(ctx, function, thisObject, 1, &object, jsException);
if (*jsException) {
return NULL;
}
} }
return NULL; return NULL;
} }
@ -257,7 +264,7 @@ JSValueRef RealmDelete(JSContextRef ctx, JSObjectRef function, JSObjectRef thisO
} }
realm::TableRef table = ObjectStore::table_for_object_type(realm->read_group(), object->object_schema.name); realm::TableRef table = ObjectStore::table_for_object_type(realm->read_group(), object->object_schema.name);
table->remove(object->row.get_index()); table->move_last_over(object->row.get_index());
return NULL; return NULL;
} }

View File

@ -87,14 +87,12 @@ var RealmTests = {
testRealmDelete: function() { testRealmDelete: function() {
var realm = new Realm({schema: [TestObjectSchema]}); var realm = new Realm({schema: [TestObjectSchema]});
realm.write(function() { realm.write(function() {
realm.create('TestObject', [1]); for (var i = 0; i < 10; i++) {
realm.create('TestObject', [2]); realm.create('TestObject', [i]);
realm.create('TestObject', [3]); }
realm.create('TestObject', [4]);
}); });
var objects = realm.objects('TestObject'); var objects = realm.objects('TestObject');
TestCase.assertEqual(objects.length, 4, 'wrong object count');
TestCase.assertThrows(function() { TestCase.assertThrows(function() {
realm.delete(objects[0]); realm.delete(objects[0]);
}, "can only delete in a write transaction"); }, "can only delete in a write transaction");
@ -105,11 +103,20 @@ var RealmTests = {
}); });
realm.delete(objects[0]); realm.delete(objects[0]);
TestCase.assertEqual(objects.length, 3, 'wrong object count'); TestCase.assertEqual(objects.length, 9, 'wrong object count');
TestCase.assertEqual(objects[0].doubleCol, 2); TestCase.assertEqual(objects[0].doubleCol, 9, "wrong property value");
TestCase.assertEqual(objects[1].doubleCol, 1, "wrong property value");
realm.delete([objects[0], objects[1]]); realm.delete([objects[0], objects[1]]);
TestCase.assertEqual(objects.length, 1, 'wrong object count'); TestCase.assertEqual(objects.length, 7, 'wrong object count');
TestCase.assertEqual(objects[0].doubleCol, 7, "wrong property value");
TestCase.assertEqual(objects[1].doubleCol, 8, "wrong property value");
var threeObjects = realm.objects('TestObject', "doubleCol < 5");
TestCase.assertEqual(threeObjects.length, 3, "wrong results count");
realm.delete(threeObjects);
TestCase.assertEqual(objects.length, 4, 'wrong object count');
TestCase.assertEqual(threeObjects.length, 0, 'threeObject should have been deleted');
}); });
}, },

View File

@ -28,7 +28,7 @@ var TestUtil = {
var TestCase = { var TestCase = {
assertEqual: function() { assertEqual: function() {
if (arguments[0] !== arguments[1]) { if (arguments[0] !== arguments[1]) {
var message = "'" + arguments[0] + "' does not equal '" + arguments[1] + "'"; var message = "'" + arguments[0] + "' does not equal expected value '" + arguments[1] + "'";
if (arguments.length == 3) { if (arguments.length == 3) {
message = arguments[2] + "\n" + message; message = arguments[2] + "\n" + message;
} }