all files / util/ Factory.js

0% Statements 0/7
0% Branches 0/2
0% Functions 0/1
0% Lines 0/7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30                                                           
import Registry from './Registry'
 
/*
  Simple factory implementation.
 
  @class Factory
  @extends Registry
*/
class Factory extends Registry {
  /**
    Create an instance of the clazz with a given name.
 
    @param {String} name
    @return A new instance.
  */
  create(name) {
    var clazz = this.get(name)
    if (!clazz) {
      throw new Error( 'No class registered by that name: ' + name )
    }
    // call the clazz providing the remaining arguments
    var args = Array.prototype.slice.call( arguments, 1 )
    var obj = Object.create( clazz.prototype )
    clazz.apply( obj, args )
    return obj
  }
}
 
export default Factory