-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathramify.js
486 lines (446 loc) · 17 KB
/
ramify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
(function(name, definition) {
if (typeof define === 'function') {
// AMD
define(definition);
} else if (typeof module !== 'undefined' && module.exports) {
// Node.js
module.exports = definition();
} else {
// Browser
var Ramify = definition();
var global = this;
var old = global[name];
Ramify.noConflict = function() {
global[name] = old;
return Ramify;
};
global[name] = Ramify;
}
})('Ramify', function() {
'use strict';
/**
* Represents an error occured at the Ramify execution process.
* @class Ramify.RamifyError
* @param {string} [message] The message property of the created
* error instance
* @example
* var Ramify = require('ramify');
* ...
* throw new Ramify.RamifyError('Something went wrong');
* ...
* try {
* ...
* } catch (e if e instanceof Ramify.RamifyError) {}
*/
function RamifyError(message) {
this.name = this.constructor.name;
this.message = message || '';
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
} else {
this.stack = new Error().stack;
}
}
RamifyError.prototype = Object.create(Error.prototype);
RamifyError.prototype.constructor = RamifyError;
var _rami = null;
var _generalErrorHandler = null;
var _returnValue = null;
var _config = {
createAlias: false,
safeAlias: true
};
var _this = null;
var _properties = {};
/**
* Represents the inner Rami object of the Rami functions,
* assigned to `this` object.
* @class Rami
* @memberOf Ramify
* @inner
*/
function Rami() {}
_this = new Rami();
/**
* Adds functions to the Ramify instance that will be used within it.
* @function Ramify~Rami#call
* @this Ramify~Rami
* @param {!string} ramusName The name of the ramus to call.
* @param {*} [passedParameter] The value the will be applied as a parameter to the called function. Could be any value, even a Promise. If a Promise is passed, the Promise will be automatically evaluated and the resolved value will be passed as a parameter, otherwise the rejected value will be passed to the errorHandler function. If no errorHandler function is declared, the generalErrorHandler function will be called.
* @param {function} [errorHandler] A function that will handle the errors that can happen at a Promise that is passed as a parameter.
* @returns {*} The resolved value.
* @example
* var myController = new Ramify();
* function somePromise () {
* return new Promise(function (resolve, reject) {
* resolve('someValue');
* });
* }
* myController.add({
* someFunction: () {
* return this.call('someOtherFunction', somePromise(), function (err) {
* console.log(err);
* });
* },
* someOtherFunction: (value) {
* return this.call('endFunction', value + '123');
* },
* endFunction: function (value) {
* return value;
* }
* });
* myController.call('someFunction');
*/
Rami.prototype.call = function() {
return _call.apply(_this, arguments);
};
/**
* Attached to `this` of a function declared within Ramify instance, either to get or set a property.
*
* _Allthough a property can be set directly to `this` object, (`this.someProp = someValue`) it is not recommended to write on to the Rami object. `prop` method is a safe way to pass values between Rami functions._
* @function Ramify~Rami#prop
* @param {!string} propertyName The name of the property to get or set.
* @param {*} [propertyValue] The value to be set.
* @returns {null|*} Returns `null` when setting the value, or the value when getting it.
* @example
* var myController = new Ramify();
* myController.add({
* someFunction: () {
* this.prop('someProp', 'someValue');
* // do something
* },
* someOtherFunction: () {
* var v = this.prop('someProp');
* // do something else
* }
* });
*/
Rami.prototype.prop = function() {
if (!arguments[0] || typeof arguments[0] !== 'string') {
if (typeof _generalErrorHandler === 'function') {
return _generalErrorHandler.call(this, new RamifyError('Prop name not specified.'));
} else {
throw new RamifyError('Prop name not specified.');
}
} else {
if (arguments.length > 1) {
_properties[arguments[0]] = arguments[1];
} else {
return _properties[arguments[0]];
}
}
};
/**
* Creates a new instance of the Ramify object.
* @class Ramify
* @param {Ramify#config} [config] An Object containing the configurations.
* @example
* var Ramify = require('ramify');
* var myController = new Ramify();
*/
function Ramify(config) {
if (config !== null && typeof config === 'object') {
var keysCount = Object.keys(this.config).length;
for (var i = 0; i < keysCount; i++) {
var configName = Object.keys(this.config)[i];
if (typeof this.config[configName] === typeof config[configName]) {
this.config[configName] = config[configName];
}
}
}
}
/**
* Returns the current version of Ramify package.
* @member {string} version
* @memberof Ramify
* @static
*/
Ramify.version = '0.4.0';
/**
* Returns the author of Ramify package.
* @member {string} author
* @memberof Ramify
* @static
*/
Ramify.author = 'Zisis Zikos';
Ramify.RamifyError = RamifyError;
/**
* A reference to internal `this` object.
* @member {Ramify~Rami} context
* @memberof Ramify
* @instance
*/
Ramify.prototype.context = _this;
/**
* The Ramify instance configuration.
* @member {Object} config
* @memberof Ramify
* @instance
* @property {boolean} createAlias=false Whether alias methods should be
* created for every ramus, at the inner Rami object. **Note:** Alias methods
* are dangerous because calling a method that doesn't exists will result
* in an unhandled exception. Instead, the `.call` method handles this kind
* of errors.
* @property {boolean} safeAlias=true Whether alias methods should be
* prepended with an underscore for safety (not overwriting existing methods).
* @example
* var myController = new Ramify({
* createAlias: true,
* safeAlias: false
* });
* myController.add(function aRamusName() {
* // do something
* });
* myController.aRamusName();
*/
Ramify.prototype.config = _config;
/**
* Adds functions to the Ramify instance that will be used within it.
* @function Ramify#addAll
* @param {!Object} rami The Object that contains the functions as Object properties.
* @returns {Ramify} The instance of current Ramify object.
* @example
* var myController = new Ramify();
* myController.addAll({
* someRamus: function () {
* // do something
* },
* someOtherRamus: function () {
* // do something else
* },
* anotherRamus: 'foo'
* });
*/
Ramify.prototype.addAll = function(rami) {
if (rami !== null && typeof rami === 'object') {
_rami = _rami || {};
var keysCount = Object.keys(rami).length;
for (var i = 0; i < keysCount; i++) {
var ramusName = Object.keys(rami)[i];
_rami[ramusName] = rami[ramusName];
if (this.config.createAlias) {
var properyName = this.config.safeAlias ? '_' + ramusName : ramusName;
Rami.prototype[properyName] = _call.bind(_this, ramusName);
Ramify.prototype[properyName] = _call.bind(_this, ramusName);
}
}
}
return this;
};
/**
* Adds a function to the Ramify instance that will be used within it.
* @function Ramify#add
* @param {!string|function} ramusName The name of the ramus or the content function.
* @param {*} [ramusContent] The content of the current ramus.
* @returns {Ramify} The instance of current Ramify object.
* @example
* var myController = new Ramify();
* myController.add('aRamusName', function () {
* // do something
* });
* myController.add(function aRamusName() {
* // do something
* });
* myController.add('anotherRamusName', 'someValue');
* myController.add('someOtherRamusName');
*/
Ramify.prototype.add = function(ramusName, ramusContent) {
var name = null;
if (typeof ramusName === 'function') {
_rami = _rami || {};
name = ramusName.name;
_rami[name] = ramusName;
} else {
if (!ramusName) {
if (typeof _generalErrorHandler === 'function') {
return _generalErrorHandler.call(this, new RamifyError('Ramus name is not specified.'));
} else {
throw new RamifyError('Ramus name is not specified.');
}
return this;
}
_rami = _rami || {};
name = ramusName;
_rami[name] = ramusContent;
}
if (this.config.createAlias) {
var properyName = this.config.safeAlias ? '_' + name : name;
Rami.prototype[properyName] = _call.bind(_this, name);
Ramify.prototype[properyName] = _call.bind(_this, name);
}
return this;
};
/**
* Sets the content of a ramus.
* @function Ramify#setContent
* @param {!string} ramusName The name of the ramus.
* @param {*} [ramusContent] The content of the current ramus.
* @returns {Ramify} The instance of current Ramify object.
* @example
* var myController = new Ramify();
* myController.setContent('aRamusName', function () {
* // do something
* });
* myController.setContent('anotherRamusName', 'someValue');
*/
Ramify.prototype.setContent = function(ramusName, ramusContent) {
if (!ramusName) {
if (typeof _generalErrorHandler === 'function') {
return _generalErrorHandler.call(this, new RamifyError('Ramus name is not specified.'));
} else {
throw new RamifyError('Ramus name is not specified.');
}
return this;
}
_rami = _rami || {};
if (!_rami[ramusName]) {
if (typeof _generalErrorHandler === 'function') {
return _generalErrorHandler.call(this, new RamifyError('Given ramus name is not defined.'));
} else {
throw new RamifyError('Given ramus name is not defined.');
}
return this;
}
_rami[ramusName] = ramusContent;
return this;
};
/**
* Gets the content of a ramus.
* @function Ramify#getContent
* @param {!string} ramusName The name of the ramus.
* @returns {*} The content of the current ramus.
* @example
* var myController = new Ramify();
* var content = myController.getContent('aRamusName');
*/
Ramify.prototype.getContent = function(ramusName) {
if (!ramusName) {
if (typeof _generalErrorHandler === 'function') {
return _generalErrorHandler.call(this, new RamifyError('Ramus name is not specified.'));
} else {
throw new RamifyError('Ramus name is not specified.');
}
return this;
}
_rami = _rami || {};
var ramusContent = _rami[ramusName];
if (!ramusContent) {
if (typeof _generalErrorHandler === 'function') {
return _generalErrorHandler.call(this, new RamifyError('Given ramus name is not defined.'));
} else {
throw new RamifyError('Given ramus name is not defined.');
}
return this;
}
return ramusContent;
};
/**
* Adds functions to the Ramify instance that will be used within it.
* @function Ramify#call
* @this Ramify~Rami
* @param {!string} ramusName The name of the ramus to call.
* @param {*} [passedParameter] The value the will be applied as a parameter to the called function. Could be any value, even a Promise. If a Promise is passed, the Promise will be automatically evaluated and the resolved value will be passed as a parameter, otherwise the rejected value will be passed to the errorHandler function. If no errorHandler function is declared, the generalErrorHandler function will be called.
* @param {function} [errorHandler] A function that will handle the errors that can happen at a Promise that is passed as a parameter.
* @returns {*} The resolved value.
* @example
* var myController = new Ramify();
* function somePromise () {
* return new Promise(function (resolve, reject) {
* resolve('someValue');
* });
* }
* myController.add({
* someFunction: () {
* return this.call('someOtherFunction', somePromise(), function (err) {
* console.log(err);
* });
* },
* someOtherFunction: (value) {
* return this.call('endFunction', value + '123');
* },
* endFunction: function (value) {
* return value;
* }
* });
* myController.call('someFunction');
*/
Ramify.prototype.call = function() {
return _call.apply(_this, arguments);
};
/**
* When used, it will handle all the errors that were not caught at the specific error handlers.
* @function Ramify#catch
* @param {function} generalErrorHandler The general error handle function.
* @returns {Ramify} The current Ramify instance.
* @example
* var myController = new Ramify();
* myController.add(Rami).catch(Promise.reject);
* myController.call('someFunction', 'someValue')
* .then(function () {
* // do something
* }).catch(Ramify.RamifyError, function (err) {
* console.log('+++++ Ramify Error Handler +++++');
* console.log(err.stack);
* }).catch(function (err) {
* console.log('+++++ General Error Handler +++++');
* console.log(err.stack);
* });
*/
Ramify.prototype.catch = function(generalErrorHandler) {
_generalErrorHandler = generalErrorHandler;
return this;
};
function _call() {
var errorHandler;
if (!_rami) {
if (typeof _generalErrorHandler === 'function') {
return _generalErrorHandler.call(this, new RamifyError('Rami are not specified.'));
} else {
throw new RamifyError('Rami are not specified.');
}
return this;
}
if (arguments.length === 0) {
if (typeof _generalErrorHandler === 'function') {
return _generalErrorHandler.call(this, new RamifyError('Ramus name is not specified.'));
} else {
throw new RamifyError('Ramus name is not specified.');
}
return this;
}
if (!_rami[arguments[0]]) {
errorHandler = arguments[2] || _generalErrorHandler;
if (typeof errorHandler === 'function') {
return errorHandler.call(this, new RamifyError('Given ramus name is not defined.'));
} else {
throw new RamifyError('Given ramus name is not defined.');
}
return this;
}
var ramusName = arguments[0];
if (typeof _rami[ramusName] === 'function') {
if (arguments.length > 1 && !!arguments[1] && typeof arguments[1].then === 'function') {
errorHandler = arguments[2] || _generalErrorHandler;
return arguments[1].then(_rami[ramusName].bind(this), errorHandler && errorHandler.bind(this));
} else {
var arg = null;
if (arguments.length > 1) {
arg = arguments[1];
}
errorHandler = arguments[2] || _generalErrorHandler;
if (typeof errorHandler === 'function') {
try {
return _rami[ramusName].call(this, arg);
} catch (e) {
return errorHandler.call(this, e);
}
} else {
return _rami[ramusName].call(this, arg);
}
}
} else {
return _rami[ramusName];
}
}
return Ramify;
});