Skip to content
This repository
  • 2 commits
  • 23 files changed
  • 0 comments
  • 1 contributor
4  lib/Action/handler/Config.js
@@ -12,8 +12,8 @@ function _cc(prop, value, target) {
12 12
             } else {
13 13
                 value = value.toUpperCase();
14 14
 
15  
-                if (!_.include(['GET', 'PUT', 'POST', 'DELETE'], value)) {
16  
-                    throw new Error('bad rest verb ' + value + 'passed to config of ' + target.name());
  15
+                if (!_.include(['GET', 'PUT', 'POST', 'DELETE', "*"], value)) {
  16
+                    throw new Error('bad rest verb ' + value + 'passed to config of ' + target.name);
17 17
                 }
18 18
                 target.method = value;
19 19
             }
85  lib/Action/index.js
@@ -10,6 +10,7 @@ var action_handler = require('./handler/Action');
10 10
 var Req_State = require('./../Req_State');
11 11
 var Loader = require('./../Loader');
12 12
 
  13
+var ensure_name = require('./../utility/ensure_name');
13 14
 function Action(config) {
14 15
     this.config = {};
15 16
     Loader.call(this);
@@ -18,16 +19,15 @@ function Action(config) {
18 19
     _.extend(this, config);
19 20
 
20 21
     var self = this;
21  
-    this.on('load_done', function(){
22  
-       self.on_load_done();
23  
-    });
  22
+    ensure_name(this, /^(action_)?(.*)$/);
24 23
 }
25 24
 
26 25
 _.extend(Action.prototype, Loader.prototype, {
27  
-    route: false,
  26
+    route:false,
28 27
     type:'',
29 28
     method:'get',
30 29
     CLASS:'ACTION',
  30
+    name:false,
31 31
 
32 32
     /* ********************* EVENTS ****************** */
33 33
 
@@ -37,54 +37,76 @@ _.extend(Action.prototype, Loader.prototype, {
37 37
     },
38 38
 
39 39
     extend:function (e) {
40  
-       // console.log('extending action with %s', util.inspect(e));
  40
+        // console.log('extending action with %s', util.inspect(e));
41 41
         _.extend(this, e);
42 42
     },
43 43
 
44  
-    on_load_done: function(){
45  
-        if (this.route === false){
  44
+    on_load_done:function () {
  45
+        if (this.route === false) {
46 46
             this._make_route();
47 47
         }
48 48
     },
49 49
 
50  
-    _make_route: function(){
51  
-        var route_path = [this.controller.name(true), this.name(true)];
52  
-        if(this.controller.parent.CLASS == 'COMPONENT'){
53  
-            route_path.unshift(this.controller.parent.name(true));
  50
+    _make_route:function () {
  51
+        var route_path = [this.controller.name, this.name];
  52
+        if (this.controller.parent.CLASS == 'COMPONENT') {
  53
+            route_path.unshift(this.controller.parent.name);
54 54
         }
55 55
         this.route = '/' + route_path.join('/');
56  
-        console.log('making route for %s: %s', this.name(), this.route);
  56
+        // console.log('making route for %s: %s', this.name, this.route);
57 57
     },
58 58
 
59 59
     /* ********************* SERVER ****************** */
60 60
 
61 61
     start_server:function (server, framework, cb) {
62  
-        this.digest_res(framework.get_resources());
  62
+        var self = this;
63 63
 
  64
+        this.digest_res(framework.get_resources());
64 65
         this.framework = framework;
65 66
 
66 67
         //@TODO: handle multiple requiest type
67 68
         var method = this.method.toLowerCase();
68  
-        var route = this.route;
  69
+        if (method == '*') {
  70
+            //console.log('MULTI METHOD!!!! %s', util.inspect(this));
  71
+            ['get', 'put', 'post', 'delete'].forEach(function (method) {
  72
+                if (self.config.hasOwnProperty(method + '_route')) {
  73
+                    var route = self.config[method + '_route'];
  74
+                    self._ss_route(server, method, route);
  75
+                }
  76
+            });
  77
+        } else {
  78
+            var route = this.config.hasOwnProperty(method + '_route') ? this.config[method + '_route'] : this.route;
  79
+            this._ss_route(server, method, route)
  80
+        }
  81
+
  82
+        framework.log('action %s added route %s', this.name, this.route);
  83
+
  84
+        cb();
  85
+    },
  86
+
  87
+    _ss_route:function (server, method, route) {
69 88
         var self = this;
70  
-     //   console.log('routing %s: %s', route, method);
  89
+        //console.log('routing %s: %s for %s', route, method, this.path);
  90
+        if (!method) {
  91
+            var msg = util.format('no method for action %s', this.path);
  92
+            console.log(msg);
  93
+            return  cb(new Error(msg));
  94
+        } else if (!route) {
  95
+            var msg = util.format('no route for action %s', this.path);
  96
+            route = this.heritage().replace(/:/g, '/').replace(/^app/, '');
  97
+            console.log(msg + ': forcing route to %s', route);
  98
+            //  return  cb( new Error(msg));
  99
+        }
71 100
         server[method](route, function (req, res) {
72 101
             return self.respond(req, res);
73 102
         });
74  
-
75  
-        framework.log('action %s added route %s', this.name(), this.route);
76  
-
77  
-        cb();
78 103
     },
79 104
 
80 105
     /* ********************** NAME ******************* */
81 106
 
82  
-    _name:false,
83  
-    name:function (short) {
84  
-        var base = this._name ? this._name : path.basename(this.path);
85  
-        return short ? base : '<<action>>' + base.replace('action_', '');
  107
+    heritage:function () {
  108
+        return this.controller.heritage() + ':' + this.name;
86 109
     },
87  
-    heritage:heritage,
88 110
 
89 111
     /* ******************** RESOURCES **************** */
90 112
 
@@ -98,7 +120,7 @@ _.extend(Action.prototype, Loader.prototype, {
98 120
     },
99 121
 
100 122
     respond:function (req, res) {
101  
-      //  console.log('responding: base');
  123
+        //  console.log('responding: base');
102 124
         var rs = new Req_State(req, res, this, this.controller, this.framework);
103 125
         this.validate(rs);
104 126
     },
@@ -136,8 +158,8 @@ _.extend(Action.prototype, Loader.prototype, {
136 158
     ],
137 159
 
138 160
     page_template_path:function () {
139  
-        //  console.log('finding templtae based on path %s, name ', this.path, this.name(true));
140  
-        return file_finder(this.path, this._ptp_props, this.name(true));
  161
+        //  console.log('finding templtae based on path %s, name ', this.path, this.name);
  162
+        return file_finder(this.path, this._ptp_props, this.name);
141 163
     },
142 164
 
143 165
     dyn_template:function (template_str, template_props) {
@@ -156,8 +178,8 @@ _.extend(Action.prototype, Loader.prototype, {
156 178
     },
157 179
 
158 180
     on_route:function (rs) {
159  
-        var method = rs.method().toUpperCase();
160  
-
  181
+        var method = rs.method().toLowerCase();
  182
+        console.log('routing %s', method);
161 183
         switch (method) {
162 184
             case 'get':
163 185
                 if (this.hasOwnProperty('on_get')) {
@@ -173,6 +195,7 @@ _.extend(Action.prototype, Loader.prototype, {
173 195
 
174 196
             case 'put':
175 197
                 if (this.hasOwnProperty('on_put')) {
  198
+                    console.log('putting');
176 199
                     return this.on_put(rs);
177 200
                 }
178 201
                 break;
@@ -190,7 +213,7 @@ _.extend(Action.prototype, Loader.prototype, {
190 213
     },
191 214
 
192 215
     on_respond:function (rs) {
193  
-     //   console.log('responding with %s', this.path);
  216
+        //   console.log('responding with %s', this.path);
194 217
         this.on_input(rs);
195 218
     },
196 219
 
@@ -204,7 +227,7 @@ _.extend(Action.prototype, Loader.prototype, {
204 227
 
205 228
     on_output:function (rs, output) {
206 229
         var template = this.page_template_path();
207  
-    //    console.log('action: %s, template: %s', this.name(), template);
  230
+        //    console.log('action: %s, template: %s', thisname, template);
208 231
         if (template) {
209 232
             rs.render(template, output);
210 233
         } else {
2  lib/Component/handler/Controller.js
@@ -10,7 +10,7 @@ module.exports = function () {
10 10
         type:'dir',
11 11
         name:'com_controller',
12 12
         execute:function (match_path, callback, target, match) {
13  
-            if (_DEBUG) console.log('PATH HANDLER >>COMPONENT<< Controller --- read %s into %s %s (%s)', match_path, target.CLASS, target.name(), target.path);
  13
+            if (_DEBUG) console.log('PATH HANDLER >>COMPONENT<< Controller --- read %s into %s %s (%s)', match_path, target.CLASS, targetname, target.path);
14 14
             this.owner.load_controller(match_path, callback);
15 15
         }
16 16
     });
11  lib/Component/index.js
@@ -14,6 +14,7 @@ var heritage = require('./../utility/heritage');
14 14
 var digest_config = require('./../utility/digest_config');
15 15
 var Gate = require('node-support/gate');
16 16
 var log = require('./../utility/log');
  17
+var ensure_name = require('./../utility/ensure_name');
17 18
 /**
18 19
  * A "Component" is essentially a base balss for the abstract Loader class, designed for the
19 20
  * MVC environment. Note that the Framework is itself a component; that is, a simple
@@ -32,6 +33,7 @@ function Component(config) {
32 33
     this._resources = [];
33 34
     _.extend(this,config);
34 35
     this._init_handlers();
  36
+    ensure_name(this, /^(com(ponent)?)?_(.*)/);
35 37
 }
36 38
 
37 39
 util.inherits(Component, Loader);
@@ -71,6 +73,7 @@ _.extend(Component.prototype, {
71 73
     load_controller:function (con_path, cb) {
72 74
         var con = new Controller({path:con_path, parent:this});
73 75
         this.controllers.push(con);
  76
+       // console.log('component: direct loading %s', con_path);
74 77
         con.start_load(cb, con_path);
75 78
     },
76 79
 
@@ -88,13 +91,9 @@ _.extend(Component.prototype, {
88 91
 
89 92
     /** ************* NAMES ********************* */
90 93
 
91  
-    name:function () {
92  
-        return  '<<component>>' + path.basename(this.path).replace(/^com(ponent)?_/, '');
93  
-    },
94  
-
95 94
     controller_names:function () {
96 95
         return _.map(this.controllers, function (con) {
97  
-            return con.name();
  96
+            return con.name;
98 97
         });
99 98
     },
100 99
 
@@ -132,7 +131,7 @@ _.extend(Component.prototype, {
132 131
            if (type && r.type !== type){
133 132
                return false;
134 133
            }
135  
-            if (name && r.name(true) != name){
  134
+            if (name && r.name != name){
136 135
                 return false;
137 136
             }
138 137
             return true;
35  lib/Controller/index.js
@@ -10,12 +10,15 @@ var ress_handler = require('./../utility/handler/resources');
10 10
 var heritage = require('./../utility/heritage');
11 11
 var digest_config = require('./../utility/digest_config');
12 12
 var Gate = require('node-support/gate');
  13
+var ensure_name = require('./../utility/ensure_name');
13 14
 
14 15
 function Controller(config) {
15 16
     this.actions = [];
16 17
     this._resources = [];
17 18
     digest_config(this, config, true);
18 19
     this._init_handlers();
  20
+    var self = this;
  21
+    ensure_name(this, /^con(troller)?_(.*)$/);
19 22
 }
20 23
 
21 24
 util.inherits(Controller, Loader);
@@ -34,6 +37,19 @@ _.extend(Controller.prototype, {
34 37
         this.add_handler(ress_handler());
35 38
     },
36 39
 
  40
+    on_load_done:function () {
  41
+        if (!this.name) {
  42
+            var name = path.basename(this.path, 'js');
  43
+            var m = /^con(troller)?_(.*)$/.exec(name);
  44
+            if (m) {
  45
+                name = m[m.length - 1];
  46
+            }
  47
+            this._name = name;
  48
+            console.log('defining name of controller %s', name);
  49
+            return name;
  50
+        }
  51
+    },
  52
+
37 53
     /* ***************** ACTIONS **************** */
38 54
 
39 55
     add_action:function (action_path, callback, target) {
@@ -55,14 +71,9 @@ _.extend(Controller.prototype, {
55 71
 
56 72
     /* ************* NAMES ************** */
57 73
 
58  
-    name:function (short) {
59  
-        var base = path.basename(this.path).replace(/^con(troller)?_/, '');
60  
-        return (short) ? base : '<<controller>>' + base;
61  
-    },
62  
-
63 74
     action_names:function () {
64 75
         return _.map(this.actions, function (action) {
65  
-            return action.name();
  76
+            return action.name;
66 77
         })
67 78
     },
68 79
 
@@ -79,15 +90,15 @@ _.extend(Controller.prototype, {
79 90
         if (!this._resources) {
80 91
             return [];
81 92
         }
82  
-        if ((!type) && (!name)){
  93
+        if ((!type) && (!name)) {
83 94
             return this._resources.slice(0);
84 95
         }
85 96
 
86  
-        return _.filter(this._resources, function(r){
87  
-           if (type && r.type !== type){
88  
-               return false;
89  
-           }
90  
-            if (name && r.name(true) != name){
  97
+        return _.filter(this._resources, function (r) {
  98
+            if (type && r.type !== type) {
  99
+                return false;
  100
+            }
  101
+            if (name && r.name != name) {
91 102
                 return false;
92 103
             }
93 104
             return true;
1  lib/Req_State/index.js
@@ -7,7 +7,6 @@ function Req_State(req, res, action, framework) {
7 7
     this.action = action;
8 8
     this.controller = action.controller;
9 9
     this.framework = framework ? framework : action.framework;
10  
-    this.framework = framework;
11 10
     this.req_props = {};
12 11
     this.read_req_props();
13 12
 }
42  lib/Resource/index.js
@@ -6,19 +6,39 @@ var util = require('util');
6 6
 var events = require('events');
7 7
 
8 8
 /**
9  
- * A Res is a catchall for a reusable bundle of functionality.
10  
- * View helpers, models and action helpers are all Ress.
  9
+ * Resource is ulitmately an interface: it must have a name and a type, and that combination must
  10
+ * be unique across the applciation. The name and type of a resouce deafaults to the values
  11
+ * set by convention. ie., resource/model/model_foo.js will by default be a model named foo.
11 12
  *
12  
- * Note - as a rule, all Res modules should be FACTORIES -
13  
- * that is, functions that return a Res. This is because
14  
- * some Ress (like models) are SINGLETONS - one object exists and
15  
- * is shared for all users. Others are unique per owner. The calling
16  
- * contexts shouldn't have to know which case is true for a given resouce.
  13
+ * Resources must be defined as either parameterless factory functions or naked objects.
17 14
  *
  15
+ * In the latter case, resources are created by extending a new Resource with said object.
  16
+ *
  17
+ * In the former case they can be - or be created by - any number of methods and may or may not be singletons.
  18
+ * They also are not absolutely required to extend from the Resource class, as long as they implement
  19
+ * name and type properties, and (if they are mixins or express_helpers) the apply method.
  20
+ *
  21
+ * In the case of factories, any configuration or individuation of a particular resource
  22
+ * must be done underneath the closure.
  23
+ *
  24
+ * A resource may optionally have an on_add(target, callback) method that responds to being
  25
+ * added to its parent. There is no guarantee regarding the ordering of resource loading at this point so
  26
+ * on_add should not depend in any way on resource load ordering. it could, for instance,
  27
+ * add an event listener for "load_done" on the target framework to execute an action after all
  28
+ * resources have loaded.
  29
+ *
  30
+ * Two other specialist resources are express_helpers and mixins.
  31
+ *
  32
+ * mixins have an apply method, apply(framework, callback) that is called after all resources have been loaded
  33
+ * but before the express_helpers have been apply()'d.
  34
+ *
  35
+ * express_helpers are specifically designed to alter or configure the express server
  36
+ * and must have an apply(server, framework, callback) method designed to facilitate this activity.
  37
+ * Again there is no guarantee as to which order this alteration will occur in, and its best to
  38
+ * put them all in app/resources to avoid overlapping component or controller alteration of the server.
18 39
  *
19 40
  */
20 41
 
21  
-
22 42
 function Res(config) {
23 43
    if (config){
24 44
        _.extend(this, config);
@@ -32,11 +52,7 @@ _.extend(Res.prototype, {
32 52
     heritage:heritage,
33 53
 
34 54
     type:'general',
35  
-    _name:'',
36  
-    name:function (short) {
37  
-        var base = this._name;
38  
-        return (short) ? base : '<<' + this.type + '>>' + base;
39  
-    }
  55
+    name: false
40 56
 })
41 57
 
42 58
 module.exports = Res;
9  lib/framework/index.js
@@ -162,10 +162,10 @@ _.extend(Framework.prototype, Component.prototype, {
162 162
     controller_names:function (include_components) {
163 163
         //  console.log('FW controllers: %s', this.controllers.length);
164 164
         this.components.forEach(function (com) {
165  
-            //   console.log('COM %s controllers: %s', com.name(), com.controllers.length);
  165
+            //   console.log('COM %s controllers: %s', comname, com.controllers.length);
166 166
         })
167 167
         var cn = _.map(this.controllers, function (c) {
168  
-            return c.name()
  168
+            return c.name
169 169
         });
170 170
 
171 171
         //  console.log('RCN of %s: [%s]', this.name(), cn.join(','));
@@ -184,10 +184,7 @@ _.extend(Framework.prototype, Component.prototype, {
184 184
         }));
185 185
     },
186 186
 
187  
-    name:function (simple) {
188  
-        var base = path.basename(this.path).replace(/^frame(work)?_/i, '');
189  
-        return simple ? base : '<<framework>>' + base;
190  
-    },
  187
+    name: 'app',
191 188
 
192 189
     controller_heritage:function (include_com) {
193 190
         var ch = _.map(this.controllers, function (c) {
2  lib/utility/digest_config.js
@@ -28,7 +28,7 @@ module.exports = function (target, config, context) {
28 28
                     break;
29 29
 
30 30
                 case 'name':
31  
-                    target['_name'] = config[b]; // note - stored in private var becuase most classes have a name function.
  31
+                    target['name'] = config[b]; // note - stored in private var becuase most classes have a name function.
32 32
                     break;
33 33
 
34 34
                 default:
4  lib/utility/digest_res.js
@@ -17,7 +17,7 @@ module.exports = function(res){
17 17
                     if (!self.hasOwnProperty('helpers')){
18 18
                         self.helpers = {};
19 19
                     }
20  
-                    self.helpers[item.name(true)] = item;
  20
+                    self.helpers[item.name] = item;
21 21
                 }
22 22
                 break;
23 23
 
@@ -26,7 +26,7 @@ module.exports = function(res){
26 26
                 if (!self.hasOwnProperty('models')){
27 27
                     self.models = {};
28 28
                 }
29  
-                self.models[item.name(true)] = item;
  29
+                self.models[item.name] = item;
30 30
 
31 31
                 break;
32 32
 
18  lib/utility/ensure_name.js
... ...
@@ -0,0 +1,18 @@
  1
+var path = require('path');
  2
+var util = require('util');
  3
+module.exports = function(target, re){
  4
+    if (!target.name){
  5
+        if (target.path){
  6
+            var name = path.basename(target.path, 'js');
  7
+            var match = re.exec(name)
  8
+            if (match){
  9
+                name = match[match.length - 1];
  10
+            } else {
  11
+                console.log('cannot find %s in %s', re.toString(), name);
  12
+            }
  13
+            target.name = name;
  14
+        } else {
  15
+            throw new Error('target for intrepret_name has neither name NOR path');
  16
+        }
  17
+    }
  18
+}
6  lib/utility/handler/Resources.js
@@ -14,7 +14,7 @@ function _make_resource(res, type, name) {
14 14
         res.type = type;
15 15
     }
16 16
     if (name && !res.name) {
17  
-        res._name = name;
  17
+        res.name = name;
18 18
     }
19 19
     var r = new Resource(res);
20 20
     return r;
@@ -92,9 +92,9 @@ module.exports = function () {
92 92
                             if (type){
93 93
                                 new_res.type = type;
94 94
                             }
95  
-                            if (!new_res._name){
  95
+                            if (!new_res.name){
96 96
                                 name = name.replace(new RegExp('(_)?' + type + '(_)?'), '');
97  
-                                new_res._name = name;
  97
+                                new_res.name = name;
98 98
                             }
99 99
 
100 100
                       //      console.log(' !!!!!!!!!! ADDING %s %s to %s', new_res.type, new_res.name(), target.name());
28  lib/utility/heritage.js
@@ -2,29 +2,27 @@ var _ = require('underscore');
2 2
 
3 3
 var H_SEP = ':';
4 4
 
5  
-module.exports = function(){
6  
-    var out = [];
  5
+module.exports = function () {
  6
+    try {
7 7
 
8  
-    if (this.name){
9  
-        if (_.isFunction(this.name)){
10  
-            out.push(this.name());
11  
-        } else {
  8
+        var out = [];
  9
+
  10
+        if (this.name) {
12 11
             out.push(this.name);
13 12
         }
14  
-    }
15 13
 
16  
-    if (this.parent){
17  
-        if (this.parent.heritage){
18  
-            out.unshift(this.parent.heritage());
19  
-        } else if (this.parent.name){
20  
-            if (_.isFunction(this.parent.name)){
21  
-                out.unshift(this.parent.name())
22  
-            } else {
  14
+        if (this.parent) {
  15
+            if (this.parent.heritage) {
  16
+                out.unshift(this.parent.heritage());
  17
+            } else if (this.parent.name) {
23 18
                 out.unshift(this.parent.name);
24 19
             }
25 20
         }
  21
+        return out.join(H_SEP);
  22
+    } catch(err){
  23
+        console.log('error in heritagge: %s', err.toString());
  24
+        return '???';
26 25
     }
27 26
 
28  
-    return out.join(H_SEP);
29 27
 
30 28
 }
162  test_resources/REST_test/app/con_folks/actions/action_folks/folks_action.js
... ...
@@ -0,0 +1,162 @@
  1
+var util = require('util');
  2
+
  3
+module.exports = {
  4
+
  5
+    on_get:function (rs) {
  6
+        this.get_validate(rs);
  7
+    },
  8
+
  9
+    get_validate:function (rs) {
  10
+        if (rs.req_props.id) {
  11
+            this.get_process(rs);
  12
+        } else {
  13
+            this.rest_err(rs, "get folks called with no ID");
  14
+        }
  15
+    },
  16
+
  17
+    get_process:function (rs) {
  18
+        var self = this;
  19
+        // console.log('getting %s', rs.req_props.id);
  20
+        this.models.folks.get(rs.req_props.id, function (err, folk) {
  21
+            //  console.log('folk gotten: %s', util.inspect(folk));
  22
+            if (err) {
  23
+                self.rest_err(rs, 'cannot get folk id %s: %s', rs.req_props.id, err.toString());
  24
+            } else if (folk) {
  25
+                self.on_output(rs, folk.toObject());
  26
+            } else {
  27
+                self.rest_err(rs, 'cannot find folk id %s', rs.req_props.id);
  28
+            }
  29
+        });
  30
+
  31
+    },
  32
+
  33
+    on_post:function (rs) {
  34
+        this.post_validate(rs);
  35
+    },
  36
+
  37
+    post_validate:function (rs) {
  38
+        var self = this;
  39
+        delete rs.req_props.id;
  40
+        var trial_model = new this.models.folks.model(rs.req_props);
  41
+        trial_model.validate(function (errs) {
  42
+
  43
+            if (errs) {
  44
+                return self.rest_err(rs, errs);
  45
+            } else {
  46
+                self.post_process(rs);
  47
+            }
  48
+        });
  49
+    },
  50
+
  51
+    post_process:function (rs) {
  52
+        var self = this;
  53
+
  54
+        this.models.folks.put(rs.req_props, function (err, folk) {
  55
+            //    console.log('have put %s -- %s', util.inspect(err), util.inspect(folk));
  56
+            if (err) {
  57
+                self.rest_err(rs, 'cannot put folk %s: %s', JSON.stringify(rs.req_props), err.toString());
  58
+            } else if (folk) {
  59
+                self.on_output(rs, folk.toObject());
  60
+            } else {
  61
+                self.rest_err(rs, 'cannot put folk %s', JSON.stringify(rs.req_props));
  62
+            }
  63
+
  64
+        });
  65
+    },
  66
+
  67
+    on_put:function (rs) {
  68
+        this.put_validate(rs);
  69
+    },
  70
+
  71
+    put_validate:function (rs) {
  72
+        var id = rs.req_props.id;
  73
+        delete rs.req_props.id;
  74
+        /**
  75
+         * note - unlike post, were we are definitively given a complete record,
  76
+         * we are not assuming that the data is complete or valid - an additive model.
  77
+         * So we are not fully validating the input here, just the existince of
  78
+         * a previous model.
  79
+         */
  80
+
  81
+        if (!id) {
  82
+            return this.rest_err(rs, 'cannot put to id %s', id);
  83
+        }
  84
+
  85
+        this.models.folks.count({_id:id}, function (err, num) {
  86
+            if (err) {
  87
+                return this.rest_err(rs, err);
  88
+            } else {
  89
+                switch (num) {
  90
+                    case 0:
  91
+                        return this.rest_err(rs, 'cannot put to id %s - record not found', id);
  92
+                        break;
  93
+
  94
+                    case 1:
  95
+                        return this.put_process(rs, {id:id});
  96
+                        break;
  97
+
  98
+                    default:
  99
+                    //@TODO: err!
  100
+                }
  101
+            }
  102
+        });
  103
+    },
  104
+
  105
+    put_process:function (rs, input) {
  106
+        var self = this;
  107
+        this.models.folks.get(input.id, function (err, old_folk) {
  108
+            if (err) {
  109
+                return self.rest_err(rs, 'err getting %s: %s', input.id, err.toString());
  110
+            } else {
  111
+                for (var f in rs.req_props) {
  112
+                    old_folk[f] = rs.req_props[f];
  113
+                }
  114
+
  115
+                old_folk.save(function (err, saved_folk) {
  116
+                    if (err) {
  117
+                        return self.rest_err(rs, 'err updating (put) %s: %s', input.id, err.toString());
  118
+                    } else {
  119
+                        self.on_output(rs, saved_folk.toObject());
  120
+                    }
  121
+                });
  122
+            }
  123
+
  124
+        });
  125
+
  126
+    },
  127
+
  128
+    on_delete:function (rs) {
  129
+        var self = this;
  130
+        if (rs.req_props.id) {
  131
+            self.model.folks.get(rs.req_props.id, function (err, folk) {
  132
+                if (err) {
  133
+                    self.rest_err(rs, 'cannot get folk id %s: %s', rs.req_props.id, err.toString());
  134
+                } else if (folk) {
  135
+                    self.delete_process(rs, folk);
  136
+                } else {
  137
+                    self.rest_err(rs, 'cannot find folk id %s', rs.req_props.id);
  138
+                }
  139
+            });
  140
+        } else {
  141
+            this.rest_err(rs, "get folks called with no ID");
  142
+        }
  143
+    },
  144
+
  145
+    delete_process:function (rs, folk) {
  146
+        var self = this;
  147
+        folk.remove(function (err, deleted_folk) {
  148
+            self.on_output(rs, deleted_folk.toObject());
  149
+        });
  150
+    },
  151
+
  152
+    rest_err:function (rs, err) {
  153
+        if (err instanceof Error) {
  154
+            rs.send({error:err.toString() }, 400);
  155
+        } else {
  156
+
  157
+            var args = [].slice.call(arguments, 0);
  158
+            args.shift();
  159
+            rs.send({error:util.format.apply(util, args)}, 400);
  160
+        }
  161
+    }
  162
+}
7  test_resources/REST_test/app/con_folks/actions/action_folks/folks_config.json
... ...
@@ -0,0 +1,7 @@
  1
+{
  2
+    "method": "*",
  3
+    "get_route": "/folks/:id",
  4
+    "put_route": "/folks",
  5
+    "post_route": "/folks/:id",
  6
+    "delete_route": "/folks/:id"
  7
+}
3  test_resources/REST_test/app/frame_config.json
... ...
@@ -0,0 +1,3 @@
  1
+{
  2
+    "port": 3335
  3
+}
9  test_resources/REST_test/app/resources/express_helper/express_body_parser.js
... ...
@@ -0,0 +1,9 @@
  1
+var express = require('express');
  2
+
  3
+module.exports = {
  4
+    apply: function(server, target, cb){
  5
+       // console.log('applying loggeg');
  6
+        server.use(express.bodyParser());
  7
+        cb();
  8
+    }
  9
+}
14  test_resources/REST_test/app/resources/express_helper/express_view.js
... ...
@@ -0,0 +1,14 @@
  1
+var ejs = require('ejs');
  2
+
  3
+
  4
+module.exports = {
  5
+    apply: function(server, target, cb){
  6
+       // console.log('applying loggeg');
  7
+        server.set('view_engine', 'ejs');
  8
+        server.set('view options', {
  9
+          layout: false
  10
+        });
  11
+        server.register('.html', ejs);
  12
+        cb();
  13
+    }
  14
+}
13  test_resources/REST_test/app/resources/express_helper/logger.js
... ...
@@ -0,0 +1,13 @@
  1
+var Express = require('express');
  2
+var util = require('util');
  3
+var _ = require('underscore');
  4
+
  5
+
  6
+module.exports = {
  7
+    apply: function(server, target, cb){
  8
+       // console.log('applying loggeg');
  9
+        server.use(Express.logger());
  10
+        cb();
  11
+    }
  12
+
  13
+}
29  test_resources/REST_test/app/resources/model/model_folks.js
... ...
@@ -0,0 +1,29 @@
  1
+var Mongoose_Model = require("node-support/mongoose_model");
  2
+var NE = require('./../../../../../lib');
  3
+var util = require('util');
  4
+var _ = require('underscore');
  5
+var mongoose = require('mongoose');
  6
+function Mongoose_Model_Resource(model, config) {
  7
+    Mongoose_Model.MongooseModel.call(this, model, config);
  8
+}
  9
+
  10
+util.inherits(Mongoose_Model_Resource, Mongoose_Model.MongooseModel);
  11
+_.extend(Mongoose_Model_Resource.prototype, NE.Resource.prototype);
  12
+
  13
+var _model;
  14
+//console.log('SMR prototype: %s, model: %s', util.inspect(Simple_Model_Resource.prototype), util.inspect(Simple_Model_Resource));
  15
+module.exports = function () {
  16
+    if (!_model) {
  17
+        var schema = new mongoose.Schema({
  18
+            name:String,
  19
+            notes:String,
  20
+            birthday:Date,
  21
+            gender: Number // 1 = male, -1 = female, 0/other == ??
  22
+        });
  23
+
  24
+        var model = mongoose.model('Folks', schema);
  25
+
  26
+        _model = new Mongoose_Model_Resource(model, {});
  27
+    }
  28
+    return _model;
  29
+}
58  tests/Component_Controller_test.js
@@ -29,55 +29,53 @@ var fr_con_her;
29 29
 module.exports = {
30 30
     setup:function (test) {
31 31
         framework_com_con = _ss([
32  
-            '<<controller>>alpha',
33  
-            '<<controller>>beta',
34  
-            '<<controller>>gamma',
35  
-            '<<controller>>alpha2',
36  
-            '<<controller>>beta2'
  32
+            'alpha',
  33
+            'beta',
  34
+            'gamma',
  35
+            'alpha2',
  36
+            'beta2'
37 37
         ]);
38  
-        framework_con_direct = _ss(['<<controller>>direct']);
  38
+        framework_con_direct = _ss(['direct']);
39 39
         framework_con_all = _ss(framework_com_con.concat(framework_con_direct));
40 40
         com_controllers = _ss([
41  
-            '<<controller>>alpha',
42  
-            '<<controller>>beta',
43  
-            '<<controller>>gamma'
  41
+            'alpha',
  42
+            'beta',
  43
+            'gamma'
44 44
         ]);
45  
-        controller_actions = _ss(['<<action>>bar', '<<action>>foo']);
  45
+        controller_actions = _ss(['bar', 'foo']);
46 46
         controller_loader_actions = _ss([
47  
-            '<<controller>>alpha:<<action>>bar',
48  
-            '<<controller>>alpha:<<action>>foo'
  47
+            'alpha:bar',
  48
+            'alpha:foo'
49 49
         ]);
50 50
         fr_con_her = _ss([
51  
-            '<<framework>>app:<<component>>bar:<<controller>>alpha',
52  
-            '<<framework>>app:<<component>>bar:<<controller>>beta',
53  
-            '<<framework>>app:<<component>>bar:<<controller>>gamma',
54  
-            '<<framework>>app:<<component>>foo:<<controller>>alpha2',
55  
-            '<<framework>>app:<<component>>foo:<<controller>>beta2',
56  
-            '<<framework>>app:<<controller>>direct'
  51
+            'app:bar:alpha',
  52
+            'app:bar:beta',
  53
+            'app:bar:gamma',
  54
+            'app:foo:alpha2',
  55
+            'app:foo:beta2',
  56
+            'app:direct'
57 57
         ]);
58 58
 
59 59
         component = new Component({path:component_path});
60 60
         controller = new Controller({path:controller_path});
61 61
         console.log('CCT setup done');
62  
-        test.done();
63  
-    },
64  
-
65  
-    test_component_loader:function (test) {
66  
-        //console.log(' -- TEST COMPONENT LOADER --');
67  
-
68 62
         component.start_load(function () {
69  
-            test.equal(component._item_count, 0, 'Item count is zero');
70  
-            test.deepEqual({com_bar_foo:3, com_bar_vey:2}, component.config, 'loading component configuration');
71  
-            test.deepEqual(com_controllers, _ss(component.controller_names()), 'controllers found');
72  
-            test.equal(component.heritage(), '<<component>>bar', 'component heritage');
73 63
             test.done();
74 64
         }, component_path);
75 65
     },
76 66
 
  67
+    test_component_loader:function (test) {
  68
+        test.equal(component._item_count, 0, 'Item count is zero');
  69
+        test.deepEqual({com_bar_foo:3, com_bar_vey:2}, component.config, 'loading component configuration');
  70
+        test.deepEqual(com_controllers, _ss(component.controller_names()), 'controllers found');
  71
+        test.equal(component.heritage(), 'bar', 'component heritage');
  72
+        test.done();
  73
+    },
  74
+
77 75
     test_controller_loader:function (test) {
78 76
         controller.start_load(function () {
79 77
             test.deepEqual(controller_actions, _ss(controller.action_names()), 'controller action nanes');
80  
-            test.equal(controller.heritage(), '<<controller>>alpha', 'controller heritage');
  78
+            test.equal(controller.heritage(), 'alpha', 'controller heritage');
81 79
             var action_h = _ss(_.map(controller.actions, function (a) {
82 80
                 return a.heritage();
83 81
             }));
@@ -93,7 +91,7 @@ module.exports = {
93 91
         fr.start_load(function () {
94 92
 
95 93
             test.equal(fr._item_count, 0, 'Item count is zero');
96  
-            test.deepEqual({port: 80, framework_foo:1, framework_bar:2 }, fr.config,
  94
+            test.deepEqual({port:80, framework_foo:1, framework_bar:2 }, fr.config,
97 95
                 util.format('loading framework configuration of framework %s', fr.path));
98 96
             test.deepEqual(framework_com_con,
99 97
                 _ss(fr.com_controller_names()), 'component controllers found');
4  tests/Framework_Server_test.js
@@ -22,8 +22,8 @@ module.exports = {
22 22
     setup: function(test){
23 23
         framework = new Framework({path: app_path});
24 24
 
25  
-        fw_log_msgs = _ss(['action <<action>>bar added route /bar/alpha/bar',
26  
-        'action <<action>>foo added route /bar/alpha/foo']);
  25
+        fw_log_msgs = _ss(['action bar added route /bar/alpha/bar',
  26
+        'action foo added route /bar/alpha/foo']);
27 27
 
28 28
         framework.start_load(function(){
29 29
         //    console.log('FS setup done');
92  tests/REST_test.js
... ...
@@ -0,0 +1,92 @@
  1
+var Framework = require('../lib/Framework');
  2
+var path = require('path');
  3
+var _ = require('underscore');
  4
+var util = require('util');
  5
+var request = require('request');
  6
+var fs = require('fs');
  7
+var path = require('path');
  8
+var mongoose = require('mongoose');
  9
+
  10
+mongoose.connect('mongodb://localhost/mmt');
  11
+
  12
+var framework;
  13
+
  14
+var app_path = path.resolve(__dirname + '/../test_resources/REST_test/app');
  15
+var root = 'http://localhost:3335/';
  16
+
  17
+function _ss(a) {
  18
+    return _.sortBy(a, function (i) {
  19
+        return i
  20
+    });
  21
+}
  22
+
  23
+function _1(a) {
  24
+    return _.map(a, function (aa) {
  25
+        return aa[0]
  26
+    });
  27
+}
  28
+
  29
+var Roger;
  30
+
  31
+module.exports = {
  32
+
  33
+    setup:function (test) {
  34
+        framework = new Framework({path:app_path});
  35
+        Roger = {name:"Roger", notes:"three cheese blend"};
  36
+        framework.start_load(function () {
  37
+            test.done();
  38
+        }, app_path);
  39
+    },
  40
+
  41
+    test_server_load:function (test) {
  42
+        framework.on('load_done', function () {
  43
+            test.ok(!(framework.hasOwnProperty('abc')),
  44
+                'framework doesn\'t have alpha on load done');
  45
+        });
  46
+
  47
+        framework.start_server(function () {
  48
+            test.equal(framework.config.port, 3335, 'port is 3335');
  49
+            framework.server().listen(framework.config.port);
  50
+            console.log('server listening at port %s', framework.config.port);
  51
+            test.done();
  52
+        })
  53
+    },
  54
+
  55
+    test_prop_response:function (test) {
  56
+        request.post({uri:root + 'folks', form:Roger },
  57
+            function (err, res, put_body) {
  58
+                var Roger_JSON = JSON.parse(put_body);
  59
+                test.ok(Roger_JSON._id, 'return json has ID');
  60
+                var id = Roger_JSON._id;
  61
+                delete Roger_JSON._id;
  62
+                test.deepEqual(Roger, Roger_JSON, 'Rogering');
  63
+
  64
+                /* *********** RETRIEVE ROGER **************** */
  65
+
  66
+                process.nextTick(function(){
  67
+                    request.get(root + 'folks/' + id, function(err, rest, get_body){
  68
+                        test.equal(put_body, get_body, 'Can retrieve Roger');
  69
+
  70
+                        /* *********** CALL HIM ROD ************** */
  71
+
  72
+                        request.post({uri: root + 'folks/' + id, form: {name: "Rod"}}, function(err, r, updated_body){
  73
+
  74
+                            var altered_rod = JSON.parse(updated_body);
  75
+                            test.equal(altered_rod.name, 'Rod', 'name is Rod');
  76
+                            test.equal(altered_rod.noted, Roger.notes, 'same old notes');
  77
+                            test.done();
  78
+
  79
+                        })
  80
+
  81
+                    });
  82
+
  83
+                })
  84
+
  85
+            });
  86
+    },
  87
+
  88
+    test_done_response:function (test) {
  89
+        framework.server().close();
  90
+        test.done();
  91
+    }
  92
+}

No commit comments for this range

Something went wrong with that request. Please try again.