vadimtsushko avatar vadimtsushko committed 303ff17

Objectory: Changes in schema structure

Comments (0)

Files changed (4)

lib/objectory/Objectory.dart

 typedef PersistentObject FactoryMethod();
+class ClassSchema{
+  String className;
+  FactoryMethod factoryMethod;
+  Set<String> properties;
+  Map<String,String> links;  
+  ClassSchema(this.className,this.factoryMethod,List<String> propertyList,
+    [this.links]){
+    properties = new Set<String>.from(propertyList);
+  }
+    
+}
 interface Objectory{  
-  void registerClass(String className, FactoryMethod fm);
+  void registerClass(ClassSchema schema);
   PersistentObject newInstance(String className);
   Future<PersistentObject> findOne(String className,[Map selector]);
   Future<List<PersistentObject>> find(String className,[Map selector]);
   void remove(PersistentObject persistentObject);
   Future<bool> open(String database, [String url]);
   Future<bool> dropDb();
+  ClassSchema getSchema(String className);
 }
 abstract class ObjectoryBaseImpl implements Objectory{
-  Map<String,FactoryMethod> factories;
+  Map<String,ClassSchema> schemata;
   ObjectoryBaseImpl(){
-    factories = new  Map<String,FactoryMethod>();
+    schemata = new  Map<String,ClassSchema>();
+  }
+  ClassSchema getSchema(String className){
+    return schemata[className];
   }
   PersistentObject newInstance(String className){
-    if (factories.containsKey(className)){
-      return factories[className]();
+    if (schemata.containsKey(className)){
+      return schemata[className].factoryMethod();
     }
     throw "Class $className have not been registered in Objectory";
   }
     if (result.isRoot()){
       result.id = map["_id"];    
     }      
-    for (var key in map.getKeys()){
-      var value = map[key];
-      if (value is Map){
-        if (value.containsKey("_pt")){
-          PersistentObject subComponent = map2Object(value["_pt"],value);
-          result.setProperty(key,subComponent);  
-          result.clearDirtyStatus();
-        }
-      }
+    ClassSchema classSchema = schemata[className];
+    if (classSchema.links !== null){      
+      classSchema.links.forEach((property,linkClass){
+        PersistentObject linkObject = map2Object(linkClass,map[property]);
+        result.setProperty(property,linkObject);  
+        result.clearDirtyStatus();
+      });
     }
     return result;
   }
-  void clearFactories(){
-    factories.clear();
+  void clearSchemata(){
+    schemata.clear();
   }
-  void registerClass(String className, FactoryMethod fm){
-    factories[className] = fm;
+  void registerClass(ClassSchema schema){
+    schemata[schema.className] = schema;
   }
 }

lib/objectory/PersistentObject.dart

 abstract class PersistentObjectBase extends MapProxy implements PersistentObject{  
   bool setupMode;
   Set<String> dirtyFields;
-  Set<String> _properties;
-  void setPropertyList(List<String> propertyList){
-    _properties = new Set<String>.from(propertyList);
-  }  
   PersistentObjectBase(){        
     if (isRoot()){
       map["_id"] = null;
     }                
     init();
     dirtyFields = new Set<String>();
-    if (_properties === null){
-      throw "$type: Properties list must be set in method init()";
-    }
   }  
   void setDirty(String fieldName){
     if (dirtyFields === null){
     return !dirtyFields.isEmpty();
   }
   noSuchMethod(String function_name, List args) {
+    ClassSchema schema = objectory.getSchema(type);
+    if (schema === null){
+      throw "Class $type have not been registered in Objectory";
+    }
     if (args.length == 0 && function_name.startsWith("get:")) {
       //synthetic getter
       var property = function_name.replaceFirst("get:", "");
-      if (_properties.contains(property)) {
+      if (schema.properties.contains(property)) {
         return this[property];
       }
       else{
       //synthetic setter
       var value = args[0];
       var property = function_name.replaceFirst("set:", "");      
-      if (_properties.contains(property)) {
+      if (schema.properties.contains(property)) {
         onValueChanging(property, value);
         this[property] = value;
         if (value is InnerPersistentObject){
 abstract class InnerPersistentObject extends PersistentObjectBase{
   PersistentObject parent;
   String pathToMe;
-  InnerPersistentObject():super(){
-    map["_pt"] = type;
-  }  
   bool isRoot()=>false;
 }

tests/objectory/DomainModel.dart

 
 class Author extends RootPersistentObject implements IAuthor{  
   String get type()=>'Author';
-  init(){
-    setPropertyList(["name","age","email"]);
-  }
   set name(String value){
     if (value is String){
       value = value.toUpperCase();
 class Person extends RootPersistentObject implements IPerson{  
   String get type()=>"Person";
   init(){    
-    setPropertyList(["firstName","lastName","birthday","address"]);
     address = new Address();
   }
 }  
 class Address extends InnerPersistentObject implements IAddress{  
   String get type()=>"Address";
-  init(){
-    setPropertyList(["cityName","zipCode","streetName"]);
-  }
 }
+
+void registerClasses(){
+  objectory.registerClass(new ClassSchema('Author',
+      ()=>new Author(),
+      ["name","age","email"]));
+  objectory.registerClass(new ClassSchema('Address',
+    ()=>new Address(),
+    ["cityName","zipCode","streetName"]));
+  objectory.registerClass(new ClassSchema('Person',
+    ()=>new Person(),
+    ["firstName","lastName","birthday","address"],
+    links: {"address": "Address"}));
+}

tests/objectory/ObjectoryImplVmTest.dart

 #source("../../lib/helpers/MapProxy.dart");
 Future<bool> setUpObjectory(){
   var res = new Completer();
-  objectory.clearFactories();
+//  objectory.clearFactories();
   objectory.open("ObjectoryTest").then((_){    
     objectory.dropDb();
-    objectory.registerClass('Author',()=>new Author());    
-    objectory.registerClass('Person',()=>new Person());
-    objectory.registerClass('Address',()=>new Address());
+    registerClasses();
     res.complete(true);
   });    
   return res.future;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.