Files
nomad/ui/app/models/node-attributes.js
Buck Doyle 24eadd269c Add massaged results of class codemod
Manual interventions:
• decorators on the same line for service and controller
  injections and most computed property macros
• preserving import order when possible, both per-line
  and intra-line
• moving new imports to the bottom
• removal of classic decorator for trivial cases
• conversion of init to constructor when appropriate
2020-06-10 16:18:42 -05:00

39 lines
1.0 KiB
JavaScript

import { get, computed } from '@ember/object';
import attr from 'ember-data/attr';
import Fragment from 'ember-data-model-fragments/fragment';
import flat from 'flat';
const { unflatten } = flat;
export default class NodeAttributes extends Fragment {
@attr() nodeAttributes;
@computed('nodeAttributes')
get attributesStructured() {
const original = this.nodeAttributes;
if (!original) {
return;
}
// `unflatten` doesn't sort keys before unflattening, so manual preprocessing is necessary.
const attrs = Object.keys(original)
.sort()
.reduce((obj, key) => {
obj[key] = original[key];
return obj;
}, {});
return unflatten(attrs, { overwrite: true });
}
unknownProperty(key) {
// Returns the exact value in index 0 and the subtree in index 1
//
// ex: nodeAttrs.get('driver.docker')
// [ "1", { version: "17.05.0-ce", volumes: { enabled: "1" } } ]
if (this.attributesStructured) {
return get(this.attributesStructured, key);
}
}
}