1.0.3 • Published 6 years ago

class-autoloader v1.0.3

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

class-autoloader

class-autoloader is a class autoloader, just like Rails class autoloader

Install

Install with npm

$ npm install class-autoloader

Hello world

npm install class-autoloader
touch index.js
mkdir app
mkdir app/controllers
touch app/controllers/articles_controller.js

articles_controller.js

module.exports = class ArticlesController {
  static hello() {
    return "Hi!";
  }
}

index.js:

const ClassAutoloader = require('class-autoloader');

const Config = {
  autoload_paths: ['app/controllers'],
  cache_classes: false
}

const Loader = new ClassAutoloader(Config);

Run it

$ node -r index.js
> ArticlesController.hello();
Hi!

Now modify the file articles_controller.js

module.exports = class ArticlesController {
  static hello() {
    return 'Hello Fred!';
  }
}

And call again

$ node -r index.js
> ArticlesController.hello();
Hi!
> ArticlesController.hello();
Hello Fred!

Cache classes

You can set cache_classes with true to cache classes. It will cause no class will be autoloaded

Name scope

You can also specify a name scope e.g.

$ mkdir app/controllers/my_scope
$ touch app/controllers/my_scope/one_class.js
MyScope.OneClass // will cause autoload one_class.js

Eager load

Set cache_classes and eager_loader with true, classes will be autoloaded before call it

License

The MIT License, 2018 Fred Shaw (@FredShawF)

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago