Skip to content

Commit

Permalink
get it working
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaurav0 committed Nov 26, 2024
1 parent 08ff6fd commit 5cfb4e5
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 53 deletions.
73 changes: 34 additions & 39 deletions addon/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,46 @@ const getValue = function (configKey) {
return get(configuration, propertyPath);
};

function configurableValue(configKey, defaultValue, originalGetter) {
const value = getValue.call(this, configKey);
if (typeof value === 'undefined') {
if (typeof defaultValue !== 'undefined') {
if (typeof defaultValue === 'function') {
return defaultValue.call(this);
} else {
return defaultValue;
}
} else if (typeof originalGetter === 'function') {
return originalGetter.call(this);
} else {
throw new Error(
'Expected configuration value ' +
configKey +
' to be defined for provider named ' +
this.get('name')
);
}
}
return value;
}

function configurable(configKey, defaultValue) {
// want to somehow determine if should return legacy or new decorator
return configurableLegacy(configKey, defaultValue);
const legacy = configurableLegacy(configKey, defaultValue);
const modern = configurableDecorator(configKey, defaultValue);
Object.assign(legacy, modern);
return legacy;
}

function configurableDecorator(configKey, defaultValue) {
return function (_target, _key, descriptor) {
const originalGetter = descriptor.get;
const getter = function () {
const value = getValue.call(this, configKey);

if (typeof value === 'undefined') {
if (typeof defaultValue !== 'undefined') {
if (typeof defaultValue === 'function') {
return defaultValue.call(this);
} else {
return defaultValue;
}
} else if (typeof originalGetter === 'function') {
return originalGetter.call(this);
} else {
throw new Error(
`Expected configuration value ${configKey} to be defined for provider named ${this.name}`
);
}
}

return value;
return configurableValue.call(
this,
configKey,
defaultValue,
originalGetter
);
};

return {
Expand All @@ -51,24 +63,7 @@ function configurableDecorator(configKey, defaultValue) {

function configurableLegacy(configKey, defaultValue) {
return computed(function configurableComputed() {
const value = getValue.call(this, configKey);
if (typeof value === 'undefined') {
if (typeof defaultValue !== 'undefined') {
if (typeof defaultValue === 'function') {
return defaultValue.call(this);
} else {
return defaultValue;
}
} else {
throw new Error(
'Expected configuration value ' +
configKey +
' to be defined for provider named ' +
this.get('name')
);
}
}
return value;
return configurableValue.call(this, configKey, defaultValue);
});
}

Expand Down
42 changes: 28 additions & 14 deletions tests/unit/configuration-test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
/* eslint-disable ember/no-classic-classes, ember/no-get */
import EmberObject from '@ember/object';
import { run } from '@ember/runloop';
import {
configurable,
configurableDecorator,
configure,
} from 'torii/configuration';
import { configurable, configure } from 'torii/configuration';
import { module, test } from 'qunit';

module('Unit | Configuration', function (hooks) {
let testable1, testable2, testables;

const Testable = EmberObject.extend({
type: 'Testable',
name: 'test',
required: configurable('apiKey'),
defaulted: configurable('scope', 'email'),
Expand All @@ -22,13 +19,14 @@ module('Unit | Configuration', function (hooks) {
});

const Testable2 = class extends EmberObject {
type = 'Testable2';
name = 'test';
@configurableDecorator('apiKey') required;
@configurableDecorator('scope', 'email') defaulted;
@configurable('apiKey') required;
@configurable('scope', 'email') defaulted;
defaultedFunctionValue = 'found-via-get';

// not supported by @computed due to limitations in decorators
@configurableDecorator('redirectUri')
@configurable('redirectUri')
get defaultedFunction() {
return this.get('defaultedFunctionValue');
}
Expand Down Expand Up @@ -66,8 +64,8 @@ module('Unit | Configuration', function (hooks) {
message = e.message;
}

assert.ok(threw, 'read threw');
assert.ok(
assert.true(threw, `${testable.type} read threw`);
assert.true(
/Expected configuration value "?apiKey"? to be defined for provider named "?test"?/.test(
message
),
Expand All @@ -91,7 +89,11 @@ module('Unit | Configuration', function (hooks) {

const value = testable.get('required');

assert.equal(value, 'item val');
assert.equal(
value,
'item val',
`${testable.type}.get('required') should equal 'item val'`
);
}
);

Expand All @@ -108,7 +110,11 @@ module('Unit | Configuration', function (hooks) {

const value = testable.get('defaulted');

assert.equal(value, 'email');
assert.equal(
value,
'email',
`${testable.type}.get('defaulted') should equal 'email'`
);
}
);

Expand All @@ -127,7 +133,11 @@ module('Unit | Configuration', function (hooks) {

const value = testable.get('defaulted');

assert.equal(value, 'baz');
assert.equal(
value,
'baz',
`${testable.type}.get('defaulted') should equal 'baz'`
);
}
);

Expand All @@ -138,7 +148,11 @@ module('Unit | Configuration', function (hooks) {
let testable = testables[testableToUse];
const value = testable.get('defaultedFunction');

assert.equal(value, 'found-via-get');
assert.equal(
value,
'found-via-get',
`${testable.type}.get('defaultedFunction') should equal 'found-via-get'`
);
}
);
});

0 comments on commit 5cfb4e5

Please sign in to comment.