From 01736eb6a8afd13e4cb5a5d18d7ceed97e2617cd Mon Sep 17 00:00:00 2001 From: jagi Date: Thu, 1 Oct 2015 14:49:48 +0200 Subject: [PATCH] Move all modules code to initDefinition and initClass events --- lib/modules/behaviors/init_class.js | 3 +++ lib/modules/behaviors/init_definition.js | 3 +-- lib/modules/behaviors/init_schema.js | 5 ----- lib/modules/core/classes.js | 5 +++++ lib/modules/core/sub_schema.js | 0 lib/modules/events/init_schema.js | 5 ----- lib/modules/fields/init_class.js | 4 ++++ lib/modules/fields/init_definition.js | 2 -- lib/modules/fields/init_schema.js | 5 ----- lib/modules/indexes/init_class.js | 3 +++ lib/modules/indexes/init_definition.js | 1 - lib/modules/indexes/init_schema.js | 5 ----- lib/modules/methods/init_class.js | 3 +++ lib/modules/methods/init_definition.js | 1 - lib/modules/methods/init_schema.js | 5 ----- package.js | 8 +------- test/core/core_extend.js | 12 ++++++------ test/core/core_inheritance.js | 13 +++++++------ 18 files changed, 33 insertions(+), 50 deletions(-) delete mode 100644 lib/modules/behaviors/init_schema.js delete mode 100644 lib/modules/core/sub_schema.js delete mode 100644 lib/modules/events/init_schema.js delete mode 100644 lib/modules/fields/init_schema.js delete mode 100644 lib/modules/indexes/init_schema.js delete mode 100644 lib/modules/methods/init_schema.js diff --git a/lib/modules/behaviors/init_class.js b/lib/modules/behaviors/init_class.js index 116dc33..07d2a4c 100644 --- a/lib/modules/behaviors/init_class.js +++ b/lib/modules/behaviors/init_class.js @@ -11,7 +11,10 @@ classMethods.getBehaviors = function() { Astro.eventManager.on( 'initClass', function onInitClassBehaviors() { var Class = this; + var schema = Class.schema; _.extend(Class, classMethods); + + schema.behaviors = schema.behaviors || {}; } ); diff --git a/lib/modules/behaviors/init_definition.js b/lib/modules/behaviors/init_definition.js index e35c9a0..6272c75 100644 --- a/lib/modules/behaviors/init_definition.js +++ b/lib/modules/behaviors/init_definition.js @@ -53,8 +53,7 @@ Astro.eventManager.on( Class.extend(classBehavior.definition); }); - // Add the "behaviors" attribute to the schema. - schema.behaviors = schema.behaviors || {}; + // Add behaviors to the schema. _.extend(schema.behaviors, behaviorsDefinitions); } } diff --git a/lib/modules/behaviors/init_schema.js b/lib/modules/behaviors/init_schema.js deleted file mode 100644 index 7007237..0000000 --- a/lib/modules/behaviors/init_schema.js +++ /dev/null @@ -1,5 +0,0 @@ -Astro.eventManager.on( - 'initSchema', function onInitSchemaBehavior(schemaDefinition) { - var schema = this; - } -); diff --git a/lib/modules/core/classes.js b/lib/modules/core/classes.js index d5b040f..23e2118 100644 --- a/lib/modules/core/classes.js +++ b/lib/modules/core/classes.js @@ -72,6 +72,11 @@ Astro.createClass = Astro.Class = function(schemaDefinition) { eventHandler.call(Class); }); + if (ParentClass) { + _.each(ParentClass.schema.definitions, function(schemaDefinition) { + Class.extend(schemaDefinition); + }); + } Class.extend(schemaDefinition); return Class; diff --git a/lib/modules/core/sub_schema.js b/lib/modules/core/sub_schema.js deleted file mode 100644 index e69de29..0000000 diff --git a/lib/modules/events/init_schema.js b/lib/modules/events/init_schema.js deleted file mode 100644 index 7985399..0000000 --- a/lib/modules/events/init_schema.js +++ /dev/null @@ -1,5 +0,0 @@ -Astro.eventManager.on( - 'initSchema', function onInitSchemaEvents(schemaDefinition) { - var schema = this; - } -); diff --git a/lib/modules/fields/init_class.js b/lib/modules/fields/init_class.js index 160efa0..55f6f73 100644 --- a/lib/modules/fields/init_class.js +++ b/lib/modules/fields/init_class.js @@ -19,8 +19,12 @@ classMethods.getFields = function() { Astro.eventManager.on( 'initClass', function onInitClassFields(schemaDefinition) { var Class = this; + var schema = Class.schema; // Add fields methods to the class. _.extend(Class, classMethods); + + schema.fields = schema.fields || {}; + schema.fieldsNames = schema.fieldsNames || []; } ); diff --git a/lib/modules/fields/init_definition.js b/lib/modules/fields/init_definition.js index 07b1c8b..cdfc203 100644 --- a/lib/modules/fields/init_definition.js +++ b/lib/modules/fields/init_definition.js @@ -184,8 +184,6 @@ Astro.eventManager.on( }); // Add fields and fields names to the schema. - schema.fields = schema.fields || {}; - schema.fieldsNames = schema.fieldsNames || []; _.extend(schema.fields, fieldsDefinitions); var fieldsNames = _.keys(fieldsDefinitions); schema.fieldsNames = _.union(schema.fieldsNames, fieldsNames); diff --git a/lib/modules/fields/init_schema.js b/lib/modules/fields/init_schema.js deleted file mode 100644 index 0b44e2e..0000000 --- a/lib/modules/fields/init_schema.js +++ /dev/null @@ -1,5 +0,0 @@ -Astro.eventManager.on( - 'initSchema', function onInitSchemaFields(schemaDefinition) { - var schema = this; - } -); diff --git a/lib/modules/indexes/init_class.js b/lib/modules/indexes/init_class.js index 4a0d3b3..9390acc 100644 --- a/lib/modules/indexes/init_class.js +++ b/lib/modules/indexes/init_class.js @@ -7,8 +7,11 @@ classMethods.getIndexes = function() { Astro.eventManager.on( 'initClass', function onInitClassIndexes(schemaDefinition) { var Class = this; + var schema = Class.schema; // Add fields methods to the class. _.extend(Class, classMethods); + + schema.indexes = schema.indexes || {}; } ); diff --git a/lib/modules/indexes/init_definition.js b/lib/modules/indexes/init_definition.js index 0df1c81..6f5b759 100644 --- a/lib/modules/indexes/init_definition.js +++ b/lib/modules/indexes/init_definition.js @@ -79,7 +79,6 @@ Astro.eventManager.on( }); // Add indexes to the schema. - schema.indexes = schema.indexes || {}; _.extend(schema.indexes, indexesDefinitions); } } diff --git a/lib/modules/indexes/init_schema.js b/lib/modules/indexes/init_schema.js deleted file mode 100644 index cc0711a..0000000 --- a/lib/modules/indexes/init_schema.js +++ /dev/null @@ -1,5 +0,0 @@ -Astro.eventManager.on( - 'initSchema', function onInitSchemaIndexes(schemaDefinition) { - var schema = this; - } -); diff --git a/lib/modules/methods/init_class.js b/lib/modules/methods/init_class.js index 3fe14b5..f921e1d 100644 --- a/lib/modules/methods/init_class.js +++ b/lib/modules/methods/init_class.js @@ -13,8 +13,11 @@ classMethods.getMethods = function() { Astro.eventManager.on( 'initClass', function onInitClassMethods(schemaDefinition) { var Class = this; + var schema = Class.schema; // Extend class with class methods. _.extend(Class, classMethods); + + schema.methods = schema.methods || {}; } ); diff --git a/lib/modules/methods/init_definition.js b/lib/modules/methods/init_definition.js index 2362a3e..c048430 100644 --- a/lib/modules/methods/init_definition.js +++ b/lib/modules/methods/init_definition.js @@ -36,7 +36,6 @@ Astro.eventManager.on( }); // Add methods to the schema. - schema.methods = schema.methods || {}; _.extend(schema.methods, schemaDefinition.methods); } } diff --git a/lib/modules/methods/init_schema.js b/lib/modules/methods/init_schema.js deleted file mode 100644 index 400c65c..0000000 --- a/lib/modules/methods/init_schema.js +++ /dev/null @@ -1,5 +0,0 @@ -Astro.eventManager.on( - 'initSchema', function onInitSchemaMethods(schemaDefinition) { - var schema = this; - } -); diff --git a/package.js b/package.js index c26873b..8a6e6c4 100644 --- a/package.js +++ b/package.js @@ -28,7 +28,6 @@ Package.onUse(function(api) { 'lib/modules/core/event_manager.js', 'lib/modules/core/base_class.js', 'lib/modules/core/schema.js', - 'lib/modules/core/sub_schema.js', 'lib/modules/core/classes.js' ], ['client', 'server']); @@ -46,14 +45,12 @@ Package.onUse(function(api) { 'lib/modules/behaviors/create_behavior.js', 'lib/modules/behaviors/init_class.js', 'lib/modules/behaviors/init_definition.js', - 'lib/modules/behaviors/init_schema.js' ], ['client', 'server']); // Events module. api.addFiles([ 'lib/modules/events/init_class.js', 'lib/modules/events/init_definition.js', - 'lib/modules/events/init_schema.js' ], ['client', 'server']); // EJSON module. @@ -63,7 +60,6 @@ Package.onUse(function(api) { api.addFiles([ 'lib/modules/methods/init_class.js', 'lib/modules/methods/init_definition.js', - 'lib/modules/methods/init_schema.js' ], ['client', 'server']); // Fields module. @@ -90,14 +86,12 @@ Package.onUse(function(api) { 'lib/modules/fields/ejson.js', 'lib/modules/fields/init_class.js', 'lib/modules/fields/init_definition.js', - 'lib/modules/fields/init_schema.js' ], ['client', 'server']); // Indexes module. api.addFiles([ 'lib/modules/indexes/init_class.js', - 'lib/modules/indexes/init_definition.js', - 'lib/modules/indexes/init_schema.js' + 'lib/modules/indexes/init_definition.js' ], 'server'); api.export(['Astro', 'Astronomy'], ['client', 'server']); diff --git a/test/core/core_extend.js b/test/core/core_extend.js index ec7e88a..7830883 100644 --- a/test/core/core_extend.js +++ b/test/core/core_extend.js @@ -35,24 +35,24 @@ Tinytest.add('Core - Extend', function(test) { var extended = new Extended(); - test.isNotNull(Extended.getField('originalField'), + test.instanceOf(Extended.getField('originalField'), Astro.BaseField, 'The class should contain original fields' ); - test.isNotNull(Extended.getField('extendField'), + test.instanceOf(Extended.getField('extendField'), Astro.BaseField, 'The class should contain extended fields' ); - test.isNotNull(extended.originalMethod, + test.instanceOf(extended.originalMethod, Function, 'The class should contain original methods' ); - test.isNotNull(extended.extendMethod, + test.instanceOf(extended.extendMethod, Function, 'The class should contain extended methods' ); - test.isNotNull(Extended.getBehavior('timestamp'), + test.instanceOf(Extended.getBehavior('timestamp'), Astro.ClassBehavior, 'The class should contain original behaviors' ); - test.isNotNull(Extended.getBehavior('slug'), + test.instanceOf(Extended.getBehavior('slug'), Astro.ClassBehavior, 'The class should contain extended behaviors' ); }); diff --git a/test/core/core_inheritance.js b/test/core/core_inheritance.js index 5cc03de..133c10b 100644 --- a/test/core/core_inheritance.js +++ b/test/core/core_inheritance.js @@ -36,23 +36,24 @@ Tinytest.add('Core - Inheritance', function(test) { var childInheritance = new ChildInheritance(); - test.isNotNull(ChildInheritance.getField('parentField'), + test.instanceOf(ChildInheritance.getField('parentField'), Astro.BaseField, 'The child class should inherit parent fields' ); - test.isNotNull(ChildInheritance.getBehavior('timestamp'), + test.instanceOf( + ChildInheritance.getBehavior('timestamp'), Astro.ClassBehavior, 'The child class should inherit parent behaviors' ); - test.isNotNull(childInheritance.parentMethod, + test.instanceOf(childInheritance.parentMethod, Function, 'The child class should inherit parent methods' ); - test.isNotNull(ChildInheritance.getField('childField'), + test.instanceOf(ChildInheritance.getField('childField'), Astro.BaseField, 'The child class should have its own fields' ); - test.isNotNull(ChildInheritance.getBehavior('slug'), + test.instanceOf(ChildInheritance.getBehavior('slug'), Astro.ClassBehavior, 'The child class should have its own behaviors' ); - test.isNotNull(childInheritance.childMethod, + test.instanceOf(childInheritance.childMethod, Function, 'The child class should have its own methods' );