Skip to content

Commit

Permalink
Remove automatic injection behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
dfreeman committed Oct 2, 2018
1 parent 76281d7 commit 9d76361
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 96 deletions.
25 changes: 4 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,37 +56,20 @@ Namespaces will automatically be differentiated by color, and the time between m

### Automatic Namespacing

By default, this addon will inject a `debug` method on to all routes, components, controllers, and services that are instantiated by your application's container. This method will automatically use its instance's container key as the namespace.
This addon exports a `debugLogger` function you can attach to a class definition. The resulting method will automatically use its instance's container key as the namespace.

```js
// app/routes/index.js
import Route from '@ember/route';
import debugLogger from 'ember-debug-logger';

export default Route.extend({
debug: debugLogger();

activate() {
this.debug('Hello from the application index.');
}
});
```

![image](https://cloud.githubusercontent.com/assets/108688/8262107/e0e71bb8-169d-11e5-9b74-9a895ed7e418.png)


You can also manually add the method when defining any other class that will be instantiated by the container.

```js
// app/adapters/application.js
import DS from 'ember-data';
import debugLogger from 'ember-debug-logger';

export default DS.JSONAPIAdapter.extend({
debug: debugLogger(),

init() {
this._super(...arguments);
this.debug('Hello from the application adapter!');
}
});
```

![image](https://cloud.githubusercontent.com/assets/108688/8262918/52e85f82-16a4-11e5-9b00-22e95e3848ae.png)
18 changes: 0 additions & 18 deletions addon/instance-initializers/debug-logger.ts

This file was deleted.

1 change: 0 additions & 1 deletion app/instance-initializers/debug-logger.js

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"devDependencies": {
"@ember/optional-features": "^0.6.3",
"@types/ember": "*",
"@types/ember-qunit": "^3.0.1",
"@types/ember-testing-helpers": "^0.0.3",
"@types/ember-qunit": "^3.4.2",
"@types/ember__test-helpers": "^0.7.5",
"@types/qunit": "^2.0.31",
"@types/rsvp": "^4.0.1",
"@types/sinon": "^4.1.3",
Expand Down
38 changes: 19 additions & 19 deletions tests/acceptance/container-keys-test.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
import Route from '@ember/routing/route';
import Service from '@ember/service';
import debug from 'debug';
import moduleForAcceptance from 'dummy/tests/helpers/module-for-acceptance';
import { TestContext } from 'ember-test-helpers';
import { test } from 'qunit';
import debugLogger from 'ember-debug-logger';
import { module, test } from 'qunit';
import { setupApplicationTest } from 'ember-qunit';
import sinon, { SinonStub } from 'sinon';
import { visit } from '@ember/test-helpers';
import { TestContext } from 'ember-test-helpers';

let log: SinonStub;
module('Acceptance | logging from container-managed objects', function(hooks) {
let log: SinonStub;

moduleForAcceptance('Acceptance | logging from container-managed objects', {
beforeEach(this: TestContext) {
this.application.register('route:application', Route);
this.application.register('service:my/test/module', Service);
setupApplicationTest(hooks);

hooks.beforeEach(function(this: TestContext) {
this.owner.register('route:application', Route.extend({ debug: debugLogger() }), {});
this.owner.register('service:my/test/module', Service.extend({ debug: debugLogger() }), {});

debug.enable('route:*, service:*');
log = sinon.stub(console, 'log');
},
});

afterEach() {
hooks.afterEach(function() {
log.restore();
debug.disable();
},
});

test('it automatically finds keys when attached to container-managed objects', function(assert) {
let lookup = (key: string) => (this.application as any).__container__.lookup(key);
});

visit('/');
test('it automatically finds keys when attached to container-managed objects', async function(assert) {
await visit('/');

andThen(() => {
const appRoute = lookup('route:application');
const appRoute = this.owner.lookup('route:application');
appRoute.debug('test message from the application route');

let [routeMessage] = log.lastCall.args;
assert.ok(/route:application/.test(routeMessage), 'Route message should include its container key');

const testService = lookup('service:my/test/module');
const testService = this.owner.lookup('service:my/test/module');
testService.debug('test message from the mysterious service');

let [serviceMessage] = log.lastCall.args;
Expand Down
46 changes: 23 additions & 23 deletions tests/unit/utils/debug-logger-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,38 @@ import debugLogger from 'ember-debug-logger';
import { module, test } from 'qunit';
import sinon, { SinonStub } from 'sinon';

let log: SinonStub;
module('Unit | Utility | debug logger', function(hooks) {
let log: SinonStub;

module('Unit | Utility | debug logger', {
beforeEach() {
hooks.beforeEach(function() {
debug.enable('test:sample-key');
log = sinon.stub(console, 'log');
},
});

afterEach() {
hooks.afterEach(function() {
debug.disable();
log.restore();
},
});
});

test('it honors an explicit key', function(assert) {
const logger = debugLogger('test:sample-key');
logger('placeholder message');
test('it honors an explicit key', function(assert) {
const logger = debugLogger('test:sample-key');
logger('placeholder message');

let [message] = A(log.args).get('lastObject') || [''];
assert.ok(/test:sample-key/.test(message), 'Log entry should contain the namespace');
assert.ok(/placeholder message/.test(message), 'Log entry should contain the message');
});
let [message] = A(log.args).get('lastObject') || [''];
assert.ok(/test:sample-key/.test(message), 'Log entry should contain the namespace');
assert.ok(/placeholder message/.test(message), 'Log entry should contain the message');
});

test('it throws with a useful message when it can\'t determine a key', function(assert) {
const logger = debugLogger();
test('it throws with a useful message when it can\'t determine a key', function(assert) {
const logger = debugLogger();

assert.throws(function() {
logger('message');
}, /explicit key/);
assert.throws(function() {
logger('message');
}, /explicit key/);

assert.throws(function() {
let object = { debug: logger };
object.debug('message');
}, /explicit key/);
assert.throws(function() {
let object = { debug: logger };
object.debug('message');
}, /explicit key/);
});
});
30 changes: 18 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@
dependencies:
"@types/rsvp" "*"

"@types/ember-qunit@^3.0.1":
version "3.0.1"
resolved "https://registry.yarnpkg.com/@types/ember-qunit/-/ember-qunit-3.0.1.tgz#ddf7f25e75eb0a91f18e159369876bf02496dd38"
integrity sha512-qXstV0xyCL8aNbmjHbt2UbmD995cRLeqa4woYLLarTNp62AKJWA1Qy+90BZSyLuV0CxZsNAPo7+TpAdyVDUsaw==
"@types/ember-qunit@^3.4.2":
version "3.4.2"
resolved "https://registry.yarnpkg.com/@types/ember-qunit/-/ember-qunit-3.4.2.tgz#90a81afca9c5baada588f44a71919ee6cad5901f"
integrity sha512-1o99nikJmXgXhyL670P4zCEqBm9ebqmKDPgyeaL6p9xu1HFQEXw4T/LvwQb1NDXBm7eMia7SceUG5qoy8ADKcQ==
dependencies:
"@types/ember" "*"
"@types/ember-test-helpers" "*"
Expand All @@ -125,14 +125,6 @@
"@types/jquery" "*"
"@types/rsvp" "*"

"@types/ember-testing-helpers@^0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@types/ember-testing-helpers/-/ember-testing-helpers-0.0.3.tgz#1a6cfc484b63d19ddd822c87e4dd710597db17d9"
integrity sha512-QG3QBBR7PFzz3zhLTbsZWBgk3cNQIZYVG6rbXKMM36+YP3dcSkkWQ6CRTyQImUIfgAkYPMaWqGlGEtkuanq3Bg==
dependencies:
"@types/jquery" "*"
"@types/rsvp" "*"

"@types/ember@*":
version "2.8.8"
resolved "https://registry.yarnpkg.com/@types/ember/-/ember-2.8.8.tgz#a3621385cb91a2ff4a9e2cd2a5f0f7c33b9f5d5d"
Expand All @@ -142,6 +134,20 @@
"@types/jquery" "*"
"@types/rsvp" "*"

"@types/ember__error@*":
version "3.0.1"
resolved "https://registry.yarnpkg.com/@types/ember__error/-/ember__error-3.0.1.tgz#1e915599d8d72cc4cf35473ee8a22d9cab036824"
integrity sha512-JON2moBi7Y6uf0bw1qrBalm7jxr4zQe4T2Q5flZLmPaRIrLTO5csQgffJGhwjFX0sAQgW1W2m9X8sPzgi8or1w==

"@types/ember__test-helpers@^0.7.5":
version "0.7.5"
resolved "https://registry.yarnpkg.com/@types/ember__test-helpers/-/ember__test-helpers-0.7.5.tgz#219305edc96875995114952a3b7902a8fbe754a1"
integrity sha512-7E1XVzzGDXERJFxKSPAoiCs550DGEHiRtFBTPjiQ5m3gAV5Aeet0HP0/ODAccXE7PuI8wJXzrXkRgceviVfJxQ==
dependencies:
"@types/ember" "*"
"@types/ember__error" "*"
"@types/htmlbars-inline-precompile" "*"

"@types/handlebars@*":
version "4.0.36"
resolved "https://registry.yarnpkg.com/@types/handlebars/-/handlebars-4.0.36.tgz#ff57c77fa1ab6713bb446534ddc4d979707a3a79"
Expand Down

0 comments on commit 9d76361

Please sign in to comment.