Skip to content

Commit

Permalink
(3.1.3) refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kimung committed Jul 11, 2018
1 parent 747e86f commit 706fdf3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 33 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## 3.1.3 - 2018-07-11
### Changed
- refactoring of factory.js

## 3.1.2 - 2018-05-04
### Changed
- `@rduk` modules as peerDependencies
Expand Down
56 changes: 35 additions & 21 deletions lib/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,40 +28,38 @@ const errors = require('@rduk/errors')
const configuration = require('@rduk/configuration')
const Section = require('./section')

module.exports = function (name, base, defaultConfig) {
if (!name || typeof name !== 'string' || name.length === 0) {
errors.throwArgumentError('name', name)
}

if (!base || !(base.prototype instanceof require('./base'))) {
errors.throwArgumentError('base', base)
class ProviderCreator {
constructor (name, Base, defaultConfig) {
this.name = name
this.Base = Base
this.defaultConfig = defaultConfig
}

let section, instance

let getSection = function () {
if (!section) {
section = configuration.load().getSection(name, Section, !!defaultConfig)
getSection () {
if (!this._section) {
this._section = configuration.load().getSection(this.name, Section, !!this.defaultConfig)
}

if (!section && !!defaultConfig) {
section = new Section(defaultConfig)
if (!this._section && !!this.defaultConfig) {
this._section = new Section(this.defaultConfig)
}

return section
return this._section
}

let create = function (Provider, options) {
create (Provider, options, section) {
let obj = new Provider(...[options, section])

if (!(obj instanceof base)) {
if (!(obj instanceof this.Base)) {
errors.throwConfigurationError('invalid provider type')
}

obj.initialize()

return obj
}
}

const factory = creator => {
let instance

return {
getInstance: function () {
Expand All @@ -72,10 +70,26 @@ module.exports = function (name, base, defaultConfig) {
return instance
},
get: function (name) {
let section = getSection()
let section = creator.getSection()
let options = section.get(name)

return create(options.type, options, section)
return creator.create(options.type, options, section)
}
}
}

module.exports = (name, base, defaultConfig) => {
if (!name) {
errors.throwArgumentError('name', name)
}

if (typeof name !== 'string') {
errors.throwArgumentError('name', name)
}

if (!base || !(base.prototype instanceof require('./base'))) {
errors.throwArgumentError('base', base)
}

return factory(new ProviderCreator(name, base, defaultConfig))
}
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rduk/provider",
"version": "3.1.2",
"version": "3.1.3",
"description": "Easily add providers to your Node.js app",
"engines": {
"node": ">=6.4.0"
Expand All @@ -9,8 +9,9 @@
"scripts": {
"pretest": "standard --fix && cp ./spec/resources/config*.yml .",
"test": "istanbul cover node_modules/jasmine/bin/jasmine.js --report cobertura",
"posttest": "rm -rf ./config*yml && istanbul report",
"coveralls": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
"posttest": "rm -rf ./config*yml",
"report": "istanbul report",
"coveralls": "npm run report && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 706fdf3

Please sign in to comment.