track-dsl
v0.0.8
Published
DSL support for track.
Downloads
2
Readme
TrackDSL
DSL support for track.
Installation
npm
npm install track-dsl
Usage
For example, create DSL having #getter
and #setter
.
class Hoge extends Base {
static definer() {
getter('foo'); // Define instance.foo
getter('bar'); // Define instance.bar
setter('piyo'); // Define instance.piyo=
}
}
Define DSL
const TrackDSL = require('track-dsl');
class Base {
constructor() {
const dsl = new TrackDSL(this, {
'getter': {func: this._defineGetter, binding: this},
'setter': {func: this._defineSetter, binding: this},
});
dsl.evaluate(this.constructor.definer);
}
_defineGetter(name) {
Object.defineProperty(this, name, {
get: function() {
return this[`_${name}`];
}
});
}
_defineSetter(name) {
Object.defineProperty(this, name, {
set: function(value) {
this[`_${name}`] = value;
}
});
}
}
Use
const hoge = new Hoge();
hoge.piyo = 'piyo';
hoge.foo;
Base class.
Use track-dsl/lib/base
.
const TrackDSLBase = require('track-dsl/lib/extendable');
class BaseA extend TrackDSLBase {
static dsl() {
return {
'getter': {func: this._defineGetter, binding: this},
}
}
constructor() {
this.evaluateDSL();
}
}
class BaseB extend BaseA {
static dsl() {
return {
'setter': {func: this._defineSetter, binding: this},
}
}
}
class ClassA extend BaseB {
static definer() {
getter('foo'); // Define instance.foo
}
}
class ClassB extend ClassA {
static definer() {
setter('bar'); // Define instance.bar
}
}
ClassB will define getter foo and setter bar.