Skip to content

Commit

Permalink
try creating new decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaurav0 committed Nov 26, 2024
1 parent 191b827 commit 08ff6fd
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 12 deletions.
59 changes: 53 additions & 6 deletions addon/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,54 @@ import { get, computed } from '@ember/object';
const NAMESPACE = 'providers';
let configuration = {};

const getValue = function (configKey) {
const configNamespace = NAMESPACE + '.' + this.get('name');
const propertyPath = configNamespace + '.' + configKey;
const configuration = getConfiguration();
return get(configuration, propertyPath);
};

function configurable(configKey, defaultValue) {
return computed(function configurableComputed() {
var configNamespace = NAMESPACE + '.' + this.get('name');
var propertyPath = configNamespace + '.' + configKey;
let configuration = getConfiguration();
var value = get(configuration, propertyPath);
// want to somehow determine if should return legacy or new decorator
return configurableLegacy(configKey, defaultValue);
}

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 {
get: getter,
enumberable: false,
configurable: true,
};
};
}

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') {
Expand Down Expand Up @@ -39,6 +80,12 @@ function getConfiguration() {
return configuration;
}

export { configurable, configure, getConfiguration };
export {
configurable,
configurableLegacy,
configurableDecorator,
configure,
getConfiguration,
};

export default {};
16 changes: 10 additions & 6 deletions tests/unit/configuration-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
/* eslint-disable ember/no-classic-classes, ember/no-get */
import EmberObject from '@ember/object';
import { run } from '@ember/runloop';
import { configurable, configure } from 'torii/configuration';
import {
configurable,
configurableDecorator,
configure,
} from 'torii/configuration';
import { module, test } from 'qunit';

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

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

// not supported by @computed due to limitations in decorators
@configurable('redirectUri')
@configurableDecorator('redirectUri')
get defaultedFunction() {
return this.get('defaultedFunctionValue');
}
};

hooks.beforeEach(function () {
testable1 = Testable.create();
testable2 = Testable2.create();
testable2 = new Testable2();
testables = { testable1, testable2 };
});

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

assert.ok(threw, 'read threw');
assert.ok(
/Expected configuration value apiKey to be defined for provider named test/.test(
/Expected configuration value "?apiKey"? to be defined for provider named "?test"?/.test(
message
),
'did not have proper error: ' + message
Expand Down

0 comments on commit 08ff6fd

Please sign in to comment.