UNPKG

babelute

Version:

Internal Domain Specific (Multi)Modeling javascript framework

141 lines (111 loc) 5.57 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.FacadePragmatics = undefined; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); exports.createFacadePragmatics = createFacadePragmatics; var _babelute = require('../babelute'); var _babelute2 = _interopRequireDefault(_babelute); var _pragmaticsCore = require('./pragmatics-core.js'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } // removed in production /** * @author Gilles Coomans * @licence MIT * @copyright 2016-2017 Gilles Coomans */ /** * FacadePragmatics : a facade oriented Pragmatics subclass. You should never instanciate a FacadePragmatics directly with new. use {@link createFacadePragmatics}. * @example * // Remarque : any lexem's method will be of the following format : * function(subject, args, ?percolator){ * // return nothing * } */ var FacadePragmatics = exports.FacadePragmatics = function (_Pragmatics) { _inherits(FacadePragmatics, _Pragmatics); /** * @param {Object} targets initial targets object * @param {?Object} pragmas pragmatics methods to add */ function FacadePragmatics(targets, pragmas) { _classCallCheck(this, FacadePragmatics); return _possibleConstructorReturn(this, (FacadePragmatics.__proto__ || Object.getPrototypeOf(FacadePragmatics)).call(this, targets, pragmas)); } /** * "each" facade implementation * @param {Object} subject the handled subject * @param {Array|arguments} args the lexem's args : [ collection:Array, itemHandler:Function ] * @param {?Object} percolator the sentence's percolator instance * @return {void} nothing */ _createClass(FacadePragmatics, [{ key: 'each', value: function each(subject, args /* collection, itemHandler */, percolator) { var collec = args[0], itemHandler = args[1]; if (collec && collec.length) // no supputation on collection kind : use "for" for (var i = 0, len = collec.length, item, templ; i < len; ++i) { item = collec[i]; templ = itemHandler(item, i); if (!templ) throw new Error('.each function should return a sentence.'); this.$output(subject, templ, percolator); } } /** * "if" facade implementation * @param {Object} subject the handled subject * @param {Array|arguments} args the lexem's args : [ conditionIsTrue:Babelute, conditionIsFalse:Babelute ] * @param {?Object} percolator the sentence's percolator instance * @return {void} nothing */ }, { key: 'if', value: function _if(subject, args /* trueBabelute, falseBabelute */, percolator) { if (args[0]) this.$output(subject, args[1], percolator);else if (args[2]) this.$output(subject, args[2], percolator); } /** * * @override * @param {Object} subject the subject handle through interpretation * @param {Babelute} babelute the babelute "to interpret on" subject * @param {Scope} percolator the sentence percolator instance (optional) * @return {Object} the subject */ }, { key: '$output', value: function $output(subject, babelute) { var percolator = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; for (var i = 0, lexem, len = babelute._lexems.length; i < len; ++i) { lexem = babelute._lexems[i]; if (this._targets[lexem.lexicon] && this[lexem.name]) this[lexem.name](subject, lexem.args, percolator); } return subject; } }]); return FacadePragmatics; }(_pragmaticsCore.Pragmatics); /** * create a FacadePragmatics instance * @param {Object} targets the pragmatics targets DSL * @param {?Object} pragmas the methods to add * @return {FacadePragmatics} the facade pragmatics instance * @example * const myPragmas = babelute.createFacadePragmatics({ * 'my-lexicon':true * }, { * foo(subject, args, percolator){ * // do something * }, * bar(subject, args, percolator){ * // do something * } * }); */ function createFacadePragmatics(targets) { var pragmas = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; return new FacadePragmatics(targets, pragmas); }