Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' into model-adapters

  • Loading branch information...
commit fda037265887bc139c8e511d6220dcd2f1379738 2 parents 8297ba3 + aab28ee
@Techwraith Techwraith authored
View
2  Jakefile
@@ -4,7 +4,7 @@ var fs = require('fs')
, exec = child_process.exec
, inflection = require('./deps/inflection')
, utils = require('./lib/utils')
- , ejs = require('./lib/template/adapters/ejs/ejs')
+ , ejs = require('./lib/template/adapters/ejs/template')
, createPackageTask;
var JSPAT = /\.js$/;
View
189 README.md
@@ -62,15 +62,17 @@ Routes are similar to Merb or Rails routes.
**Basic routes**
- router.match('/moving/pictures/:id').to(
- {controller: 'Moving', action: 'pictures'});
+```javascript
+router.match('/moving/pictures/:id').to(
+ {controller: 'Moving', action: 'pictures'});
- router.match('/farewells/:farewelltype/kings/:kingid').to(
- {controller: 'Farewells', action: 'kings'});
+router.match('/farewells/:farewelltype/kings/:kingid').to(
+ {controller: 'Farewells', action: 'kings'});
- //Can also match specific HTTP methods only
- router.match('/xandadu', 'get').to(
- {controller: 'Xandadu', action: 'specialHandler'});
+//Can also match specific HTTP methods only
+router.match('/xandadu', 'get').to(
+ {controller: 'Xandadu', action: 'specialHandler'});
+```
**Resource-based routes**
@@ -168,37 +170,38 @@ DELETE */snow_dogs/:id[.extension]<br/>
A simple controller that just responds with any
form-post/query-string params looks like this:
- var SnowDogs = function () {
- this.respondsWith = ['text', 'json', 'html'];
+```javascript
+var SnowDogs = function () {
+ this.respondsWith = ['text', 'json', 'html'];
- this.index = function (params) {
- this.respond({params: params});
- };
+ this.index = function (params) {
+ this.respond({params: params});
+ };
- this.add = function (params) {
- this.respond({params: params});
- };
+ this.add = function (params) {
+ this.respond({params: params});
+ };
- this.create = function (params) {
- this.respond({params: params});
- };
+ this.create = function (params) {
+ this.respond({params: params});
+ };
- this.show = function (params) {
- this.respond({params: params});
- };
+ this.show = function (params) {
+ this.respond({params: params});
+ };
- this.update = function (params) {
- this.respond({params: params});
- };
+ this.update = function (params) {
+ this.respond({params: params});
+ };
- this.remove = function (params) {
- this.respond({params: params});
- };
+ this.remove = function (params) {
+ this.respond({params: params});
+ };
- };
-
- exports.SnowDogs = SnowDogs;
+};
+exports.SnowDogs = SnowDogs;
+```
## Content-negotiation
@@ -209,13 +212,15 @@ If you have a JSON-serializable JavaScript object you want to
return in JSON format, pass your JavaScript object to the
`respond` method in the action on that controller.
- this.respondsWith = ['text', 'json'];
+```javascript
+this.respondsWith = ['text', 'json'];
- this.show = function (params) {
- // (Fetch some item by params.id)
- item = {foo: 'FOO', bar: 1, baz: false};
- this.respond(item);
- };
+this.show = function (params) {
+ // (Fetch some item by params.id)
+ item = {foo: 'FOO', bar: 1, baz: false};
+ this.respond(item);
+};
+```
## Models and validations
@@ -225,54 +230,60 @@ Ruby's ActiveRecord or DataMapper.
Here is an example of a model with some validations:
- var User = function () {
- this.property('login', 'string', {required: true});
- this.property('password', 'string', {required: true});
- this.property('lastName', 'string');
- this.property('firstName', 'string');
-
- this.validatesPresent('login');
- this.validatesFormat('login', /[a-z]+/, {message: 'Subdivisions!'});
- this.validatesLength('login', {min: 3});
- this.validatesConfirmed('password', 'confirmPassword');
- this.validatesWithFunction('password', function (s) {
- // Something that returns true or false
- return s.length > 0;
- });
-
- // Can define methods for instances like this
- this.someMethod = function () {
- // Do some stuff
- };
- };
-
- // Can also define them on the prototype
- User.prototype.someOtherMethod = function () {
- // Do some other stuff
- };
-
- User = geddy.model.registerModel('User', User);
+```javascript
+var User = function () {
+ this.property('login', 'string', {required: true});
+ this.property('password', 'string', {required: true});
+ this.property('lastName', 'string');
+ this.property('firstName', 'string');
+
+ this.validatesPresent('login');
+ this.validatesFormat('login', /[a-z]+/, {message: 'Subdivisions!'});
+ this.validatesLength('login', {min: 3});
+ this.validatesConfirmed('password', 'confirmPassword');
+ this.validatesWithFunction('password', function (s) {
+ // Something that returns true or false
+ return s.length > 0;
+ });
+
+ // Can define methods for instances like this
+ this.someMethod = function () {
+ // Do some stuff
+ };
+};
+
+// Can also define them on the prototype
+User.prototype.someOtherMethod = function () {
+ // Do some other stuff
+};
+
+User = geddy.model.registerModel('User', User);
+```
Alternatively, you can use the `defineProperties` method to lay out your model:
- var User = function () {
- this.defineProperties({
- login: {type: 'string', required: true}
- , password: {type: 'string', required: true}
- , lastName: {type: 'string'}
- , firstName: {type: 'string'}
- });
- }
+```javascript
+var User = function () {
+ this.defineProperties({
+ login: {type: 'string', required: true}
+ , password: {type: 'string', required: true}
+ , lastName: {type: 'string'}
+ , firstName: {type: 'string'}
+ });
+}
+```
Creating an instance of one of these models is easy:
- var params = {
- login: 'alex'
- , password: 'lerxst'
- , lastName: 'Lifeson'
- , firstName: 'Alex'
- };
- var user = User.create(params);
+```javascript
+var params = {
+ login: 'alex'
+, password: 'lerxst'
+, lastName: 'Lifeson'
+, firstName: 'Alex'
+};
+var user = User.create(params);
+```
Data-validation happens on the call to `create`, and any
validation errors show up inside an `errors` property on
@@ -280,16 +291,18 @@ the instance, keyed by field name. Instances have a `valid`
method that returns a Boolean indicating whether the instance
is valid.
- // Leaving out the required password field
- var params = {
- login: 'alex'
- };
- var user = User.create(params);
-
- // Prints 'false'
- sys.puts(user.valid());
- // Prints 'Field "password" is required'
- sys.puts(user.errors.password);
+```javascript
+// Leaving out the required password field
+var params = {
+ login: 'alex'
+};
+var user = User.create(params);
+
+// Prints 'false'
+sys.puts(user.valid());
+// Prints 'Field "password" is required'
+sys.puts(user.errors.password);
+```
## Running the tests
View
3  bin/cli.js
@@ -152,6 +152,9 @@ else {
case 'controller':
cmd += 'gen:bareController[' + cmds[1] + ']';
break;
+ case 'model':
+ cmd += 'gen:model[' + cmds[1] + ']';
+ break;
case 'secret':
cmd += 'gen:secret';
break;
View
8 examples/todo_app_mongo/config/development.js
@@ -22,7 +22,13 @@ var config = {
, hostname: null
, port: 4000
, sessions: {
- store: 'memory'
+ server: {
+ host : 'localhost'
+ , port : 27017
+ , db : 'testDB'
+ , collection : 'sessions'
+ }
+ , store: 'mongodb'
, key: 'sid'
, expiry: 14 * 24 * 60 * 60
}
View
114 lib/sessions/stores/mongodb.js
@@ -0,0 +1,114 @@
+/*
+ * Geddy JavaScript Web development framework
+ * Copyright 2012 Matthew Eernisse (mde@fleegix.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+*/
+
+/*
+Config section should look like this:
+
+, sessions: {
+ store: 'mongodb'
+ , server: {
+ host : 'localhost'
+ , port : 27017
+ , db : 'testDB'
+ , collection : 'sessions'
+ , user : "tom" // [optional] user
+ , password: 'superpass' //[optional] password
+ }
+ , key: 'sid'
+ , expiry: 14 * 24 * 60 * 60
+
+'server' is your MongoDB server.
+
+If don't provide an expiry in your config file your sessions will live forever.
+*/
+
+
+try {
+ mongo = require('mongodb-wrapper');
+}
+catch(e) {
+ throw new Error('The mongodb wrapper module could not be found. Try doing "npm install mongodb-wrapper."');
+}
+
+var MongoDB = function (callback) {
+ this.setup(callback);
+};
+
+MongoDB.prototype = new (function () {
+ var _sessions = {};
+
+ var _client;
+ var _key;
+ var sessionCollection;
+
+ var self = this;
+
+ this.setup = function (callback) {
+ var host = geddy.config.sessions.server.host || 'localhost';
+ var port = geddy.config.sessions.server.port || 27017;
+ var db = geddy.config.sessions.server.db || 'sessionDB';
+ var prefix = geddy.config.sessions.server.prefix;
+ var user = geddy.config.sessions.server.user || 'user';
+ var password = geddy.config.sessions.server.password || 'pass';
+
+ sessionCollection = geddy.config.sessions.server.collection || 'sessions';
+ _key = geddy.config.sessions.key || 'sid';
+
+ //Authentication will not work until mongodb-wrapper is updated. A fix
+ //was just merged to master on April 18th, 2012
+ _client = mongo.db(host, port, db, prefix, user, password);
+ _client.collection(sessionCollection);
+
+ //Mongo-wrapper offers lazy open/close so we can't really check if the client
+ //exists because it always will exist.
+ callback();
+ };
+
+ this.read = function (session, callback) {
+ var query = {};
+ query[_key] = session.id;
+ _client[sessionCollection].findOne(query, function (err, result) {
+ if (err) {
+ geddy.log.error(err);
+ }
+ callback(result || {});
+ });
+ };
+
+ this.write = function (sess, callback) {
+ var session = sess.data;
+ session[_key] = sess.id;
+
+ if (geddy.config.sessions.expiry) {
+ session.expiry = geddy.config.sessions.expiry;
+ }
+
+ _client[sessionCollection].save(session, function (err, result) {
+ if (err) {
+ geddy.log.error('foo' + err);
+ }
+
+ callback();
+ });
+ };
+
+})();
+
+exports.Mongodb = MongoDB;
+
+
View
9 lib/sessions/stores/redis.js
@@ -1,6 +1,6 @@
/*
* Geddy JavaScript Web development framework
- * Copyright 2112 Matthew Eernisse (mde@fleegix.org)
+ * Copyright 2012 Matthew Eernisse (mde@fleegix.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -34,8 +34,6 @@ Config section should look like this:
'server' is your Redis server.
*/
-var Redis;
-
try {
RedisServer = require('redis');
}
@@ -51,17 +49,18 @@ Redis.prototype = new (function () {
var _sessions = {};
var _client;
+ var _ns;
var self = this;
this.setup = function (callback) {
- var port = geddy.config.sessions.server.posrt || null;
+ var port = geddy.config.sessions.server.port || null;
var host = geddy.config.sessions.server.host || null;
var opts = geddy.config.sessions.server.opts || {};
_ns = geddy.config.sessions.server.ns || "";
_client = RedisServer.createClient(port, host, opts);
_client.on("error", function (err) {
- sys.error("Redis error " + client.host + ":" + client.port + " - " + err);
+ sys.error("Redis error " + _client.host + ":" + _client.port + " - " + err);
});
if (geddy.config.sessions.server.auth) {
Please sign in to comment.
Something went wrong with that request. Please try again.