bike

Class system to organize namespaces, define, create, extend, mixin, super inheritance and more

npm install bike
1 downloads in the last day
3 downloads in the last week
15 downloads in the last month

Stage
Build Status

version 0.5.5 (stable) - History

Bike

Organize your classes in namespaces, extend or mix them. Bike helps you keep your project clear and in order.

This module can be used with node or in the browser.

Features

  • namespaces
  • extend (inheritance like)
  • mixin
  • super methods
  • self require dependencies
  • cache

Installation

Install this using npm as follows

$ npm install bike

Quick start

Define a class

This will register a class.

var Bike = require('bike');

Bike.define('person', {
  talk: function(){
    return 'Ciao, i am a person'
  }
});

Create a class

This will instantiate a new object.

var person = Bike.create('person');

person.talk();
// => Ciao, i am a person

Extend a class

Extending a class will allow to inherit all properties and methods from the extended object.

To call the parent method use the this._super() syntax.

Bike.define('singer', {
  extend: 'person',
  talk: function(){
    return this._super() + ' and a good singer!'
  },
  sing: function(){
    return 'I can sing'
  }
});

var singer = Bike.create('singer');

singer.talk();
// => Ciao, i am a person and a good singer!

Optionally you can also give properies or methods that will extend the new object just created, not its definition.

var frank = Bike.create('singer', {
  name: 'Frank',
  sing: function(){
    return this._super() + ' songs.'
  }
});

frank.name;
// => Frank

frank.sing();
// => I can sing songs

Define namespaces

The namespace will help you organize classes in a tree structure.

Bike.namespace('people', __dirname + '/jobs');

Now when you create a class people.* it will automatically require it from the folder /jobs.

Bike.define('people.architect', {
  /* ... */
});
// => will look for '/jobs/architect.js'

// OR

Bike.define('people.architect', {
  extend: 'people.general.worker'
});
// => will look for '/jobs/architect.js' extending '/jobs/general/worker.js'
// No need to require('/jobs/architect.js') or not require('/jobs/general/worker.js')

Mixing classes

You could also borrow properties or methods from other classes beside direct extending

Bike.define('people.architect', {
  mixins: [
    'people.general.engineer',
    'freetime.runner'
  ]
});

Extend your own class with Bike

It is possible to extend your own class/module/project with Bike's functionality.

var myClass = function(){/**/};
myClass.version = '0.0.0';
myClass.ciao = function(){};

/* ... */

Bike.extend(myClass);

// Now you can do
myClass.define('...', {});
// and
myClass.create('...');

// Optionally you can also pass namespaces directly into the extend fn
Bike.extend(myClass, {
  'people', __dirname + '/jobs'
})

Running Tests

Install dev dependencies and make tests:

$ npm install -d
$ make test

Contributors

Gabriele Di Stefano <gabriele.ds@gmail.com>

endorse

Behere Logo

License

The MIT License

npm loves you