Skip to content

Commit

Permalink
Merge pull request #446 from SBoudrias/inheritance
Browse files Browse the repository at this point in the history
Implement new Generator.extend inheritance method
  • Loading branch information
sindresorhus committed Dec 27, 2013
2 parents c1210d0 + cc21acf commit c549c67
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
9 changes: 8 additions & 1 deletion lib/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,13 @@ Base.prototype.getCollisionFilter = function () {
return config.callback();
});
}

};

/**
* Extend this Class to create a new one inherithing this one.
* Also add a helper __super__ object poiting to the parent prototypes methods
* @param {Object} protoProps Prototype properties (available on the instances)
* @param {Object} staticProps Static properties (available on the contructor)
* @return {Object} New sub class
*/
Base.extend = require('class-extend').extend;
14 changes: 6 additions & 8 deletions lib/named-base.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var util = require('util');
var Base = require('./base');

/**
Expand All @@ -11,10 +10,9 @@ var Base = require('./base');
* @param {Object} options [description]
*/

function NamedBase(args, options) {
Base.apply(this, arguments);
this.argument('name', { type: String, required: true });
}

util.inherits(NamedBase, Base);
module.exports = NamedBase;
module.exports = Base.extend({
constructor: function () {
Base.apply(this, arguments);
this.argument('name', { type: String, required: true });
}
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"text-table": "~0.2.0",
"download": "~0.1.6",
"request": "~2.29.0",
"file-utils": "~0.1.1"
"file-utils": "~0.1.1",
"class-extend": "~0.1.0"
},
"devDependencies": {
"mocha": "~1.15.1",
Expand Down
25 changes: 25 additions & 0 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ var generators = require('..');
var helpers = require('../lib/test/helpers');
var _ = require('lodash');

var Base = generators.generators.Base;


describe('yeoman.generators.Base', function () {
// TODO(mklabs): generate generator about to be tested, or add it in fixtures.
Expand Down Expand Up @@ -52,6 +54,29 @@ describe('yeoman.generators.Base', function () {
});
});

describe('.extend', function () {
it('create a new object inheriting the Generator', function () {
assert.equal(Base.extend().prototype.constructor, Base);
});

it('pass the extend method along', function () {
var Sub = Base.extend();
assert.ok(Sub.extend);
});

it('assign prototype methods', function () {
var proto = { foo: function () {} };
var Sub = Base.extend(proto);
assert.equal(Sub.prototype.foo, proto.foo)
});

it('assign static methods', function () {
var staticProps = { foo: function () {} };
var Sub = Base.extend({}, staticProps);
assert.equal(Sub.foo, staticProps.foo)
});
});

describe('#run', function () {
beforeEach(function () {
this.TestGenerator = generators.test.createDummyGenerator();
Expand Down

0 comments on commit c549c67

Please sign in to comment.