Skip to content

Commit

Permalink
dic loader replaced dic class loader
Browse files Browse the repository at this point in the history
  • Loading branch information
matuszeman committed Jun 15, 2017
1 parent ca15a08 commit bc4d143
Show file tree
Hide file tree
Showing 15 changed files with 229 additions and 210 deletions.
119 changes: 68 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ dic.getAsync('myApp').then(app => {
## Classes

<dl>
<dt><a href="#DicClassLoader">DicClassLoader</a></dt>
<dd><p>Class loader</p>
</dd>
<dt><a href="#DicConfigLoader">DicConfigLoader</a></dt>
<dd><p>Config loader - sets up Dic from the config (plain object)</p>
</dd>
<dt><a href="#DicLoader">DicLoader</a></dt>
<dd><p>Dic loader</p>
</dd>
<dt><a href="#Dic">Dic</a></dt>
<dd><p>Dependency injection container</p>
<p>For more usage examples see: <a href="#Dic+instance">instance</a>, <a href="#Dic+class">class</a>, <a href="#Dic+factory">factory</a>,
Expand All @@ -116,54 +116,6 @@ dic.getAsync('myApp').then(app => {
<dd></dd>
</dl>

<a name="DicClassLoader"></a>

## DicClassLoader
Class loader

**Kind**: global class

* [DicClassLoader](#DicClassLoader)
* [new DicClassLoader(dic, opts)](#new_DicClassLoader_new)
* [.loadPath(path)](#DicClassLoader+loadPath)

<a name="new_DicClassLoader_new"></a>

### new DicClassLoader(dic, opts)

| Param | Type | Description |
| --- | --- | --- |
| dic | <code>[Dic](#Dic)</code> | |
| opts | <code>Object</code> | |
| opts.rootDir | <code>string</code> | Absolute path to root folder of source files. Default: `process.cwd()` |

<a name="DicClassLoader+loadPath"></a>

### dicClassLoader.loadPath(path)
Load all files and register exported classes to [Dic](#Dic).

All files are expected to export a class.

File name dictates what name the service will be registered as.
E.g. `my-service.js` service would become registered as `myService` => file name is camelCased.

**Kind**: instance method of <code>[DicClassLoader](#DicClassLoader)</code>

| Param | Type | Description |
| --- | --- | --- |
| path | <code>string</code> | glob expression [https://www.npmjs.com/package/globby](https://www.npmjs.com/package/globby) |

**Example**
```js
// Registers all classes under `CWD/src` folder.

const {Dic, DicClassLoader} = require('bb-dic');
const dic = new Dic();
const loader = new DicClassLoader(dic);
loader.loadPath('src/*.js');

module.exports = dic;
```
<a name="DicConfigLoader"></a>

## DicConfigLoader
Expand Down Expand Up @@ -218,6 +170,71 @@ Set up Dic according the config
}
}
```
<a name="DicLoader"></a>

## DicLoader
Dic loader

**Kind**: global class

* [DicLoader](#DicLoader)
* [new DicLoader(opts)](#new_DicLoader_new)
* [.loadPath(dic, path)](#DicLoader+loadPath)

<a name="new_DicLoader_new"></a>

### new DicLoader(opts)

| Param | Type | Description |
| --- | --- | --- |
| opts | <code>Object</code> | |
| opts.rootDir | <code>string</code> | Absolute path to root folder of source files. Default: `process.cwd()` |

**Example**
```js
const {Dic, DicLoader} = require('bb-dic');
const dic = new Dic()

const loader = new DicLoader({
rootDir: __dirname //if not specified process.cwd() is used
});

//loads all .js files under src folder
loader.loadPath('src/*.js');
```
<a name="DicLoader+loadPath"></a>

### dicLoader.loadPath(dic, path)
Load all instances/factories/classes to [Dic](#Dic).

File types and what they should export
- name.js -> class
- name.factory.js -> factory
- name.async-factory.js -> async factory
- name.instance.js -> instance


File name dictates what name the service will be registered as.
E.g. `my-service.js` service would become registered as `myService` => file name is camelCased.

**Kind**: instance method of <code>[DicLoader](#DicLoader)</code>

| Param | Type | Description |
| --- | --- | --- |
| dic | <code>[Dic](#Dic)</code> | |
| path | <code>string</code> | glob expression [https://www.npmjs.com/package/globby](https://www.npmjs.com/package/globby) |

**Example**
```js
// Registers all services under `CWD/src` folder.

const {Dic, DicLoader} = require('bb-dic');
const dic = new Dic();
const loader = new DicLoader();
loader.loadPath(dic, 'src/*.js');

module.exports = dic;
```
<a name="Dic"></a>

## Dic
Expand Down
100 changes: 0 additions & 100 deletions es5/dic-class-loader.js

This file was deleted.

2 changes: 1 addition & 1 deletion es5/dic.js
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ var Dic = function () {
paramsAlias: Joi.object().default({}),
asyncFactory: Joi.func(),
inject: Joi.object().default({}),
container: Joi.object().type(Dic).optional().default(this)
container: Joi.object().default(this) //type(Dic) - this does not work when having Dic from different packages obviously
}));

if (!def.type) {
Expand Down
2 changes: 1 addition & 1 deletion es5/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module.exports = {
Dic: require('./dic'),
DicClassLoader: require('./dic-class-loader'),
DicLoader: require('./dic-loader'),
DicConfigLoader: require('./dic-config-loader'),
DicFactory: require('./dic-factory'),
Parser: require('./parser')
Expand Down
3 changes: 2 additions & 1 deletion es5/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ var Parser = function () {
});
return this.parseNode(node);
} catch (e) {
console.log('XXX'); //XXX
console.log('Failed to parse the class'); //XXX
console.log(target); //XXX
console.log(e); //XXX
throw e;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bb-dic",
"version": "0.9.1",
"version": "1.0.0",
"description": "A dependency injection container",
"main": "src/index.js",
"scripts": {
Expand Down
53 changes: 0 additions & 53 deletions src/dic-class-loader.js

This file was deleted.

Loading

0 comments on commit bc4d143

Please sign in to comment.