spine-rails 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  (function() {
2
- if (typeof Spine === "undefined" || Spine === null) {
3
- Spine = require('spine');
4
- }
2
+
3
+ if (typeof Spine === "undefined" || Spine === null) Spine = require('spine');
4
+
5
5
  Spine.Model.Local = {
6
6
  extended: function() {
7
7
  this.change(this.saveLocal);
@@ -20,7 +20,9 @@
20
20
  });
21
21
  }
22
22
  };
23
+
23
24
  if (typeof module !== "undefined" && module !== null) {
24
25
  module.exports = Spine.Model.Local;
25
26
  }
27
+
26
28
  }).call(this);
@@ -1,25 +1,25 @@
1
1
  (function() {
2
- var $;
3
- var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
4
- for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
5
- function ctor() { this.constructor = child; }
6
- ctor.prototype = parent.prototype;
7
- child.prototype = new ctor;
8
- child.__super__ = parent.prototype;
9
- return child;
10
- }, __slice = Array.prototype.slice, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
11
- if (typeof Spine === "undefined" || Spine === null) {
12
- Spine = require('spine');
13
- }
2
+ var $,
3
+ __hasProp = Object.prototype.hasOwnProperty,
4
+ __extends = 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
+ __slice = Array.prototype.slice;
6
+
7
+ if (typeof Spine === "undefined" || Spine === null) Spine = require('spine');
8
+
14
9
  $ = Spine.$;
15
- Spine.Manager = (function() {
16
- __extends(Manager, Spine.Module);
10
+
11
+ Spine.Manager = (function(_super) {
12
+
13
+ __extends(Manager, _super);
14
+
17
15
  Manager.include(Spine.Events);
16
+
18
17
  function Manager() {
19
18
  this.controllers = [];
20
19
  this.bind('change', this.change);
21
20
  this.add.apply(this, arguments);
22
21
  }
22
+
23
23
  Manager.prototype.add = function() {
24
24
  var cont, controllers, _i, _len, _results;
25
25
  controllers = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
@@ -30,20 +30,24 @@
30
30
  }
31
31
  return _results;
32
32
  };
33
+
33
34
  Manager.prototype.addOne = function(controller) {
34
- controller.bind('active', __bind(function() {
35
+ var _this = this;
36
+ controller.bind('active', function() {
35
37
  var args;
36
38
  args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
37
- return this.trigger.apply(this, ['change', controller].concat(__slice.call(args)));
38
- }, this));
39
- controller.bind('release', __bind(function() {
40
- return this.controllers.splice(this.controllers.indexOf(controller), 1);
41
- }, this));
39
+ return _this.trigger.apply(_this, ['change', controller].concat(__slice.call(args)));
40
+ });
41
+ controller.bind('release', function() {
42
+ return _this.controllers.splice(_this.controllers.indexOf(controller), 1);
43
+ });
42
44
  return this.controllers.push(controller);
43
45
  };
46
+
44
47
  Manager.prototype.deactivate = function() {
45
48
  return this.trigger.apply(this, ['change', false].concat(__slice.call(arguments)));
46
49
  };
50
+
47
51
  Manager.prototype.change = function() {
48
52
  var args, cont, current, _i, _len, _ref, _results;
49
53
  current = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
@@ -51,12 +55,19 @@
51
55
  _results = [];
52
56
  for (_i = 0, _len = _ref.length; _i < _len; _i++) {
53
57
  cont = _ref[_i];
54
- _results.push(cont === current ? cont.activate.apply(cont, args) : cont.deactivate.apply(cont, args));
58
+ if (cont === current) {
59
+ _results.push(cont.activate.apply(cont, args));
60
+ } else {
61
+ _results.push(cont.deactivate.apply(cont, args));
62
+ }
55
63
  }
56
64
  return _results;
57
65
  };
66
+
58
67
  return Manager;
59
- })();
68
+
69
+ })(Spine.Module);
70
+
60
71
  Spine.Controller.include({
61
72
  active: function() {
62
73
  var args;
@@ -81,15 +92,27 @@
81
92
  return this;
82
93
  }
83
94
  });
84
- Spine.Stack = (function() {
85
- __extends(Stack, Spine.Controller);
95
+
96
+ Spine.Stack = (function(_super) {
97
+
98
+ __extends(Stack, _super);
99
+
86
100
  Stack.prototype.controllers = {};
101
+
87
102
  Stack.prototype.routes = {};
103
+
88
104
  Stack.prototype.className = 'spine stack';
105
+
89
106
  function Stack() {
90
- var key, value, _fn, _ref, _ref2;
107
+ var key, value, _fn, _ref, _ref2,
108
+ _this = this;
91
109
  Stack.__super__.constructor.apply(this, arguments);
92
110
  this.manager = new Spine.Manager;
111
+ this.manager.bind('change', function() {
112
+ var args, controller;
113
+ controller = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
114
+ if (controller) return _this.active.apply(_this, args);
115
+ });
93
116
  _ref = this.controllers;
94
117
  for (key in _ref) {
95
118
  value = _ref[key];
@@ -99,32 +122,33 @@
99
122
  this.add(this[key]);
100
123
  }
101
124
  _ref2 = this.routes;
102
- _fn = __bind(function(key, value) {
125
+ _fn = function(key, value) {
103
126
  var callback;
104
- if (typeof value === 'function') {
105
- callback = value;
106
- }
107
- callback || (callback = __bind(function() {
127
+ if (typeof value === 'function') callback = value;
128
+ callback || (callback = function() {
108
129
  var _ref3;
109
- return (_ref3 = this[value]).active.apply(_ref3, arguments);
110
- }, this));
111
- return this.route(key, callback);
112
- }, this);
130
+ return (_ref3 = _this[value]).active.apply(_ref3, arguments);
131
+ });
132
+ return _this.route(key, callback);
133
+ };
113
134
  for (key in _ref2) {
114
135
  value = _ref2[key];
115
136
  _fn(key, value);
116
137
  }
117
- if (this["default"]) {
118
- this[this["default"]].active();
119
- }
138
+ if (this["default"]) this[this["default"]].active();
120
139
  }
140
+
121
141
  Stack.prototype.add = function(controller) {
122
142
  this.manager.add(controller);
123
143
  return this.append(controller);
124
144
  };
145
+
125
146
  return Stack;
126
- })();
147
+
148
+ })(Spine.Controller);
149
+
127
150
  if (typeof module !== "undefined" && module !== null) {
128
151
  module.exports = Spine.Manager;
129
152
  }
153
+
130
154
  }).call(this);
@@ -1,145 +1,175 @@
1
1
  (function() {
2
- var Collection, Instance, Singleton, singularize, underscore;
3
- var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
4
- for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
5
- function ctor() { this.constructor = child; }
6
- ctor.prototype = parent.prototype;
7
- child.prototype = new ctor;
8
- child.__super__ = parent.prototype;
9
- return child;
10
- }, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
11
- if (typeof Spine === "undefined" || Spine === null) {
12
- Spine = require('spine');
13
- }
2
+ var Collection, Instance, Singleton, isArray, singularize, underscore,
3
+ __hasProp = Object.prototype.hasOwnProperty,
4
+ __extends = 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
+
6
+ if (typeof Spine === "undefined" || Spine === null) Spine = require('spine');
7
+
8
+ isArray = Spine.isArray;
9
+
14
10
  if (typeof require === "undefined" || require === null) {
15
11
  require = (function(value) {
16
12
  return eval(value);
17
13
  });
18
14
  }
19
- Collection = (function() {
20
- __extends(Collection, Spine.Module);
15
+
16
+ Collection = (function(_super) {
17
+
18
+ __extends(Collection, _super);
19
+
21
20
  function Collection(options) {
22
21
  var key, value;
23
- if (options == null) {
24
- options = {};
25
- }
22
+ if (options == null) options = {};
26
23
  for (key in options) {
27
24
  value = options[key];
28
25
  this[key] = value;
29
26
  }
30
27
  }
28
+
31
29
  Collection.prototype.all = function() {
32
- return this.model.select(__bind(function(rec) {
33
- return this.associated(rec);
34
- }, this));
30
+ var _this = this;
31
+ return this.model.select(function(rec) {
32
+ return _this.associated(rec);
33
+ });
35
34
  };
35
+
36
36
  Collection.prototype.first = function() {
37
37
  return this.all()[0];
38
38
  };
39
+
39
40
  Collection.prototype.last = function() {
40
41
  var values;
41
42
  values = this.all();
42
43
  return values[values.length - 1];
43
44
  };
45
+
44
46
  Collection.prototype.find = function(id) {
45
- var records;
46
- records = this.model.select(__bind(function(rec) {
47
- return this.associated(rec) && rec.id === id;
48
- }, this));
49
- if (!records[0]) {
50
- throw 'Unknown record';
51
- }
47
+ var records,
48
+ _this = this;
49
+ records = this.select(function(rec) {
50
+ return rec.id + '' === id + '';
51
+ });
52
+ if (!records[0]) throw 'Unknown record';
52
53
  return records[0];
53
54
  };
55
+
56
+ Collection.prototype.findAllByAttribute = function(name, value) {
57
+ var _this = this;
58
+ return this.model.select(function(rec) {
59
+ return rec[name] === value;
60
+ });
61
+ };
62
+
63
+ Collection.prototype.findByAttribute = function(name, value) {
64
+ return this.findAllByAttribute(name, value)[0];
65
+ };
66
+
54
67
  Collection.prototype.select = function(cb) {
55
- return this.model.select(__bind(function(rec) {
56
- return this.associated(rec) && cb(rec);
57
- }, this));
68
+ var _this = this;
69
+ return this.model.select(function(rec) {
70
+ return _this.associated(rec) && cb(rec);
71
+ });
58
72
  };
73
+
59
74
  Collection.prototype.refresh = function(values) {
60
- var record, records, value, _i, _j, _len, _len2;
61
- records = this.all();
62
- for (_i = 0, _len = records.length; _i < _len; _i++) {
63
- record = records[_i];
75
+ var record, records, _i, _j, _len, _len2, _ref;
76
+ _ref = this.all();
77
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
78
+ record = _ref[_i];
64
79
  delete this.model.records[record.id];
65
80
  }
66
- values = this.model.fromJSON(values);
67
- for (_j = 0, _len2 = values.length; _j < _len2; _j++) {
68
- value = values[_j];
69
- value.newRecord = false;
70
- value[this.fkey] = this.record.id;
71
- this.model.records[value.id] = value;
81
+ records = this.model.fromJSON(values);
82
+ if (!isArray(records)) records = [records];
83
+ for (_j = 0, _len2 = records.length; _j < _len2; _j++) {
84
+ record = records[_j];
85
+ record.newRecord = false;
86
+ record[this.fkey] = this.record.id;
87
+ this.model.records[record.id] = record;
72
88
  }
73
- return this.model.trigger('refresh');
89
+ return this.model.trigger('refresh', records);
74
90
  };
91
+
75
92
  Collection.prototype.create = function(record) {
76
93
  record[this.fkey] = this.record.id;
77
94
  return this.model.create(record);
78
95
  };
96
+
79
97
  Collection.prototype.associated = function(record) {
80
98
  return record[this.fkey] === this.record.id;
81
99
  };
100
+
82
101
  return Collection;
83
- })();
84
- Instance = (function() {
85
- __extends(Instance, Spine.Module);
102
+
103
+ })(Spine.Module);
104
+
105
+ Instance = (function(_super) {
106
+
107
+ __extends(Instance, _super);
108
+
86
109
  function Instance(options) {
87
110
  var key, value;
88
- if (options == null) {
89
- options = {};
90
- }
111
+ if (options == null) options = {};
91
112
  for (key in options) {
92
113
  value = options[key];
93
114
  this[key] = value;
94
115
  }
95
116
  }
117
+
96
118
  Instance.prototype.exists = function() {
97
119
  return this.record[this.fkey] && this.model.exists(this.record[this.fkey]);
98
120
  };
121
+
99
122
  Instance.prototype.update = function(value) {
123
+ if (!(value instanceof this.model)) value = new this.model(value);
124
+ if (value.isNew()) value.save();
100
125
  return this.record[this.fkey] = value && value.id;
101
126
  };
127
+
102
128
  return Instance;
103
- })();
104
- Singleton = (function() {
105
- __extends(Singleton, Spine.Module);
129
+
130
+ })(Spine.Module);
131
+
132
+ Singleton = (function(_super) {
133
+
134
+ __extends(Singleton, _super);
135
+
106
136
  function Singleton(options) {
107
137
  var key, value;
108
- if (options == null) {
109
- options = {};
110
- }
138
+ if (options == null) options = {};
111
139
  for (key in options) {
112
140
  value = options[key];
113
141
  this[key] = value;
114
142
  }
115
143
  }
144
+
116
145
  Singleton.prototype.find = function() {
117
146
  return this.record.id && this.model.findByAttribute(this.fkey, this.record.id);
118
147
  };
148
+
119
149
  Singleton.prototype.update = function(value) {
120
- if (value != null) {
121
- value[this.fkey] = this.id;
122
- }
123
- return value;
150
+ if (!(value instanceof this.model)) value = this.model.fromJSON(value);
151
+ value[this.fkey] = this.record.id;
152
+ return value.save();
124
153
  };
154
+
125
155
  return Singleton;
126
- })();
156
+
157
+ })(Spine.Module);
158
+
127
159
  singularize = function(str) {
128
160
  return str.replace(/s$/, '');
129
161
  };
162
+
130
163
  underscore = function(str) {
131
164
  return str.replace(/::/g, '/').replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2').replace(/([a-z\d])([A-Z])/g, '$1_$2').replace(/-/g, '_').toLowerCase();
132
165
  };
166
+
133
167
  Spine.Model.extend({
134
168
  hasMany: function(name, model, fkey) {
135
169
  var association;
136
- if (fkey == null) {
137
- fkey = "" + (underscore(this.className)) + "_id";
138
- }
170
+ if (fkey == null) fkey = "" + (underscore(this.className)) + "_id";
139
171
  association = function(record) {
140
- if (typeof model === 'string') {
141
- model = require(model);
142
- }
172
+ if (typeof model === 'string') model = require(model);
143
173
  return new Collection({
144
174
  name: name,
145
175
  model: model,
@@ -148,21 +178,15 @@
148
178
  });
149
179
  };
150
180
  return this.prototype[name] = function(value) {
151
- if (value != null) {
152
- association(this).refresh(value);
153
- }
181
+ if (value != null) association(this).refresh(value);
154
182
  return association(this);
155
183
  };
156
184
  },
157
185
  belongsTo: function(name, model, fkey) {
158
186
  var association;
159
- if (fkey == null) {
160
- fkey = "" + (singularize(name)) + "_id";
161
- }
187
+ if (fkey == null) fkey = "" + (singularize(name)) + "_id";
162
188
  association = function(record) {
163
- if (typeof model === 'string') {
164
- model = require(model);
165
- }
189
+ if (typeof model === 'string') model = require(model);
166
190
  return new Instance({
167
191
  name: name,
168
192
  model: model,
@@ -171,22 +195,16 @@
171
195
  });
172
196
  };
173
197
  this.prototype[name] = function(value) {
174
- if (value != null) {
175
- association(this).update(value);
176
- }
198
+ if (value != null) association(this).update(value);
177
199
  return association(this).exists();
178
200
  };
179
201
  return this.attributes.push(fkey);
180
202
  },
181
203
  hasOne: function(name, model, fkey) {
182
204
  var association;
183
- if (fkey == null) {
184
- fkey = "" + (underscore(this.className)) + "_id";
185
- }
205
+ if (fkey == null) fkey = "" + (underscore(this.className)) + "_id";
186
206
  association = function(record) {
187
- if (typeof model === 'string') {
188
- model = require(model);
189
- }
207
+ if (typeof model === 'string') model = require(model);
190
208
  return new Singleton({
191
209
  name: name,
192
210
  model: model,
@@ -195,11 +213,10 @@
195
213
  });
196
214
  };
197
215
  return this.prototype[name] = function(value) {
198
- if (value != null) {
199
- association(this).update(value);
200
- }
216
+ if (value != null) association(this).update(value);
201
217
  return association(this).find();
202
218
  };
203
219
  }
204
220
  });
221
+
205
222
  }).call(this);