Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added fn support

  • Loading branch information...
commit 21ac11c8cc977bc65200f2c8d5ce07c00a2bb617 1 parent a84082d
Martin Wawrusch authored January 04, 2013
13  README.md
Source Rendered
@@ -31,6 +31,15 @@ where apiFacade is the instantiated apiFacade object (which contains all the sch
31 31
 Resonable documentation will be written when I have a bit more time.
32 32
 
33 33
 ## Release Notes
  34
+### 0.2.2
  35
+* Added support for fn mapping. No docs or tests yet, but works like this:
  36
+
  37
+```coffeescript
  38
+    mappings:
  39
+      url: 
  40
+        fn: (self,options) -> "#{options.baseUrl}/products/#{self._id}"
  41
+```
  42
+Using self for clarity. Thought it might be easier then mapping this.
34 43
 
35 44
 ### 0.2.1
36 45
 * Added support for default scope if none is present. E.g. define a default scope, which is used if options.scopes is null or contains nothing.
@@ -47,8 +56,8 @@ Resonable documentation will be written when I have a bit more time.
47 56
 
48 57
 * Change version in package.json
49 58
 git add . -A
50  
-git commit -m "Upgrading to v0.2.1"
51  
-git tag -a v0.2.1 -m 'version 0.2.1'
  59
+git commit -m "Upgrading to v0.2.2"
  60
+git tag -a v0.2.2 -m 'version 0.2.2'
52 61
 git push --tags
53 62
 npm publish
54 63
 
13  lib/api-facade-client.js
@@ -21,6 +21,8 @@
21 21
 
22 22
       this._handleSingleMapping = __bind(this._handleSingleMapping, this);
23 23
 
  24
+      this._invokeFnMapping = __bind(this._invokeFnMapping, this);
  25
+
24 26
       this._isCollectionTypeListOrArray = __bind(this._isCollectionTypeListOrArray, this);
25 27
 
26 28
       this._isCollectionTypeArray = __bind(this._isCollectionTypeArray, this);
@@ -176,6 +178,13 @@
176 178
       return this._isCollectionTypeList(collectionType) || this._isCollectionTypeArray(collectionType);
177 179
     };
178 180
 
  181
+    ApiFacadeClient.prototype._invokeFnMapping = function(fn, source, options) {
  182
+      if (options == null) {
  183
+        options = {};
  184
+      }
  185
+      return fn(source, options);
  186
+    };
  187
+
179 188
     ApiFacadeClient.prototype._handleSingleMapping = function(targetKey, mappingTarget, source, result, resolver, options) {
180 189
       var idField, v,
181 190
         _this = this;
@@ -186,6 +195,10 @@
186 195
         return result[targetKey] = source[mappingTarget];
187 196
       } else if (_.isObject(mappingTarget)) {
188 197
         v = source[mappingTarget.name];
  198
+        if (mappingTarget.fn && _.isFunction(mappingTarget.fn)) {
  199
+          result[targetKey] = this._invokeFnMapping(mappingTarget.fn, source, options);
  200
+          return;
  201
+        }
189 202
         if (v) {
190 203
           if (mappingTarget.type) {
191 204
             if (mappingTarget.resolve) {
2  package.json
@@ -2,7 +2,7 @@
2 2
   "author": "Martin Wawrusch <martin@wawrusch.com> (http://martinatsunset.com)",
3 3
   "name": "api-facade",
4 4
   "description": "A library that simplifies the exposure of data through REST interfaces in a secure, scope dependent way. Basically transforms internal data into whatever a client of your API has the right to see.",
5  
-  "version": "0.2.1",
  5
+  "version": "0.2.2",
6 6
   "main" : "lib/index.js",
7 7
   "repository": {
8 8
     "type": "git",
7  src/api-facade-client.coffee
@@ -80,6 +80,8 @@ module.exports = class ApiFacadeClient
80 80
   _isCollectionTypeListOrArray: (collectionType) =>
81 81
     @_isCollectionTypeList(collectionType) || @_isCollectionTypeArray(collectionType)
82 82
 
  83
+  _invokeFnMapping: (fn,source,options = {}) =>
  84
+    fn(source,options)
83 85
 
84 86
   _handleSingleMapping: (targetKey,mappingTarget,source,result,resolver,options = {}) =>
85 87
       if _.isString(mappingTarget)
@@ -87,6 +89,11 @@ module.exports = class ApiFacadeClient
87 89
       else if _.isObject(mappingTarget)
88 90
         v = source[mappingTarget.name]
89 91
 
  92
+        if mappingTarget.fn && _.isFunction(mappingTarget.fn)
  93
+          result[targetKey] = @_invokeFnMapping(mappingTarget.fn,source,options)
  94
+          return
  95
+
  96
+
90 97
         if v
91 98
           if mappingTarget.type
92 99
 

0 notes on commit 21ac11c

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