Skip to content

Commit c14e54a

Browse files
committed
try creating new decorator
1 parent 191b827 commit c14e54a

File tree

2 files changed

+63
-12
lines changed

2 files changed

+63
-12
lines changed

addon/configuration.js

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,54 @@ import { get, computed } from '@ember/object';
44
const NAMESPACE = 'providers';
55
let configuration = {};
66

7+
const getValue = function (configKey) {
8+
const configNamespace = NAMESPACE + '.' + this.get('name');
9+
const propertyPath = configNamespace + '.' + configKey;
10+
const configuration = getConfiguration();
11+
return get(configuration, propertyPath);
12+
};
13+
714
function configurable(configKey, defaultValue) {
8-
return computed(function configurableComputed() {
9-
var configNamespace = NAMESPACE + '.' + this.get('name');
10-
var propertyPath = configNamespace + '.' + configKey;
11-
let configuration = getConfiguration();
12-
var value = get(configuration, propertyPath);
15+
// want to somehow determine if should return legacy or new decorator
16+
return configurableLegacy(configKey, defaultValue);
17+
}
18+
19+
function configurableDecorator(configKey, defaultValue) {
20+
return function (_target, _key, descriptor) {
21+
const originalGetter = descriptor.get;
22+
const getter = function () {
23+
const value = getValue.call(this, configKey);
24+
25+
if (typeof value === 'undefined') {
26+
if (typeof defaultValue !== 'undefined') {
27+
if (typeof defaultValue === 'function') {
28+
return defaultValue.call(this);
29+
} else {
30+
return defaultValue;
31+
}
32+
} else if (typeof originalGetter === 'function') {
33+
return originalGetter.call(this);
34+
} else {
35+
throw new Error(
36+
`Expected configuration value ${configKey} to be defined for provider named ${this.name}`
37+
);
38+
}
39+
}
40+
41+
return value;
42+
};
1343

44+
return {
45+
get: getter,
46+
enumerable: false,
47+
configurable: true,
48+
};
49+
};
50+
}
51+
52+
function configurableLegacy(configKey, defaultValue) {
53+
return computed(function configurableComputed() {
54+
const value = getValue.call(this, configKey);
1455
if (typeof value === 'undefined') {
1556
if (typeof defaultValue !== 'undefined') {
1657
if (typeof defaultValue === 'function') {
@@ -39,6 +80,12 @@ function getConfiguration() {
3980
return configuration;
4081
}
4182

42-
export { configurable, configure, getConfiguration };
83+
export {
84+
configurable,
85+
configurableLegacy,
86+
configurableDecorator,
87+
configure,
88+
getConfiguration,
89+
};
4390

4491
export default {};

tests/unit/configuration-test.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
/* eslint-disable ember/no-classic-classes, ember/no-get */
22
import EmberObject from '@ember/object';
33
import { run } from '@ember/runloop';
4-
import { configurable, configure } from 'torii/configuration';
4+
import {
5+
configurable,
6+
configurableDecorator,
7+
configure,
8+
} from 'torii/configuration';
59
import { module, test } from 'qunit';
610

711
module('Unit | Configuration', function (hooks) {
@@ -19,20 +23,20 @@ module('Unit | Configuration', function (hooks) {
1923

2024
const Testable2 = class extends EmberObject {
2125
name = 'test';
22-
@configurable('apiKey') required;
23-
@configurable('scope', 'email') defaulted;
26+
@configurableDecorator('apiKey') required;
27+
@configurableDecorator('scope', 'email') defaulted;
2428
defaultedFunctionValue = 'found-via-get';
2529

2630
// not supported by @computed due to limitations in decorators
27-
@configurable('redirectUri')
31+
@configurableDecorator('redirectUri')
2832
get defaultedFunction() {
2933
return this.get('defaultedFunctionValue');
3034
}
3135
};
3236

3337
hooks.beforeEach(function () {
3438
testable1 = Testable.create();
35-
testable2 = Testable2.create();
39+
testable2 = new Testable2();
3640
testables = { testable1, testable2 };
3741
});
3842

@@ -64,7 +68,7 @@ module('Unit | Configuration', function (hooks) {
6468

6569
assert.ok(threw, 'read threw');
6670
assert.ok(
67-
/Expected configuration value apiKey to be defined for provider named test/.test(
71+
/Expected configuration value "?apiKey"? to be defined for provider named "?test"?/.test(
6872
message
6973
),
7074
'did not have proper error: ' + message

0 commit comments

Comments
 (0)