emerald-rails 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,14 @@
1
1
  var Emerald = {
2
+ Version: "0.0.1",
2
3
  Core: {},
4
+ Controller: {},
3
5
  ActionView: {
4
6
  Elements: {},
5
7
  Events: {}
6
- }
8
+ },
9
+ Model: {}
7
10
  };
8
11
 
9
-
10
12
  Emerald.ActionView.extend = function(actions){
11
13
  // instance object to be returned
12
14
  var instance = new Object;
@@ -77,108 +79,119 @@ Emerald.ActionView.Events.defineDefaultEvent = function(domElement, _event) {
77
79
  return 'keyup';
78
80
  }
79
81
 
80
- function emeraldActionController(){
81
- this.extend = function(properties){
82
- var _this = this;
83
- _this.properties = properties;
82
+ Emerald.Controller.extend = function(actions){
83
+ var instance = new Object;
84
+ var _this = this;
85
+ _this.actions = actions;
84
86
 
85
- return new emeraldActionControllerInstance(properties);
87
+ // defines the method of the new controller instance
88
+ for (action in actions) {
89
+ instance[action] = actions[action];
86
90
  }
87
- }
88
91
 
89
- function emeraldActionControllerInstance(properties){
90
- var singleton = function() {
91
- this.self = function(properties){
92
- this.properties = properties;
93
-
94
- for (propertyName in properties) {
95
- if (!this[propertyName])
96
- this[propertyName] = properties[propertyName];
97
- }
92
+ instance.params = function(domElements){
93
+ var params = new Object;
98
94
 
99
- return this;
95
+ for (i = 0; i < domElements.length; i++) {
96
+ var element = domElements[i];
97
+ params[element.name] = element.value;
100
98
  }
101
99
 
102
- this.params = function(domElements){
103
- var params = new Object;
100
+ return params;
101
+ }
104
102
 
105
- for (i = 0; i < domElements.length; i++) {
106
- var element = domElements[i];
107
- params[element.name] = element.value;
108
- }
103
+ instance.persistView = true;
109
104
 
110
- return params;
111
- }
105
+ instance.persistViewCallback = function(JSON, observerObject) {
106
+ if (!observerObject)
107
+ observerObject = Emerald.Model.Observer;
112
108
 
113
- this.persistView = true;
109
+ if (this.persistView)
110
+ observerObject.update(JSON);
114
111
 
115
- this.persistViewCallback = function(JSON) {
116
- debugger;
117
- if (this.persistView)
118
- Emerald.modelObserver.update(JSON);
119
- return true;
120
- }
112
+ return true;
121
113
  }
122
114
 
123
- var instance = new singleton().self(properties);
124
115
  return instance;
125
116
  }
126
117
 
127
- function emeraldActiveModel(){
128
- this.extend = function(properties){
129
- var _this = this;
130
- _this.properties = properties;
118
+ Emerald.Model.extend = function(properties) {
119
+ var instance = new Object;
120
+ var _this = this;
121
+ this.properties = properties;
131
122
 
132
- return new emeraldActiveModelInstance(properties);
123
+ // defines the method of the new view instance
124
+ for (property in properties) {
125
+ instance[property] = properties[property];
133
126
  }
134
- }
135
127
 
136
- function emeraldActiveModelInstance(properties){
137
- var singleton = function() {
138
- this.self = function(properties){
139
- this.properties = properties;
140
- return this;
141
- }
128
+ // runs initialization on startup
129
+ if (this.properties.initialize) {
130
+ $(document).ready(function(){ _this.properties.initialize(); });
131
+ }
142
132
 
143
- this.attributes = function() {
144
- attributesValues = {};
145
- if (this.properties.attrAccessible) {
146
- var attributes = this.properties.attrAccessible;
147
- for (attribute in attributes) {
148
- var attributeName = attributes[attribute];
149
- if (this[attributeName])
150
- attributesValues[attributeName] = this[attributeName];
151
- }
152
- }
153
- return attributesValues;
154
- }
133
+ // defines attrAccessible fields
134
+ Emerald.Model.Attributes.initAttrAccessible(instance, properties.attrAccessible);
155
135
 
156
- this.route = function() { return this.properties.route; }
136
+ instance.get = function(attribute) {
137
+ if (_this.properties[attribute])
138
+ return _this.properties[attribute];
139
+ else
140
+ return null;
141
+ }
157
142
 
158
- this.get = function(attribute) {
159
- if (this.properties[attribute])
160
- return this.properties[attribute];
161
- else
162
- return "none";
163
- }
143
+ instance.save = function(data, andCallbackController, persistenceObject) {
144
+ if (!persistenceObject)
145
+ persistenceObject = Emerald.Persistence;
164
146
 
165
- this.save = function(data, andCallbackController) {
166
- // TODO verify that `data` fields are the ones listed in this.attrAccessible
167
- debugger;
168
- var persistence = new emeraldPersistence(this).save(data, andCallbackController);
169
- return persistence;
170
- }
147
+ // TODO verify that `data` fields are the ones listed in this.attrAccessible
148
+ return new persistenceObject(this).save(data, andCallbackController);
171
149
  }
172
150
 
173
- function createAccessibleAttributes(singleton, attributes) {
174
- for (attribute in attributes) {
175
- singleton.prototype[attributes[attribute]] = null;
151
+ return instance;
152
+ }
153
+
154
+ Emerald.Model.Attributes = {}
155
+ Emerald.Model.Attributes.initAttrAccessible = function(model, attrAccessible) {
156
+ model.attributes = function() { return false; }
157
+ if (!attrAccessible)
158
+ return false;
159
+
160
+ var attributes = attrAccessible;
161
+ for (attribute in attributes) {
162
+ var attributeName = attributes[attribute];
163
+
164
+ // model method
165
+ if (!model[attributeName])
166
+ model[attributeName] = "";
167
+
168
+ // attributes hash
169
+ model.attributes = function(attributeName) {
170
+ var attributes = Emerald.Model.Attributes.getAttributes(model);
171
+
172
+ if (attributeName)
173
+ return attributes[attributeName];
174
+ else
175
+ return attributes;
176
176
  }
177
177
  }
178
178
 
179
- var instance = new singleton().self(properties);
180
- createAccessibleAttributes(singleton, properties.attrAccessible);
181
- return instance;
179
+ return true;
180
+ }
181
+
182
+ Emerald.Model.Attributes.getAttributes = function(model) {
183
+ var attributesValues = {}
184
+ if (!model.attrAccessible)
185
+ return attributesValues;
186
+
187
+ var attributes = model.attrAccessible;
188
+ for (attribute in attributes) {
189
+ var attributeName = attributes[attribute];
190
+
191
+ // model method
192
+ attributesValues[attributeName] = model[attributeName];
193
+ }
194
+ return attributesValues;
182
195
  }
183
196
 
184
197
  // Observes the whole document for [data-observe] elements. Whenever an Ajax
@@ -200,93 +213,96 @@ function emeraldActiveModelInstance(properties){
200
213
  // The first HTML element will be updated with "My item" automatically and
201
214
  // the second, price, with "US$40,00".
202
215
  //
203
- function modelObserver(){}
204
-
205
- Emerald.modelObserver = new modelObserver();
206
-
207
- modelObserver.prototype.update = function(jsonData){
208
- $("[data-observe]").each(function(index){
209
- var observing = $(this).data("observe");
210
- var observedResources = observing.split(".");
211
-
212
- var currentValue = jsonData;
213
- $.each(observedResources, function(index, value){
214
- if (currentValue[value] || typeof currentValue[value] == "string")
216
+ Emerald.Model.Observer = {
217
+ update: function(jsonData){
218
+ $("[data-observe]").each(function(index){
219
+ var observing = $(this).data("observe");
220
+ var observedResources = observing.split(".");
221
+
222
+ var currentValue = jsonData;
223
+ $.each(observedResources, function(index, value){
224
+ if (currentValue[value] || typeof currentValue[value] == "string")
215
225
  currentValue = currentValue[value];
216
- else
226
+ else
217
227
  return false;
228
+ });
229
+
230
+ switch (typeof currentValue) {
231
+ case "number":
232
+ case "bool":
233
+ case "string":
234
+ $(this).html(currentValue);
235
+ return true;
236
+ }
237
+
218
238
  });
219
239
 
220
- switch (typeof currentValue) {
221
- case "number":
222
- case "bool":
223
- case "string":
224
- $(this).html(currentValue);
225
- return true;
226
- }
240
+ return true;
241
+ }
242
+ }
227
243
 
228
- });
229
- };
244
+ Emerald.Persistence = function(model, persistenceObject) {
245
+ var instance = new Object;
246
+
247
+ if (!persistenceObject)
248
+ persistenceObject = Emerald.Persistence;
249
+
250
+ instance.save = function(model, callbackController){
251
+ return persistenceObject.save(model, callbackController);
252
+ }
230
253
 
231
- function emeraldPersistence(model){
232
- var singleton = function() {
233
- this.initialize = function(model, _class) {
254
+ return instance;
255
+ }
256
+
257
+
258
+ Emerald.Persistence.save = function(model, callbackController) {
259
+ var instance = function() {
260
+ this.initialize = function(model) {
234
261
  this.model = model;
235
- this._class = _class;
236
262
  return this;
237
263
  }
238
264
 
239
- this.save = function(data, callbackController){
240
- return new this._class.PersistenceSave().save(data, callbackController);
241
- }
242
- }
265
+ this.save = function(model, controller) {
266
+ var attributes = this.model.attributes();
243
267
 
244
- this.PersistenceSave = function() {
245
- var singleton = function() {
246
- this.initialize = function(model, _class) {
247
- this.model = model;
248
- this._class = _class;
249
- return this;
250
- }
268
+ if (!attributes)
269
+ return false;
251
270
 
252
- this.save = function(data, controller) {
253
- //var attributes = this.model.attributes();
254
- var attributes = data;
255
- var _controller = controller;
256
-
257
- var requestSpecs = {
258
- url: this.model.route(),
259
- data: attributes,
260
- type: this.requestType(attributes),
261
- dataType: "json"
262
- };
263
-
264
- $.ajax(requestSpecs).done(function(JSON) {
265
- debugger;
266
- if (_controller)
267
- _controller.persistViewCallback(JSON);
268
- }).fail(function(response) {
269
- // TODO handle errors
270
- if (_controller)
271
- _controller.persistViewCallback(JSON);
272
- });
273
-
274
- return requestSpecs;
275
- }
271
+ var _controller = controller;
272
+ var requestSpecs = Emerald.Persistence.saveRequest(model);
276
273
 
277
- this.requestType = function(attributes) {
278
- if (attributes["id"])
279
- return "PUT";
280
- else
281
- return "POST";
282
- }
274
+ $.ajax(requestSpecs).done(function(jsonResponse) {
275
+ if (_controller)
276
+ _controller.persistViewCallback(jsonResponse);
277
+ }).fail(function(jsonResponse) {
278
+ if (_controller)
279
+ _controller.failedAjaxResponseCallback(jsonResponse);
280
+ });
281
+
282
+ return requestSpecs;
283
283
  }
284
284
 
285
- var instance = new singleton().initialize(model, this);
286
- return instance;
285
+ return this;
287
286
  }
288
287
 
289
- var instance = new singleton().initialize(model, this);
290
- return instance;
288
+ instance().initialize(model);
289
+ return instance().save(model, callbackController);
291
290
  }
292
291
 
292
+ Emerald.Persistence.saveRequest = function(model) {
293
+ var attributes = model.attributes();
294
+
295
+ return {
296
+ url: model.route,
297
+ data: attributes,
298
+ type: Emerald.Persistence.saveRequestType(attributes),
299
+ dataType: "json"
300
+ }
301
+ }
302
+
303
+ Emerald.Persistence.saveRequestType = function(attributes) {
304
+ if (attributes["id"])
305
+ return "PUT";
306
+ else
307
+ return "POST";
308
+ }
@@ -1,5 +1,5 @@
1
1
  module Emerald
2
2
  module Rails
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: emerald-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-21 00:00:00.000000000 Z
12
+ date: 2012-11-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: railties