Skip to content

Commit

Permalink
Move all modules code to initDefinition and initClass events
Browse files Browse the repository at this point in the history
  • Loading branch information
lukejagodzinski committed Oct 1, 2015
1 parent 4f3312c commit 01736eb
Show file tree
Hide file tree
Showing 18 changed files with 33 additions and 50 deletions.
3 changes: 3 additions & 0 deletions lib/modules/behaviors/init_class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {};
}
);
3 changes: 1 addition & 2 deletions lib/modules/behaviors/init_definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
5 changes: 0 additions & 5 deletions lib/modules/behaviors/init_schema.js

This file was deleted.

5 changes: 5 additions & 0 deletions lib/modules/core/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Empty file removed lib/modules/core/sub_schema.js
Empty file.
5 changes: 0 additions & 5 deletions lib/modules/events/init_schema.js

This file was deleted.

4 changes: 4 additions & 0 deletions lib/modules/fields/init_class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || [];
}
);
2 changes: 0 additions & 2 deletions lib/modules/fields/init_definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 0 additions & 5 deletions lib/modules/fields/init_schema.js

This file was deleted.

3 changes: 3 additions & 0 deletions lib/modules/indexes/init_class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {};
}
);
1 change: 0 additions & 1 deletion lib/modules/indexes/init_definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ Astro.eventManager.on(
});

// Add indexes to the schema.
schema.indexes = schema.indexes || {};
_.extend(schema.indexes, indexesDefinitions);
}
}
Expand Down
5 changes: 0 additions & 5 deletions lib/modules/indexes/init_schema.js

This file was deleted.

3 changes: 3 additions & 0 deletions lib/modules/methods/init_class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {};
}
);
1 change: 0 additions & 1 deletion lib/modules/methods/init_definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ Astro.eventManager.on(
});

// Add methods to the schema.
schema.methods = schema.methods || {};
_.extend(schema.methods, schemaDefinition.methods);
}
}
Expand Down
5 changes: 0 additions & 5 deletions lib/modules/methods/init_schema.js

This file was deleted.

8 changes: 1 addition & 7 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);

Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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']);
Expand Down
12 changes: 6 additions & 6 deletions test/core/core_extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
});
13 changes: 7 additions & 6 deletions test/core/core_inheritance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);

Expand Down

0 comments on commit 01736eb

Please sign in to comment.