-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from ragboyjr/serviceFn
Adding serviceFn
- Loading branch information
Showing
6 changed files
with
87 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* Register a function service | ||
*/ | ||
var serviceFactory = function serviceFactory(name, factoryService) { | ||
return createService.apply(this, [name, factoryService, false].concat(slice.call(arguments, 2))); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,31 @@ | ||
/** | ||
* Register a service inside a generic factory. | ||
* Register a class service | ||
* | ||
* @param String name | ||
* @param Function Service | ||
* @return Bottle | ||
*/ | ||
var service = function service(name, Service) { | ||
var deps = arguments.length > 2 ? slice.call(arguments, 2) : null; | ||
return createService.apply(this, [name, Service, true].concat(slice.call(arguments, 2))); | ||
}; | ||
|
||
/** | ||
* Private helper for creating service and service factories. | ||
* | ||
* @param String name | ||
* @param Function Service | ||
* @return Bottle | ||
*/ | ||
var createService = function createService(name, Service, isClass) { | ||
var deps = arguments.length > 3 ? slice.call(arguments, 3) : []; | ||
var bottle = this; | ||
return factory.call(this, name, function GenericFactory() { | ||
var ServiceCopy = Service; | ||
if (deps) { | ||
var args = deps.map(getNestedService, bottle.container); | ||
args.unshift(Service); | ||
ServiceCopy = Service.bind.apply(Service, args); | ||
var serviceFactory = Service; // alias for jshint | ||
var args = deps.map(getNestedService, bottle.container); | ||
|
||
if (!isClass) { | ||
return serviceFactory.apply(null, args); | ||
} | ||
return new ServiceCopy(); | ||
return new (Service.bind.apply(Service, [null].concat(args)))(); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* globals Bottle */ | ||
;(function() { | ||
'use strict'; | ||
|
||
/** | ||
* Bottle Factory test suite | ||
*/ | ||
describe('Bottle#serviceFactory', function() { | ||
it('injects dependencies to a service factory', function() { | ||
var b = new Bottle(); | ||
var createThing = function(foo, bar) { | ||
return {foo: foo, bar: bar}; | ||
}; | ||
b.serviceFactory('Thing', createThing, 'foo', 'bar'); | ||
b.service('foo', function() { this.name = 'foo'; }); | ||
b.value('bar', 'bippity'); | ||
|
||
expect(b.container.Thing).toBeDefined(); | ||
expect(b.container.Thing.foo).toBeDefined(); | ||
expect(b.container.Thing.foo.name).toBe('foo'); | ||
expect(b.container.Thing.bar).toBe('bippity'); | ||
}); | ||
}); | ||
}()); |