Skip to content

Commit 815f19f

Browse files
committed
minor improvements (#61)
1 parent 9961f61 commit 815f19f

File tree

4 files changed

+136
-36
lines changed

4 files changed

+136
-36
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,6 @@
6464
"karma-mocha": "^1.3.0",
6565
"karma-script-launcher": "^1.0.0",
6666
"load-grunt-tasks": "^3.5.2",
67-
"mocha": "^5.0.1"
67+
"mocha": "^5.0.4"
6868
}
6969
}

src/component.js

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -725,11 +725,6 @@ function createClass(classId) {
725725
'return function id () { return __body.call(this) };'
726726
)(body);
727727

728-
// classInfo
729-
if ($metamodel.inheritFrom(classId, '_Component')) {
730-
config.classInfo = classId + 'Info';
731-
}
732-
733728
// create link to db
734729
$db.store[classId][config._id] = config;
735730

@@ -758,19 +753,19 @@ function createClass(classId) {
758753
}
759754

760755
/**
761-
* @method addId
756+
* @method addIdClass
762757
* @param {Function} Class a class
763758
* @param {String} classId name of the class
764759
* @private
765760
* @description Add an id method to a class that will return its id
766761
*/
767-
function addId(Class, classId) {
762+
function addIdClass(Class, classId) {
768763
var body = function body() {
769764
return classId;
770765
};
771766
Class.id = new Function(
772767
'__body',
773-
'return function id (prop, val) { return __body.call(this, prop, val) };'
768+
'return function id () { return __body.call(this) };'
774769
)(body);
775770
}
776771

@@ -1461,15 +1456,15 @@ function addStructure(path, name, model, id) {
14611456
* @param {Function} Class Class
14621457
* @param {String} classId name of the class
14631458
* @private
1464-
* @description * Add methods to a component.
1459+
* @description Add methods to a component.
14651460
* The call to these methods will invoke the workflow in order to check that inpouts / outputs are compliant with the model.
14661461
*/
14671462
function addMethods(model, Class, classId) {
14681463
var methods = getMethods(model);
14691464

14701465
methods.forEach(function method(methodName) {
14711466
var paramsName = getParamNames(classId, methodName);
1472-
var params = paramsName.join(',');
1467+
var params = paramsName.join(', ');
14731468
var paramsWithContext = '';
14741469

14751470
var body = function body() {
@@ -1506,7 +1501,7 @@ function addMethods(model, Class, classId) {
15061501

15071502
if (params) {
15081503
paramsName.unshift('context');
1509-
paramsWithContext = paramsName.join('');
1504+
paramsWithContext = paramsName.join(', ');
15101505

15111506
Class.prototype[methodName] = new Function(
15121507
'__body',
@@ -1560,7 +1555,7 @@ function addEvents(model, Class, classId) {
15601555
var events = getEvents(model);
15611556
events.forEach(function event(methodName) {
15621557
var paramsName = getParamNames(classId, methodName);
1563-
var params = paramsName.join(',');
1558+
var params = paramsName.join(', ');
15641559

15651560
var body = function body() {
15661561
var systems = [];
@@ -1635,7 +1630,7 @@ function addEvents(model, Class, classId) {
16351630
* @description Add a on method to a component to add behaviors to the component
16361631
*/
16371632
function addOn(Class, classId) {
1638-
var body = function body(state, handler, useCoreAPI, isCore, context) {
1633+
var body = function body(state, action, useCoreAPI, isCore, context) {
16391634
var behaviorId = '';
16401635
var currentState = '';
16411636

@@ -1659,11 +1654,11 @@ function addOn(Class, classId) {
16591654
) {
16601655
$log.behaviorNotUnique(classId, state);
16611656
} else {
1662-
if ($workflow.validParamNumbers(classId, state, handler)) {
1657+
if ($workflow.validParamNumbers(classId, state, action)) {
16631658
behaviorId = $behavior.add(
16641659
this.id(),
16651660
state,
1666-
handler,
1661+
action,
16671662
useCoreAPI,
16681663
isCore,
16691664
context
@@ -1689,7 +1684,7 @@ function addOn(Class, classId) {
16891684
};
16901685
Class.prototype.on = new Function(
16911686
'__body',
1692-
'return function on (state, handler, useCoreAPI, isCore, context) { return __body.call(this, state, handler, useCoreAPI, isCore, context) };'
1687+
'return function on (state, action, useCoreAPI, isCore, context) { return __body.call(this, state, action, useCoreAPI, isCore, context) };'
16931688
)(body);
16941689
}
16951690

@@ -1701,7 +1696,7 @@ function addOn(Class, classId) {
17011696
* @description Add a on method to a class component to add behaviors to the class
17021697
*/
17031698
function addOnClass(Class, classId) {
1704-
var body = function body(state, handler, useCoreAPI, isCore, context) {
1699+
var body = function body(state, action, useCoreAPI, isCore, context) {
17051700
var behaviorId = '';
17061701
var currentState = '';
17071702

@@ -1725,11 +1720,11 @@ function addOnClass(Class, classId) {
17251720
) {
17261721
$log.behaviorNotUnique(classId, state);
17271722
} else {
1728-
if ($workflow.validParamNumbers(classId, state, handler)) {
1723+
if ($workflow.validParamNumbers(classId, state, action)) {
17291724
behaviorId = $behavior.add(
17301725
this.id(),
17311726
state,
1732-
handler,
1727+
action,
17331728
useCoreAPI,
17341729
isCore,
17351730
context
@@ -1755,7 +1750,7 @@ function addOnClass(Class, classId) {
17551750
};
17561751
Class.on = new Function(
17571752
'__body',
1758-
'return function on (state, handler, useCoreAPI, isCore, context) { return __body.call(this, state, handler, useCoreAPI, isCore, context) };'
1753+
'return function on (state, action, useCoreAPI, isCore, context) { return __body.call(this, state, action, useCoreAPI, isCore, context) };'
17591754
)(body);
17601755
}
17611756

@@ -1836,6 +1831,36 @@ function addDestroyClass(Class) {
18361831
)(body);
18371832
}
18381833

1834+
/**
1835+
* @method addRequireClass
1836+
* @param {Object} Class Class
1837+
* @private
1838+
* @description Require a component
1839+
*/
1840+
function addRequireClass(Class) {
1841+
var body = function body(id) {
1842+
return exports.get(id);
1843+
};
1844+
Class.require = new Function(
1845+
'__body',
1846+
'return function require (id) { return __body.call(this, id) };'
1847+
)(body);
1848+
}
1849+
1850+
/**
1851+
* @method addInitClass
1852+
* @param {Object} Class Class
1853+
* @private
1854+
* @description Init a class
1855+
*/
1856+
function addInitClass(Class) {
1857+
var body = function body() {};
1858+
Class.init = new Function(
1859+
'__body',
1860+
'return function init (conf) { return __body.call(this, conf) };'
1861+
)(body);
1862+
}
1863+
18391864
/**
18401865
* @method addClassInfoClass
18411866
* @param {Object} Class Class
@@ -1875,7 +1900,7 @@ function factory(config) {
18751900

18761901
store[classId] = Class;
18771902

1878-
addId(Class, classId);
1903+
addIdClass(Class, classId);
18791904

18801905
addProperties(config.model, Class, classId);
18811906
addMethods(config.model, Class, classId);
@@ -1885,8 +1910,11 @@ function factory(config) {
18851910
// inherit from _Component
18861911
if ($metamodel.inheritFrom(classId, '_Component')) {
18871912
addOn(Class, classId);
1913+
18881914
addOnClass(Class, classId);
18891915
addOffClass(Class, classId);
1916+
addRequireClass(Class);
1917+
addInitClass(Class);
18901918
addDestroyClass(Class);
18911919
addClassInfoClass(Class);
18921920
}

src/systems/classes/_Component-class.json

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"_name": "_Component",
1010
"_inherit": [],
1111
"_core": true,
12-
"classInfo": "property",
12+
"classInfo": "method",
1313
"on": "method",
1414
"off": "method",
1515
"require": "method",
@@ -25,8 +25,14 @@
2525
"_core": true,
2626
"on": {
2727
"params": [
28-
{ "name": "state", "type": "string" },
29-
{ "name": "handler", "type": "function" },
28+
{
29+
"name": "state",
30+
"type": "string"
31+
},
32+
{
33+
"name": "action",
34+
"type": "function"
35+
},
3036
{
3137
"name": "useCoreAPI",
3238
"type": "boolean",
@@ -49,20 +55,48 @@
4955
},
5056
"off": {
5157
"params": [
52-
{ "name": "state", "type": "string", "mandatory": false },
53-
{ "name": "behaviorId", "type": "string", "mandatory": false }
58+
{
59+
"name": "state",
60+
"type": "string",
61+
"mandatory": false
62+
},
63+
{
64+
"name": "behaviorId",
65+
"type": "string",
66+
"mandatory": false
67+
}
68+
]
69+
},
70+
"require": {
71+
"params": [
72+
{
73+
"name": "id",
74+
"type": "string"
75+
}
5476
]
5577
},
56-
"require": { "params": [{ "name": "id", "type": "string" }] },
57-
"destroy": { "params": [] },
78+
"destroy": {
79+
"params": []
80+
},
5881
"classInfo": {
59-
"type": "_ClassInfo",
60-
"readOnly": false,
61-
"mandatory": false,
62-
"default": {}
82+
"result": "_ClassInfo"
6383
},
64-
"init": { "params": [{ "name": "conf", "type": "object" }] },
65-
"error": { "params": [{ "name": "data", "type": "errorParam" }] }
84+
"init": {
85+
"params": [
86+
{
87+
"name": "conf",
88+
"type": "object"
89+
}
90+
]
91+
},
92+
"error": {
93+
"params": [
94+
{
95+
"name": "data",
96+
"type": "errorParam"
97+
}
98+
]
99+
}
66100
}
67101
},
68102
"behaviors": {
@@ -90,6 +124,15 @@
90124
"action": "function require(id) {\n return $component.get(id);\n}",
91125
"core": true,
92126
"useCoreAPI": true
127+
},
128+
"d152631c35813f2e": {
129+
"_id": "d152631c35813f2e",
130+
"component": "_Component",
131+
"state": "classInfo",
132+
"action":
133+
"function classInfo() { \n\tconst className = this.constructor.name;\n\tlet result;\n\t\n\tif (className !== 'Function') {\n\t result = this.require(className + 'Info');\n\t}\n\t\n\treturn result;\n}",
134+
"useCoreAPI": false,
135+
"core": true
93136
}
94137
},
95138
"types": {},

test/runtime/component-spec.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ describe('a System Runtime component', function () {
1717
'lastName': 'property',
1818
'address': 'property',
1919
'likes': 'property',
20+
'fullName': 'method',
2021
'children': 'collection',
2122
'father': 'link',
2223
'moving': 'event'
@@ -49,6 +50,9 @@ describe('a System Runtime component', function () {
4950
'mandatory': false,
5051
'default': []
5152
},
53+
'fullName': {
54+
'result': 'string'
55+
},
5256
'father': {
5357
'type': 'Person',
5458
'readOnly': false,
@@ -63,6 +67,12 @@ describe('a System Runtime component', function () {
6367
},
6468
'moving': {}
6569
});
70+
71+
metamodel.schema({
72+
'_name': 'Teacher',
73+
'_inherit': ['Person']
74+
});
75+
6676
metamodel.create();
6777
});
6878

@@ -236,7 +246,7 @@ describe('a System Runtime component', function () {
236246
'firstName': 'Yoda',
237247
'lastName': 'Master'
238248
});
239-
249+
240250
expect(yoda.firstName()).equal('Yoda');
241251
});
242252

@@ -465,4 +475,23 @@ describe('a System Runtime component', function () {
465475
expect(runtime.bundle().indexOf('Shadow')).equal(-1);
466476
});
467477

478+
it('can call a parent method', function () {
479+
const Person = runtime.require('Person');
480+
const Teacher = runtime.require('Teacher');
481+
482+
Person.on('fullName', () => {
483+
return this.firstName() + ' ' + this.lastName();
484+
});
485+
486+
Teacher.on('fullName', () => {
487+
return 'Great Teacher ' + this.require('Person').fullName(this);
488+
});
489+
490+
const eikichi = new Teacher({
491+
'firstName': 'Eikichi',
492+
'lastName': 'Onizuka'
493+
});
494+
495+
expect(eikichi.fullName()).equal('Great Teacher Eikichi Onizuka');
496+
});
468497
});

0 commit comments

Comments
 (0)