UNPKG

3.75 kBJavaScriptView Raw
1// Generated by CoffeeScript 1.12.7
2(function() {
3 var defineModule, each, isPlainObject, log, lowerCamelCase, object, ref, upperCamelCase,
4 extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
5 hasProp = {}.hasOwnProperty;
6
7 ref = require('art-standard-lib'), defineModule = ref.defineModule, log = ref.log, object = ref.object, upperCamelCase = ref.upperCamelCase, lowerCamelCase = ref.lowerCamelCase, each = ref.each, isPlainObject = ref.isPlainObject;
8
9 defineModule(module, function() {
10 return function(superClass) {
11 var DeclarableMixin;
12 return DeclarableMixin = (function(superClass1) {
13 extend(DeclarableMixin, superClass1);
14
15 function DeclarableMixin() {
16 return DeclarableMixin.__super__.constructor.apply(this, arguments);
17 }
18
19
20 /*
21 define a declarable field
22
23 IN:
24 map:
25 key: name: string
26 value: true-ish OR
27 options:
28 preprocess: (v) -> newV
29 validate: (v) -> truthish
30 extendable: defaultValue
31 If present, this is an extendable property.
32 See: @extendableProperty
33 passed to: @extendableProperty "#{key}": options.extendable
34 NOTE: validate is evaluated BEFORE preprocess
35
36 EFFECT:
37 creates:
38
39 * class declarator function, with preprocessing
40 @name: (...)->
41
42 * class getter-function
43 @getName: ->
44
45 * instance-getter
46 @getter name: ->
47 */
48
49 DeclarableMixin.declarable = function(map) {
50 return each(map, (function(_this) {
51 return function(options, name) {
52 var extendable, getter, getterName, internalName, obj, preprocess, ucProp, validate;
53 if (isPlainObject(options)) {
54 preprocess = options.preprocess, validate = options.validate, extendable = options.extendable, getter = options.getter;
55 }
56 preprocess || (preprocess = function(v) {
57 return v;
58 });
59 validate || (validate = function() {
60 return true;
61 });
62 name = lowerCamelCase(name);
63 ucProp = upperCamelCase(name);
64 internalName = _this.propInternalName(name);
65 getterName = "get" + ucProp;
66 if (extendable) {
67 return _this.extendableProperty((
68 obj = {},
69 obj["" + name] = extendable,
70 obj
71 ));
72 } else {
73 _this[name] = function(value) {
74 if (!validate(value)) {
75 throw new Error("invalid value: " + (formattedInspect({
76 value: value,
77 name: name
78 })));
79 }
80 return this[internalName] = preprocess(value);
81 };
82 _this[getterName] = getter || function() {
83 return this[internalName];
84 };
85 return _this.addGetter(name, function() {
86 return this["class"][internalName];
87 });
88 }
89 };
90 })(this));
91 };
92
93 return DeclarableMixin;
94
95 })(superClass);
96 };
97 });
98
99}).call(this);
100
101//# sourceMappingURL=DeclarableMixin.js.map