Skip to content

Commit

Permalink
add support for finer grained configKey definition
Browse files Browse the repository at this point in the history
see issue #10
  • Loading branch information
alrik committed Oct 14, 2015
1 parent 15d1384 commit 0ae40e2
Show file tree
Hide file tree
Showing 2 changed files with 237 additions and 14 deletions.
46 changes: 32 additions & 14 deletions Application.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,47 +125,65 @@ class Application extends Service {
var configService = this.configService,
serviceManager = this.getServiceManager(),
servicesConfig = configService.get('app.services', {}),
servicesToLoad = [],
serviceKey, serviceClass, service, i, l;
servicesToLoad = [];

for (serviceKey in servicesConfig) {
serviceClass = servicesConfig[serviceKey];
for (let serviceKey in servicesConfig) {
let
serviceDefinition = servicesConfig[serviceKey];

if (serviceClass) {
if (typeof serviceDefinition === 'function') {
serviceDefinition = {
service: serviceDefinition
};
}

if (serviceDefinition !== null && typeof serviceDefinition.service === 'function') {
// instantiate the fetched service class.
service = new serviceClass(serviceManager);
let service = new (serviceDefinition.service)(serviceManager);
serviceManager.set(serviceKey, service);

// determine the configkey
if (typeof serviceDefinition.configKey !== 'string' && serviceDefinition.configKey !== null) {
if (typeof service.configKey === 'string' || service.configKey === null) {
serviceDefinition.configKey = service.configKey
} else if (typeof serviceDefinition.service.CONFIG_KEY === 'string' || serviceDefinition.service.CONFIG_KEY === null) {
serviceDefinition.configKey = serviceDefinition.service.CONFIG_KEY;
} else {
serviceDefinition.configKey = serviceKey;
}
}

servicesToLoad.push({
key: serviceKey,
instance: service,
definition: serviceClass
definition: serviceDefinition.service,
configKey: serviceDefinition.configKey
});
}
}

// configure all required services
for (i = 0, l = servicesToLoad.length; i < l; ++i) {
service = servicesToLoad[i];
for (let i = 0, l = servicesToLoad.length; i < l; ++i) {
let service = servicesToLoad[i];

// in case the service class has a config key defined
// in case the service has a config key defined
// try to load the service specific config and pass this to the configure method.
if (typeof service.definition.CONFIG_KEY !== 'undefined') {
if (typeof service.configKey === 'string') {
service.instance.configure(
configService.get(service.definition.CONFIG_KEY)
configService.get(service.configKey)
);
} else {
service.instance.configure();
}
}

// bootstrap all required services
for (i = 0, l = servicesToLoad.length; i < l; ++i) {
for (let i = 0, l = servicesToLoad.length; i < l; ++i) {
servicesToLoad[i].instance.bootstrap();
}

// launch all required services
for (i = 0, l = servicesToLoad.length; i < l; ++i) {
for (let i = 0, l = servicesToLoad.length; i < l; ++i) {
servicesToLoad[i].instance.launch();
}

Expand Down
205 changes: 205 additions & 0 deletions test/Application.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,209 @@ describe('srvoa::application', function() {
assert(app.getServiceManager().has('s2'));
assert(app.getServiceManager().has('s3'));
});

it('should be able to handle `app.services` object and function definition.', function() {
var app = new Application;

app.configure({
configs: [{
app: {
services: {
test: {
service: Service
},
test2: Service,
test3: {
service: null,
},
test4: null
}
}
}]
}).bootstrap().launch();

assert(
app.getServiceManager().get('test') instanceof Service,
'Expect service `test` to be a service within the service manager of type `Service`.'
);

assert(
app.getServiceManager().get('test2') instanceof Service,
'Expect service `test2` to be a service within the service manager of type `Service`.'
);

assert(
app.getServiceManager().has('test3') === false,
'Expect service `test3` to not exists within the service manager.'
);

assert(
app.getServiceManager().has('test4') === false,
'Expect service `test4` to not exists within the service manager.'
);
});

it('should configure the services by service key (as config key) from `app.services` as a fallback.', function() {
var app = new Application;

app.configure({
configs: [{
app: {
services: {
test: Service,
test2: Service
}
}
}, {
test: {
foo: 'bar'
}
}]
}).bootstrap().launch();

assert(
app.getServiceManager().get('test').serviceConfig.get('foo') === 'bar',
'Expect service `test` to be configured with a foo: bar through service key fallback.'
);

assert(
Object.keys(app.getServiceManager().get('test2').serviceConfig.getHash()).length === 0,
'Expect service `test2` to be configured with no config through service key fallback with non existent config.'
);
});

it('should configure the services by static CONFIG_KEY rather then by service key.', function() {
class ServiceWithStaticConfigKey extends Service {
static get CONFIG_KEY() {
return 'test-test';
}
}

class ServiceWithStaticConfigKeyNull extends Service {
static get CONFIG_KEY() {
return null;
}
}

var app = new Application;

app.configure({
configs: [{
app: {
services: {
test: ServiceWithStaticConfigKey,
test2: {
service: ServiceWithStaticConfigKeyNull
}
}
}
}, {
'test-test': {
foo: 'bar'
}
}]
}).bootstrap().launch();

assert(
app.getServiceManager().get('test').serviceConfig.get('foo') === 'bar',
'Expect service `test` to be configured with a foo: bar through static CONFIG_KEY.'
);

assert(
Object.keys(app.getServiceManager().get('test2').serviceConfig.getHash()).length === 0,
'Expect service `test2` to be configured with no config through static CONFIG_KEY that equals `null`.'
);
});

it('should configure the services by configKey property rather then by static CONFIG_KEY.', function() {
class ServiceWithMultipleConfigKeys extends Service {
static get CONFIG_KEY() {
return 'test-test';
}

get configKey() {
return 'test';
}
}

class ServiceWithMultipleConfigKeysNull extends Service {
static get CONFIG_KEY() {
return 'test';
}

get configKey() {
return null;
}
}

var app = new Application;

app.configure({
configs: [{
app: {
services: {
test: ServiceWithMultipleConfigKeys,
test2: {
service: ServiceWithMultipleConfigKeysNull
}
}
}
}, {
'test': {
foo: 'bar'
}
}]
}).bootstrap().launch();

assert(
app.getServiceManager().get('test').serviceConfig.get('foo') === 'bar',
'Expect service `test` to be configured with a foo: bar through configKey property.'
);

assert(
Object.keys(app.getServiceManager().get('test2').serviceConfig.getHash()).length === 0,
'Expect service `test2` to be configured with no config through configKey property that equals `null`.'
);
});

it('should configure the services by configKey from service definition rather then by the service\'s configKey property.', function() {
class ServiceWithConfigKey extends Service {
get configKey() {
return 'test';
}
}

var app = new Application;

app.configure({
configs: [{
app: {
services: {
test: {
service: ServiceWithConfigKey,
configKey: 'test-test'
},
test2: {
service: ServiceWithConfigKey,
configKey: null
}
}
}
}, {
'test-test': {
foo: 'bar'
}
}]
}).bootstrap().launch();

assert(
app.getServiceManager().get('test').serviceConfig.get('foo') === 'bar',
'Expect service `test` to be configured with a foo: bar through the service definition configKey property.'
);

assert(
Object.keys(app.getServiceManager().get('test2').serviceConfig.getHash()).length === 0,
'Expect service `test2` to be configured with no config through the service definition configKey property that equals `null`.'
);
});
});

0 comments on commit 0ae40e2

Please sign in to comment.