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 | return this.parts[index];
|
24 | };
|
25 |
|
26 | Composed.prototype.val = function() {
|
27 | if (arguments.length === 0) {
|
28 | if (this.parts) {
|
29 | return _.map(_.flatten(this.parts), function(part) {
|
30 | return part.val();
|
31 | }).join(this.delimiter);
|
32 | } else {
|
33 | return this.raw;
|
34 | }
|
35 | } else {
|
36 | this.setVal(_.first(arguments));
|
37 | return this.rebuild();
|
38 | }
|
39 | };
|
40 |
|
41 | Composed.prototype.setVal = function(values) {
|
42 | return this.raw = values;
|
43 | };
|
44 |
|
45 | Composed.prototype.toString = function() {
|
46 | if (this.parts) {
|
47 | return _.map(this.parts, function(part) {
|
48 | return part.toString();
|
49 | }).join(this.delimiter);
|
50 | } else if (_.isArray(this.raw)) {
|
51 | return this.raw.join(this.delimiter);
|
52 | } else {
|
53 | return this.raw;
|
54 | }
|
55 | };
|
56 |
|
57 | return Composed;
|
58 |
|
59 | })();
|
60 |
|
61 | module.exports = Composed;
|
62 |
|
63 | }).call(this);
|