Dart Documentationpersistent_objectBasePersistentObject

BasePersistentObject class

class BasePersistentObject {
 final Map _map = objectory.dataMapDecorator(new LinkedHashMap());
 Map get map => _map;
 set map(Map newValue) {
   _setMap(newValue);
 }
 Set<String> _dirtyFields;
 Map<String,dynamic> _compoundProperties;
 bool saveOnUpdate = false;
 BasePersistentObject() {
   _setMap(map);
 }
 void _setMap(Map newValue) {
   if (newValue == null || newValue.isEmpty) {
     _initMap();
   } else {
     _map.clear();
     newValue.forEach((k, v) => _map[k] = v);
   }
   _compoundProperties = new Map<String,dynamic>();
   init();
   _dirtyFields = new Set<String>();
 }
 Set<String> get dirtyFields => _dirtyFields;
 EmbeddedPersistentObject getEmbeddedObject(Type classType, String property) {
   EmbeddedPersistentObject result = _compoundProperties[property];
   if (result == null) {
     result = objectory.newInstance(classType);
     result._setMap(map[property]);
     map[property] = result.map;
     result._parent = this;
     result._pathToMe = property;
   }
   return result;
 }
 PersistentList getPersistentList(Type classType, String property) {
   PersistentList result = _compoundProperties[property];
   if (result == null) {
     result = new PersistentList(this,classType,property);
     _compoundProperties[property] = result;
   }
   return result;
 }
 
 PersistentObject getLinkedObject(String property) {
   DbRef dbRef = map[property];
   if (dbRef == null) {
     return null;
   }
   Type classType = objectory.getClassTypeByCollection(dbRef.collection);
   return objectory.findInCacheOrGetProxy(dbRef.id,classType);
 }

 setLinkedObject(String property, PersistentObject value) {
   if (value == null) {
     map[property] = null;
   } else {
     if (value.id == null) {
       throw new Exception('Attemt to set link to unsaved object: $value');
     }
     onValueChanging(property, value.dbRef);
     map[property] = value.dbRef;
   }
 }
 void _initMap() {
 }

 void setDirty(String fieldName) {
   if (_dirtyFields == null){
     return;
   }
   _dirtyFields.add(fieldName);
 }

 void clearDirtyStatus() {
   _dirtyFields.clear();
 }

 void onValueChanging(String fieldName, newValue) {
   setDirty(fieldName);
 }

 isDirty() {
   return !_dirtyFields.isEmpty;
 }


 void setProperty(String property, value){
   onValueChanging(property, value);
   this.map[property] = value;
 }

 dynamic getProperty(String property){
   return this.map[property];
 }

 String toString()=>"$collectionName($map)";

 void init(){}
 
 /// Name of MongoDB collection where instance of this class would  be persistet in DB.
 /// By default equals to class name, but may be overwritten
 String get collectionName => runtimeType.toString();

 Future<PersistentObject> fetchLinks(){
   var dbRefs = new List<DbRef>();
   getDbRefsFromMap(map, dbRefs);
   var objects = dbRefs.map((each) => objectory.dbRef2Object(each));
   return Future.forEach(objects,(each) => each.fetch()).then((_)=>new Future.value(this));
 }

 getDbRefsFromMap(Map map, List result){
   for(var each in map.values){
     if (each is DbRef) {
       result.add(each);
     }
     if (each is Map) {
       getDbRefsFromMap(each, result);
     }
     if (each is List) {
       getDbRefsFromList(each, result);
     }
   }
 }
 getDbRefsFromList(List list, List result){
   for (var each in list) {
     if (each is DbRef) {
       result.add(each);
     }
     if (each is Map) {
       getDbRefsFromMap(each, result);
     }
     if (each is List) {
       getDbRefsFromList(each, result);
     }
   }
 }

}

Subclasses

EmbeddedPersistentObject, PersistentObject

Constructors

new BasePersistentObject() #

Creates a new Object instance.

Object instances have no meaningful state, and are only useful through their identity. An Object instance is equal to itself only.

docs inherited from Object
BasePersistentObject() {
 _setMap(map);
}

Properties

final String collectionName #

Name of MongoDB collection where instance of this class would be persistet in DB. By default equals to class name, but may be overwritten

String get collectionName => runtimeType.toString();

final Set<String> dirtyFields #

Set<String> get dirtyFields => _dirtyFields;

Map map #

Map get map => _map;
set map(Map newValue) {
 _setMap(newValue);
}

bool saveOnUpdate #

bool saveOnUpdate = false

Methods

void clearDirtyStatus() #

void clearDirtyStatus() {
 _dirtyFields.clear();
}
Future<PersistentObject> fetchLinks(){
 var dbRefs = new List<DbRef>();
 getDbRefsFromMap(map, dbRefs);
 var objects = dbRefs.map((each) => objectory.dbRef2Object(each));
 return Future.forEach(objects,(each) => each.fetch()).then((_)=>new Future.value(this));
}

dynamic getDbRefsFromList(List list, List result) #

getDbRefsFromList(List list, List result){
 for (var each in list) {
   if (each is DbRef) {
     result.add(each);
   }
   if (each is Map) {
     getDbRefsFromMap(each, result);
   }
   if (each is List) {
     getDbRefsFromList(each, result);
   }
 }
}

dynamic getDbRefsFromMap(Map map, List result) #

getDbRefsFromMap(Map map, List result){
 for(var each in map.values){
   if (each is DbRef) {
     result.add(each);
   }
   if (each is Map) {
     getDbRefsFromMap(each, result);
   }
   if (each is List) {
     getDbRefsFromList(each, result);
   }
 }
}

EmbeddedPersistentObject getEmbeddedObject(Type classType, String property) #

EmbeddedPersistentObject getEmbeddedObject(Type classType, String property) {
 EmbeddedPersistentObject result = _compoundProperties[property];
 if (result == null) {
   result = objectory.newInstance(classType);
   result._setMap(map[property]);
   map[property] = result.map;
   result._parent = this;
   result._pathToMe = property;
 }
 return result;
}

PersistentObject getLinkedObject(String property) #

PersistentObject getLinkedObject(String property) {
 DbRef dbRef = map[property];
 if (dbRef == null) {
   return null;
 }
 Type classType = objectory.getClassTypeByCollection(dbRef.collection);
 return objectory.findInCacheOrGetProxy(dbRef.id,classType);
}

PersistentList getPersistentList(Type classType, String property) #

PersistentList getPersistentList(Type classType, String property) {
 PersistentList result = _compoundProperties[property];
 if (result == null) {
   result = new PersistentList(this,classType,property);
   _compoundProperties[property] = result;
 }
 return result;
}

dynamic getProperty(String property) #

dynamic getProperty(String property){
 return this.map[property];
}

void init() #

void init(){}

dynamic isDirty() #

isDirty() {
 return !_dirtyFields.isEmpty;
}

void onValueChanging(String fieldName, newValue) #

void onValueChanging(String fieldName, newValue) {
 setDirty(fieldName);
}

void setDirty(String fieldName) #

void setDirty(String fieldName) {
 if (_dirtyFields == null){
   return;
 }
 _dirtyFields.add(fieldName);
}

dynamic setLinkedObject(String property, PersistentObject value) #

setLinkedObject(String property, PersistentObject value) {
 if (value == null) {
   map[property] = null;
 } else {
   if (value.id == null) {
     throw new Exception('Attemt to set link to unsaved object: $value');
   }
   onValueChanging(property, value.dbRef);
   map[property] = value.dbRef;
 }
}

void setProperty(String property, value) #

void setProperty(String property, value){
 onValueChanging(property, value);
 this.map[property] = value;
}

String toString() #

Returns a string representation of this object.

docs inherited from Object
String toString()=>"$collectionName($map)";