Skip to content
This repository
Browse code

[core, resource] getters/setters API

  • Loading branch information...
commit 33a3675fbada4861a52d603be55ebf9ad084082c 1 parent 8da8cef
Fedor Indutny authored
10  lib/resourceful/core.js
@@ -183,20 +183,20 @@ resourceful.defineProperty = function (obj, property, schema) {
183 183
 
184 184
   // Sanitize defaults and per-creation properties
185 185
   if (schema.sanitize) {
186  
-    var val = obj.readProperty(property);
  186
+    var val = obj.readProperty(property, schema.get);
187 187
     if (val !== undefined) {
188  
-      obj.writeProperty(property, schema.sanitize(val));
  188
+      obj.writeProperty(property, schema.sanitize(val), schema.set);
189 189
     }
190 190
   }
191 191
 
192 192
   Object.defineProperty(obj, property, {
193 193
     get: function () {
194  
-      return this.readProperty(property);
  194
+      return this.readProperty(property, schema.get);
195 195
     },
196 196
     set: schema.sanitize ? function (val) {
197  
-      return this.writeProperty(property, schema.sanitize(val));
  197
+      return this.writeProperty(property, schema.sanitize(val), schema.set);
198 198
     } : function (val) {
199  
-      return this.writeProperty(property, val);
  199
+      return this.writeProperty(property, val, schema.set);
200 200
     },
201 201
     enumerable: true
202 202
   });
11  lib/resourceful/resource.js
@@ -512,12 +512,15 @@ Resource.prototype.saveAttachment = function (name, data, callback) {
512 512
 
513 513
 Resource.prototype.destroy = function () {};
514 514
 Resource.prototype.reload = function () {};
515  
-Resource.prototype.readProperty = function (k) {
516  
-  return this._properties[k];
  515
+Resource.prototype.readProperty = function (k, getter) {
  516
+  return getter ? getter.call(this, this._properties[k]) : this._properties[k];
517 517
 };
518 518
 
519  
-Resource.prototype.writeProperty = function (k, val) {
520  
-  return this._properties[k] = val;
  519
+Resource.prototype.writeProperty = function (k, val, setter) {
  520
+  return this._properties[k] = setter ?
  521
+      setter.call(this, val)
  522
+      :
  523
+      val;
521 524
 };
522 525
 
523 526
 

0 notes on commit 33a3675

Please sign in to comment.
Something went wrong with that request. Please try again.