create RealmObject class and make isValid a member

This commit is contained in:
Ari Lazier 2016-04-26 16:30:04 -07:00
parent 988f723f49
commit b59784670b
7 changed files with 60 additions and 41 deletions

View File

@ -151,7 +151,6 @@ export default class Realm {
// Non-mutating methods: // Non-mutating methods:
util.createMethods(Realm.prototype, objectTypes.REALM, [ util.createMethods(Realm.prototype, objectTypes.REALM, [
'close', 'close',
'isValid',
]); ]);
// Mutating methods: // Mutating methods:

View File

@ -23,10 +23,18 @@ import { getterForProperty, setterForProperty } from './util';
const registeredConstructors = {}; const registeredConstructors = {};
export default class RealmObject {
}
// Non-mutating methods:
createMethods(RealmObject.prototype, objectTypes.OBJECT, [
'isValid',
]);
export function create(realmId, info) { export function create(realmId, info) {
let schema = info.schema; let schema = info.schema;
let constructor = (registeredConstructors[realmId] || {})[schema.name]; let constructor = (registeredConstructors[realmId] || {})[schema.name];
let object = constructor ? Object.create(constructor.prototype) : {}; let object = Object.create(constructor ? constructor.prototype : RealmObject.prototype);
object[keys.realm] = realmId; object[keys.realm] = realmId;
object[keys.id] = info.id; object[keys.id] = info.id;

View File

@ -138,7 +138,6 @@ class Realm {
static void create(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &); static void create(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
static void delete_one(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &); static void delete_one(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
static void delete_all(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &); static void delete_all(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
static void is_object_valid(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
static void write(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &); static void write(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
static void add_listener(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &); static void add_listener(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
static void remove_listener(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &); static void remove_listener(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
@ -213,7 +212,6 @@ struct RealmClass : ClassDefinition<T, SharedRealm> {
{"create", wrap<Realm::create>}, {"create", wrap<Realm::create>},
{"delete", wrap<Realm::delete_one>}, {"delete", wrap<Realm::delete_one>},
{"deleteAll", wrap<Realm::delete_all>}, {"deleteAll", wrap<Realm::delete_all>},
{"isValid", wrap<Realm::is_object_valid>},
{"write", wrap<Realm::write>}, {"write", wrap<Realm::write>},
{"addListener", wrap<Realm::add_listener>}, {"addListener", wrap<Realm::add_listener>},
{"removeListener", wrap<Realm::remove_listener>}, {"removeListener", wrap<Realm::remove_listener>},
@ -233,11 +231,13 @@ inline typename T::Function Realm<T>::create_constructor(ContextType ctx) {
FunctionType collection_constructor = ObjectWrap<T, CollectionClass<T>>::create_constructor(ctx); FunctionType collection_constructor = ObjectWrap<T, CollectionClass<T>>::create_constructor(ctx);
FunctionType list_constructor = ObjectWrap<T, ListClass<T>>::create_constructor(ctx); FunctionType list_constructor = ObjectWrap<T, ListClass<T>>::create_constructor(ctx);
FunctionType results_constructor = ObjectWrap<T, ResultsClass<T>>::create_constructor(ctx); FunctionType results_constructor = ObjectWrap<T, ResultsClass<T>>::create_constructor(ctx);
FunctionType realm_object_constructor = ObjectWrap<T, RealmObjectClass<T>>::create_constructor(ctx);
PropertyAttributes attributes = PropertyAttributes(ReadOnly | DontEnum | DontDelete); PropertyAttributes attributes = PropertyAttributes(ReadOnly | DontEnum | DontDelete);
Object::set_property(ctx, realm_constructor, "Collection", collection_constructor, attributes); Object::set_property(ctx, realm_constructor, "Collection", collection_constructor, attributes);
Object::set_property(ctx, realm_constructor, "List", list_constructor, attributes); Object::set_property(ctx, realm_constructor, "List", list_constructor, attributes);
Object::set_property(ctx, realm_constructor, "Results", results_constructor, attributes); Object::set_property(ctx, realm_constructor, "Results", results_constructor, attributes);
Object::set_property(ctx, realm_constructor, "RealmObject", realm_object_constructor, attributes);
return realm_constructor; return realm_constructor;
} }
@ -457,19 +457,6 @@ void Realm<T>::delete_all(ContextType ctx, ObjectType this_object, size_t argc,
} }
} }
template<typename T>
void Realm<T>::is_object_valid(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
validate_argument_count(argc, 1);
ObjectType arg = Value::validated_to_object(ctx, arguments[0]);
if (Object::template is_instance<RealmObjectClass<T>>(ctx, arg)) {
return_value.set(get_internal<T, RealmObjectClass<T>>(arg)->is_valid());
}
else {
throw std::runtime_error("Object is not a Realm Object");
}
}
template<typename T> template<typename T>
void Realm<T>::write(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { void Realm<T>::write(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
validate_argument_count(argc, 1); validate_argument_count(argc, 1);
@ -484,7 +471,7 @@ void Realm<T>::write(ContextType ctx, ObjectType this_object, size_t argc, const
} }
catch (std::exception &e) { catch (std::exception &e) {
realm->cancel_transaction(); realm->cancel_transaction();
throw e; throw;
} }
realm->commit_transaction(); realm->commit_transaction();

View File

@ -46,6 +46,8 @@ class RealmObject {
static void get_property(ContextType, ObjectType, const String &, ReturnValue &); static void get_property(ContextType, ObjectType, const String &, ReturnValue &);
static bool set_property(ContextType, ObjectType, const String &, ValueType); static bool set_property(ContextType, ObjectType, const String &, ValueType);
static std::vector<String> get_property_names(ContextType, ObjectType); static std::vector<String> get_property_names(ContextType, ObjectType);
static void is_valid(ContextType, ObjectType, size_t, const ValueType [], ReturnValue &);
}; };
template<typename T> template<typename T>
@ -59,8 +61,23 @@ struct RealmObjectClass : ClassDefinition<T, realm::Object> {
wrap<RealmObject::set_property>, wrap<RealmObject::set_property>,
wrap<RealmObject::get_property_names>, wrap<RealmObject::get_property_names>,
}; };
using ContextType = typename T::Context;
using ObjectType = typename T::Object;
using ValueType = typename T::Value;
using ReturnValue = js::ReturnValue<T>;
MethodMap<T> const methods = {
{"isValid", wrap<RealmObject::is_valid>},
};
}; };
template<typename T>
void RealmObject<T>::is_valid(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
return_value.set(get_internal<T, RealmObjectClass<T>>(this_object)->is_valid());
}
template<typename T> template<typename T>
typename T::Object RealmObject<T>::create_instance(ContextType ctx, realm::Object &realm_object) { typename T::Object RealmObject<T>::create_instance(ContextType ctx, realm::Object &realm_object) {
static String prototype_string = "prototype"; static String prototype_string = "prototype";

View File

@ -48,7 +48,7 @@ namespace realm {
const ObjectSchema &get_object_schema() { return *m_object_schema; } const ObjectSchema &get_object_schema() { return *m_object_schema; }
Row row() { return m_row; } Row row() { return m_row; }
bool is_valid() { return m_row.is_attached(); } bool is_valid() const { return m_row.is_attached(); }
private: private:
SharedRealm m_realm; SharedRealm m_realm;
@ -148,8 +148,6 @@ namespace realm {
template <typename ValueType, typename ContextType> template <typename ValueType, typename ContextType>
inline void Object::set_property_value(ContextType ctx, std::string prop_name, ValueType value, bool try_update) inline void Object::set_property_value(ContextType ctx, std::string prop_name, ValueType value, bool try_update)
{ {
verify_attached();
const Property *prop = m_object_schema->property_for_name(prop_name); const Property *prop = m_object_schema->property_for_name(prop_name);
if (!prop) { if (!prop) {
throw InvalidPropertyException(m_object_schema->name, prop_name, throw InvalidPropertyException(m_object_schema->name, prop_name,
@ -161,8 +159,6 @@ namespace realm {
template <typename ValueType, typename ContextType> template <typename ValueType, typename ContextType>
inline ValueType Object::get_property_value(ContextType ctx, std::string prop_name) inline ValueType Object::get_property_value(ContextType ctx, std::string prop_name)
{ {
verify_attached();
const Property *prop = m_object_schema->property_for_name(prop_name); const Property *prop = m_object_schema->property_for_name(prop_name);
if (!prop) { if (!prop) {
throw InvalidPropertyException(m_object_schema->name, prop_name, throw InvalidPropertyException(m_object_schema->name, prop_name,
@ -176,6 +172,8 @@ namespace realm {
{ {
using Accessor = NativeAccessor<ValueType, ContextType>; using Accessor = NativeAccessor<ValueType, ContextType>;
verify_attached();
if (!m_realm->is_in_transaction()) { if (!m_realm->is_in_transaction()) {
throw MutationOutsideTransactionException("Can only set property values within a transaction."); throw MutationOutsideTransactionException("Can only set property values within a transaction.");
} }
@ -240,6 +238,8 @@ namespace realm {
{ {
using Accessor = NativeAccessor<ValueType, ContextType>; using Accessor = NativeAccessor<ValueType, ContextType>;
verify_attached();
size_t column = property.table_column; size_t column = property.table_column;
if (property.is_nullable && m_row.is_null(column)) { if (property.is_nullable && m_row.is_null(column)) {
return Accessor::null_value(ctx); return Accessor::null_value(ctx);

View File

@ -431,5 +431,29 @@ module.exports = BaseTest.extend({
object.dataCol = [1]; object.dataCol = [1];
}); });
}); });
} },
testObjectConstructor: function() {
var realm = new Realm({schema: [schemas.TestObject]});
realm.write(function() {
var obj = realm.create('TestObject', {doubleCol: 1});
TestCase.assertTrue(obj instanceof Realm.RealmObject);
});
},
testIsValid: function() {
var realm = new Realm({schema: [schemas.TestObject]});
var obj;
realm.write(function() {
obj = realm.create('TestObject', {doubleCol: 1});
TestCase.assertEqual(obj.isValid(), true);
realm.delete(obj);
TestCase.assertEqual(obj.isValid(), false);
});
TestCase.assertEqual(obj.isValid(), false);
TestCase.assertThrows(function() {
obj.doubleCol;
});
},
}); });

View File

@ -516,22 +516,6 @@ module.exports = BaseTest.extend({
TestCase.assertEqual(realm.objects('IntPrimaryObject').length, 0); TestCase.assertEqual(realm.objects('IntPrimaryObject').length, 0);
}, },
testIsValid: function() {
var realm = new Realm({schema: [schemas.TestObject]});
var obj;
realm.write(function() {
obj = realm.create('TestObject', {doubleCol: 1});
TestCase.assertEqual(realm.isValid(obj), true);
realm.delete(obj);
TestCase.assertEqual(realm.isValid(obj), false);
});
TestCase.assertEqual(realm.isValid(obj), false);
TestCase.assertThrows(function() {
obj.doubleCol;
});
},
testRealmObjects: function() { testRealmObjects: function() {
var realm = new Realm({schema: [schemas.PersonObject, schemas.DefaultValues, schemas.TestObject]}); var realm = new Realm({schema: [schemas.PersonObject, schemas.DefaultValues, schemas.TestObject]});