Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion lib/mixins.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var debug = require('debug')('loopback:mixin');
var assert = require('assert');
var inflection = require('inflection');
var DefaultModelBaseClass = require('./model.js');

function isModelClass(cls) {
Expand All @@ -9,21 +10,37 @@ function isModelClass(cls) {
return cls.prototype instanceof DefaultModelBaseClass;
}

function normalizeName(str) {
str = inflection.underscore(str); // pre-normalize
str = String(str).replace(/[\W_]/g, ' ').toLowerCase();
str = str.replace(/(?:^|\s|-)\S/g, function(c){ return c.toUpperCase(); });
return str.replace(/\s/g, '');
}

module.exports = MixinProvider;

function MixinProvider(modelBuilder) {
this.modelBuilder = modelBuilder;
this.mixins = {};
}

/**
* Get named mixin
* @param {String} name
* @returns {Function} mixin
*/
MixinProvider.prototype.getMixin = function getMixin(name) {
return this.mixins[normalizeName(name)];
};

/**
* Apply named mixin to the model class
* @param {Model} modelClass
* @param {String} name
* @param {Object} options
*/
MixinProvider.prototype.applyMixin = function applyMixin(modelClass, name, options) {
var fn = this.mixins[name];
var fn = this.getMixin(name);
if (typeof fn === 'function') {
if (modelClass.dataSource) {
fn(modelClass, options || {});
Expand Down Expand Up @@ -51,6 +68,7 @@ MixinProvider.prototype.applyMixin = function applyMixin(modelClass, name, optio
*/
MixinProvider.prototype.define = function defineMixin(name, mixin) {
assert(typeof mixin === 'function', 'The mixin must be a function or model class');
name = normalizeName(name);
if (this.mixins[name]) {
debug('Duplicate mixin: %s', name);
} else {
Expand Down
20 changes: 19 additions & 1 deletion test/mixins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function timestamps(Model, options) {
};
}

mixins.define('TimeStamp', timestamps);
mixins.define('time-stamp', timestamps);

describe('Model class', function () {

Expand All @@ -55,6 +55,11 @@ describe('Model class', function () {
Model.multiMixin = Model.multiMixin || {};
Model.multiMixin[options.key] = options.value;
});
mixins.define('check-access', function(Model, options) {
Model.accessMixin = Model.accessMixin || {};
Model.accessMixin[options.key] = options.value;
});
mixins.mixins['CheckAccess'].should.be.a.function;
});

it('should apply a mixin class', function() {
Expand Down Expand Up @@ -105,4 +110,17 @@ describe('Model class', function () {
});
});

it('should apply mixins using normalized names', function() {
var memory = new DataSource('mem', {connector: Memory}, modelBuilder);
var Item = memory.createModel('Item', { name: 'string' });

Item.mixin('CheckAccess', { key: 'classified', value: true });
Item.mixin('check_access', { key: 'underscored', value: true });
Item.mixin('check-access', { key: 'dasherized', value: true });

Item.accessMixin.classified.should.be.true;
Item.accessMixin.underscored.should.be.true;
Item.accessMixin.dasherized.should.be.true;
});

});