Skip to content
This repository has been archived by the owner on Feb 21, 2018. It is now read-only.

Commit

Permalink
Merge pull request #6 from buildo/add-autobound
Browse files Browse the repository at this point in the history
add autobound
  • Loading branch information
giogonzo committed Jun 2, 2015
2 parents 5e25302 + 9e7548b commit 9c0ed50
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
33 changes: 33 additions & 0 deletions lib/decorators/autobound.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

exports.__esModule = true;
exports['default'] = autobound;

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

var _tcomb = require('tcomb');

var _tcomb2 = _interopRequireDefault(_tcomb);

function autobound(target, key, descriptor) {
var value = descriptor.value;

if (process.env.NODE_ENV !== 'production') {
_tcomb2['default'].assert(_tcomb2['default'].Func.is(value), '@autobound decorator can only be applied to methods');
}

return {
configurable: true,
get: function get() {
var boundValue = value.bind(this);
Object.defineProperty(this, key, {
value: boundValue,
configurable: true,
writable: true
});
return boundValue;
}
};
}

module.exports = exports['default'];
8 changes: 7 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,10 @@ var _decoratorsSkinnable = require('./decorators/skinnable');

var _decoratorsSkinnable2 = _interopRequireDefault(_decoratorsSkinnable);

exports.skinnable = _decoratorsSkinnable2['default'];
exports.skinnable = _decoratorsSkinnable2['default'];

var _decoratorsAutobound = require('./decorators/autobound');

var _decoratorsAutobound2 = _interopRequireDefault(_decoratorsAutobound);

exports.autobound = _decoratorsAutobound2['default'];
23 changes: 23 additions & 0 deletions src/decorators/autobound.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import t from 'tcomb';

export default function autobound(target, key, descriptor) {
const { value } = descriptor;

if (process.env.NODE_ENV !== 'production') {
t.assert(t.Func.is(value), `@autobound decorator can only be applied to methods`);
}

return {
configurable: true,
get() {
const boundValue = value.bind(this);
Object.defineProperty(this, key, {
value: boundValue,
configurable: true,
writable: true
});
return boundValue;
}
};
}

1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export props from './decorators/props';
export pure from './decorators/pure';
export queries from './decorators/queries';
export skinnable from './decorators/skinnable';
export autobound from './decorators/autobound';

0 comments on commit 9c0ed50

Please sign in to comment.