Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3913748
Showing
17 changed files
with
599 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.DS_Store | ||
couchdb-flex/.actionScriptProperties | ||
couchdb-flex/.flexLibProperties | ||
couchdb-flex/.project | ||
couchdb-flex/.settings | ||
couchdb-flex/bin |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
Oops, something went wrong.