Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
gravityblast committed Nov 2, 2009
0 parents commit 3913748
Show file tree
Hide file tree
Showing 17 changed files with 599 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
@@ -0,0 +1,6 @@
.DS_Store
couchdb-flex/.actionScriptProperties
couchdb-flex/.flexLibProperties
couchdb-flex/.project
couchdb-flex/.settings
couchdb-flex/bin
Binary file added couchdb-flex/lib/corelib-20070830.swc
Binary file not shown.
36 changes: 36 additions & 0 deletions couchdb-flex/src/com/gravityblast/couchdb/CouchDb.as
@@ -0,0 +1,36 @@
package com.gravityblast.couchdb
{
import com.adobe.net.URI;

public class CouchDb
{

public var host:String;
public var port:uint;
private var _uri:URI;

public static var defaultDatabase:Database;

public function CouchDb(host:String="127.0.0.1", port:uint=5984)
{
this.host = host;
this.port = port;
this._uri = new URI();
this._uri.unknownToURI(this.host);
this._uri.port = this.port.toString();
}

public function database(name:String):Database
{
var database:Database = new Database(this, name);
if (defaultDatabase == null)
defaultDatabase = database;
return database;
}

public function get uri():String
{
return this._uri.toString();
}
}
}
112 changes: 112 additions & 0 deletions couchdb-flex/src/com/gravityblast/couchdb/CouchRest.as
@@ -0,0 +1,112 @@
package com.gravityblast.couchdb
{
import com.adobe.serialization.json.JSON;
import com.adobe.serialization.json.JSONParseError;
import com.gravityblast.couchdb.errors.CouchRestError;
import com.gravityblast.couchdb.events.CouchRestEvent;

import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.HTTPStatusEvent;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;

public class CouchRest extends EventDispatcher
{
public var loader:URLLoader;

public function CouchRest():void
{
this.loader = new URLLoader();
loader.addEventListener(Event.COMPLETE, this.completeEventHandler);
loader.addEventListener(IOErrorEvent.IO_ERROR, this.ioErrorEventHandler);
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, this.httpStatusEventHandler);
super();
}

private function completeEventHandler(event:Event):void
{
var jsonResponse:Object = JSON.decode(this.loader.data);
var event:Event = new CouchRestEvent(CouchRestEvent.COMPLETE, jsonResponse, this.loader.data);
this.dispatchEvent(event);
}

private function ioErrorEventHandler(event:Event):void
{
try
{
var jsonResponse:Object = JSON.decode(this.loader.data);
var event:Event = new CouchRestEvent(CouchRestEvent.COMPLETE, jsonResponse, this.loader.data);
this.dispatchEvent(event);
}
catch (error:JSONParseError)
{
if (this.hasEventListener(CouchRestEvent.IO_ERROR))
{
var _event:CouchRestEvent = new CouchRestEvent(CouchRestEvent.IO_ERROR);
this.dispatchEvent(_event);
}
else
{
throw new CouchRestError("CouchDb Connection Error");
}
}
}

private function httpStatusEventHandler(event:Event):void
{
trace("http status: " + event)
}

public function get(uri:String, params:*=null):void
{
this.load(uri, URLRequestMethod.GET, params);
}

public function put(uri:String, params:*=null, body:*=null):void
{
this.load(uri, URLRequestMethod.PUT, params, body);
}

public function post(uri:String, params:*=null, body:*=null):void
{
this.load(uri, URLRequestMethod.POST, params, body);
}

public function del(uri:String, params:*=null, body:*=null):void
{
this.load(uri, URLRequestMethod.DELETE);
}

public function load(uri:String, method:String, params:*=null, body:*=null, callback:Function=null):void
{
var request:URLRequest = new URLRequest(uri);
request.contentType = "application/json";
request.method = method;
if (params)
{
request.data = this.objectToUrlVariables(params);
}
else if (body)
{
request.data = body;
}

this.loader.load(request);
}

public function objectToUrlVariables(object:Object):URLVariables
{
var variables:URLVariables = new URLVariables();
for (var propertyName:String in object)
{
variables[propertyName] = object[propertyName];
}

return variables;
}
}
}
79 changes: 79 additions & 0 deletions couchdb-flex/src/com/gravityblast/couchdb/Database.as
@@ -0,0 +1,79 @@
package com.gravityblast.couchdb
{
import com.adobe.serialization.json.JSON;
import com.gravityblast.couchdb.errors.DatabaseError;
import com.gravityblast.couchdb.events.CouchRestEvent;
import com.gravityblast.couchdb.events.DatabaseEvent;

import flash.events.EventDispatcher;


public class Database extends EventDispatcher
{

public var couchdb:CouchDb;
public var name:String;

public function Database(couchdb:CouchDb, name:String)
{
this.couchdb = couchdb;
this.name = name;
super();
}

public function create(completeCallback:Function=null):void
{
var couchRest:CouchRest = this.buildCouchRest(completeCallback);
couchRest.put(this.couchdb.uri + "/" + this.name);
}

public function del(completeCallback:Function=null):void
{
var couchRest:CouchRest = this.buildCouchRest(completeCallback);
couchRest.del(this.couchdb.uri + "/" + this.name);
}

public function saveDocument(document:Object, completeCallback:Function=null):void
{
var couchRest:CouchRest = this.buildCouchRest(completeCallback);
if (document._id)
couchRest.put(this.couchdb.uri + "/" + this.name + "/" + document._id, null, JSON.encode(document));
else
couchRest.post(this.couchdb.uri + "/" + this.name, null, JSON.encode(document));
}

public function get(id:String, params:Object=null, completeCallback:Function=null):void
{
var couchRest:CouchRest = this.buildCouchRest(completeCallback);
couchRest.get(this.couchdb.uri + "/" + this.name + "/" + id);
}

public function documents(params:Object=null, completeCallback:Function=null):void
{
var couchRest:CouchRest = this.buildCouchRest(completeCallback);
couchRest.get(this.couchdb.uri + "/" + this.name + "/" + "_all_docs");
}

protected function couchRestErrorHandler(event:CouchRestEvent):void
{
if (hasEventListener(DatabaseEvent.ERROR))
{
var _event:DatabaseEvent = new DatabaseEvent(DatabaseEvent.ERROR);
this.dispatchEvent(_event);
}
else
{
throw new DatabaseError();
}
}

protected function buildCouchRest(completeCallback:Function=null):CouchRest
{
var couchRest:CouchRest = new CouchRest();
couchRest.addEventListener(CouchRestEvent.IO_ERROR, this.couchRestErrorHandler);
if (completeCallback != null)
couchRest.addEventListener(CouchRestEvent.COMPLETE, completeCallback);
return couchRest;
}
}
}
42 changes: 42 additions & 0 deletions couchdb-flex/src/com/gravityblast/couchdb/Design.as
@@ -0,0 +1,42 @@
package com.gravityblast.couchdb
{
public class Design extends Document
{
public var name:String;
private var documentClass:Class;
private var _views:Array;

public function Design(name:String, documentClass:Class=null)
{
this.name = name;
this.documentClass = documentClass;
this._views = new Array();
super();
}

public function addView(view:View):void
{
this._views.push(view);
}

override public function get attributes():Object
{
var _attributes:Object = super.attributes;
delete _attributes["couchdb-flex-type"];
_attributes.language = "javascript";
_attributes._id = "_design/" + this.name;
var views:Object = new Object();
for each (var _view:View in this._views)
{
var view:Object = new Object();
view.map = _view.map;
if (_view.reduce)
view.reduce = _view.reduce;
views[_view.name] = view;
}
_attributes.views = views;

return _attributes;
}
}
}
80 changes: 80 additions & 0 deletions couchdb-flex/src/com/gravityblast/couchdb/Document.as
@@ -0,0 +1,80 @@
package com.gravityblast.couchdb
{
import com.gravityblast.couchdb.events.CouchRestEvent;
import com.gravityblast.couchdb.events.DocumentEvent;

import flash.utils.describeType;
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;

dynamic public class Document
{
public static var database:Database;

public var _id:String;
public var _rev:String;

public function Document()
{
if (Document.database == null)
database = CouchDb.defaultDatabase;
super();
}

public function get attributes():Object
{
var _attributes:Object = {};
for each (var attributeName:String in this.attributeNames())
_attributes[attributeName] = this[attributeName];
for (var dynamicAttributeName:String in this)
_attributes[dynamicAttributeName] = this[dynamicAttributeName];
_attributes["couchdb-flex-type"] = getQualifiedClassName(this);

return _attributes;
}

public function set attributes(value:Object):void
{
for (var propertyName:String in value)
{
try
{
this[propertyName] = value[propertyName];
}
catch(error:Error)
{
trace(error);
}
}
}

public function save(completeCallback:Function=null):void
{
var saver:DocumentSaver = new DocumentSaver(database, this);
if (completeCallback != null)
saver.addEventListener(CouchRestEvent.COMPLETE, completeCallback);
saver.save();
}

private function attributeNames():Array
{
var names:Array = new Array();
var classInfo:XML = describeType(this);
for each ( var v:XML in classInfo..*.((true) && (name() == "variable" || (name() == "accessor" && attribute( "access" ).charAt( 0 ) == "r") )))
{
if (v.@name != "attributes" && v.@name != "database" && v.@name != "_id" && v.@name != "_rev")
names.push(v.@name);
}

return names;
}

public static function load(id:String, completeCallback:Function=null):void
{
var loader:DocumentLoader = new DocumentLoader();
if (completeCallback != null)
loader.addEventListener(DocumentEvent.LOADED, completeCallback);
loader.load(id);
}
}
}

0 comments on commit 3913748

Please sign in to comment.