UNPKG

ember-legacy-class-transform

Version:
52 lines 1.5 kB
import { Blueprint, Descriptor } from './mixin'; import { ComputedBlueprint } from './computed'; class AliasMethodDescriptor extends Descriptor { constructor(name) { super(); this.name = name; } define(target, key, _home) { let name = this.name; Object.defineProperty(target, key, { enumerable: true, configurable: true, get() { return this[name]; } }); } } class AliasMethodBlueprint extends Blueprint { constructor(name) { super(); this.name = name; } descriptor(_target, _key, _meta) { return new AliasMethodDescriptor(this.name); } } export function aliasMethod(name) { return new AliasMethodBlueprint(name); } class AliasBlueprint extends ComputedBlueprint { constructor(name) { let parent = name.slice(0, -1); let last = name[name.length - 1]; let get = function () { return name.reduce((obj, n) => obj[n], this); }; let set = function (value) { let p = parent.reduce((obj, n) => obj[n], this); p[last] = value; }; super({ get, set }, [name]); this.name = name; } descriptor(target, key, meta) { if (this.name[0] === key) throw new Error(`Setting alias '${key}' on self`); return super.descriptor(target, key, meta); } } export function alias(name) { return new AliasBlueprint(name.split('.')); }