1 |
|
2 | (function() {
|
3 | var Composed, _;
|
4 |
|
5 | _ = require('underscore');
|
6 |
|
7 | Composed = (function() {
|
8 | function Composed() {}
|
9 |
|
10 | Composed.prototype.assign = function(clazz) {
|
11 | this.clazz = clazz;
|
12 | return this.rebuild();
|
13 | };
|
14 |
|
15 | Composed.prototype.rebuild = function() {
|
16 | return this.parts = _.reduce(this.raw, function(memo, component) {
|
17 | memo.push(new this.clazz(component));
|
18 | return memo;
|
19 | }, [], this);
|
20 | };
|
21 |
|
22 | Composed.prototype.getPart = function(index) {
|
23 | var val = this.parts[index];
|
24 |
|
25 | return val === undefined ? null : val;
|
26 | };
|
27 |
|
28 | Composed.prototype.val = function() {
|
29 | if (arguments.length === 0) {
|
30 | if (this.parts) {
|
31 | return _.map(_.flatten(this.parts), function(part) {
|
32 | return part.val();
|
33 | }).join(this.delimiter);
|
34 | } else {
|
35 | return this.raw;
|
36 | }
|
37 | } else {
|
38 | this.setVal(_.first(arguments));
|
39 | return this.rebuild();
|
40 | }
|
41 | };
|
42 |
|
43 | Composed.prototype.setVal = function(values) {
|
44 | return this.raw = values;
|
45 | };
|
46 |
|
47 | Composed.prototype.toString = function() {
|
48 | if (this.parts) {
|
49 | return _.map(this.parts, function(part) {
|
50 | return part.toString();
|
51 | }).join(this.delimiter);
|
52 | } else if (_.isArray(this.raw)) {
|
53 | return this.raw.join(this.delimiter);
|
54 | } else {
|
55 | return this.raw;
|
56 | }
|
57 | };
|
58 |
|
59 | return Composed;
|
60 |
|
61 | })();
|
62 |
|
63 | module.exports = Composed;
|
64 |
|
65 | }).call(this);
|