Skip to content

Commit

Permalink
adding create operation methods for query requests
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultz committed Apr 21, 2011
1 parent 81572c0 commit 631deb8
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 57 deletions.
22 changes: 22 additions & 0 deletions src/mesh/services/ItemQueryRequest.as
@@ -0,0 +1,22 @@
package mesh.services
{
import flash.utils.flash_proxy;

use namespace flash_proxy;

public dynamic class ItemQueryRequest extends QueryRequest
{
public function ItemQueryRequest(service:Service, deserializer:Function, block:Function)
{
super(service, deserializer, block);
}

override flash_proxy function set object(value:*):void
{
if (value is Array) {
value = value[0];
}
super.object = value;
}
}
}
22 changes: 11 additions & 11 deletions src/mesh/services/ListQueryRequest.as
Expand Up @@ -12,15 +12,15 @@ package mesh.services
{
private var _list:ArrayList;

public function ListQueryRequest(service:Service, block:Function)
public function ListQueryRequest(service:Service, deserializer:Function, block:Function)
{
_list = new ArrayList();
_list.addEventListener(CollectionEvent.COLLECTION_CHANGE, function(event:CollectionEvent):void
{
dispatchEvent(event);
});

super(service, block);
super(service, deserializer, block);
}

/**
Expand All @@ -36,7 +36,7 @@ package mesh.services
*/
public function addItemAt(item:Object, index:int):void
{
flash_proxy::object.addItemAt(item, index);
object.addItemAt(item, index);
}

/**
Expand All @@ -56,23 +56,23 @@ package mesh.services
*/
public function getItemAt(index:int, prefetch:int = 0):Object
{
return flash_proxy::object.getItemAt(index, prefetch);
return object.getItemAt(index, prefetch);
}

/**
* @inheritDoc
*/
public function getItemIndex(item:Object):int
{
return flash_proxy::object.getItemIndex(item);
return object.getItemIndex(item);
}

/**
* @inheritDoc
*/
public function itemUpdated(item:Object, property:Object = null, oldValue:Object = null, newValue:Object = null):void
{
flash_proxy::object.itemUpdated(item, property, oldValue, newValue);
object.itemUpdated(item, property, oldValue, newValue);
}

/**
Expand Down Expand Up @@ -110,7 +110,7 @@ package mesh.services
*/
public function removeAll():void
{
flash_proxy::object.removeAll();
object.removeAll();
}

/**
Expand All @@ -132,31 +132,31 @@ package mesh.services
*/
public function removeItemAt(index:int):Object
{
return flash_proxy::object.removeItemAt(index);
return object.removeItemAt(index);
}

/**
* @inheritDoc
*/
public function setItemAt(item:Object, index:int):Object
{
return flash_proxy::object.setItemAt(item, index);
return object.setItemAt(item, index);
}

/**
* @inheritDoc
*/
public function toArray():Array
{
return flash_proxy::object.toArray();
return object.toArray();
}

/**
* @inheritDoc
*/
public function get length():int
{
return flash_proxy::object.length;
return object.length;
}

/**
Expand Down
19 changes: 13 additions & 6 deletions src/mesh/services/QueryRequest.as
@@ -1,18 +1,25 @@
package mesh.services
{
public dynamic class QueryRequest extends OperationRequest
import mesh.core.array.flatten;

public dynamic class QueryRequest extends ServiceRequest
{
private var _service:Service;
private var _deserializer:Function;

public function QueryRequest(service:Service, block:Function)
public function QueryRequest(service:Service, deserializer:Function, block:Function)
{
super(block);
_service = service;
super(service, block);
_deserializer = deserializer;
}

override protected function result(data:Object):void
{
_service.register(data);
if (data.hasOwnProperty("toArray")) {
data = data.toArray();
}

data = _deserializer(flatten(data));
service.register(data);
super.result(data);
}
}
Expand Down
40 changes: 31 additions & 9 deletions src/mesh/services/Service.as
Expand Up @@ -36,23 +36,29 @@ package mesh.services
* @return An unexecuted operation.
*/
public function all():ListQueryRequest
{
return new ListQueryRequest(this, deserialize, function():Operation
{
return createAllOperation();
});
}

protected function createAllOperation():Operation
{
throw new IllegalOperationError(reflect.name + " does not support retrieval of entities using all()");
}

public function belongingTo(entity:Entity):ListQueryRequest
{
throw new IllegalOperationError(reflect.name + " does not support retrieval of entities belonging to " + entity);
return new ListQueryRequest(this, deserialize, function():Operation
{
return createBelongingToOperation(entity);
});
}

public function create(clazz:Class):*
protected function createBelongingToOperation(entity:Entity):Operation
{
if (clazz is Entity) {
var instance:Entity = new clazz();
register(instance);
return instance;
}
throw new ArgumentError("Expected class to be an Entity");
throw new IllegalOperationError(reflect.name + " does not support retrieval of entities belonging to " + entity);
}

/**
Expand Down Expand Up @@ -115,12 +121,28 @@ package mesh.services
return ids.length == 1 ? findOne(ids[0]) : findMany(ids);
}

public function findOne(id:*):QueryRequest
public function findOne(id:*):ItemQueryRequest
{
return new ItemQueryRequest(this, deserialize, function():Operation
{
return createFindOneOperation(id);
});
}

protected function createFindOneOperation(id:*):Operation
{
throw new IllegalOperationError(reflect.name + " does not support retrieval of entities using findOne().");
}

public function findMany(...ids):ListQueryRequest
{
return new ListQueryRequest(this, deserialize, function():Operation
{
return createFindManyOperation.apply(null, ids);
});
}

protected function createFindManyOperation(...ids):Operation
{
throw new IllegalOperationError(reflect.name + " does not support retrieval of entities using findMany().");
}
Expand Down
17 changes: 17 additions & 0 deletions src/mesh/services/ServiceRequest.as
@@ -0,0 +1,17 @@
package mesh.services
{
public class ServiceRequest extends OperationRequest
{
public function ServiceRequest(service:Service, block:Function)
{
super(block);
_service = service;
}

private var _service:Service;
protected function get service():Service
{
return _service;
}
}
}
48 changes: 19 additions & 29 deletions src/mesh/services/TestService.as
Expand Up @@ -23,48 +23,38 @@ package mesh.services
_belongingToBlock = belongingToBlock;
}

override public function belongingTo(entity:Entity):ListQueryRequest
override protected function createBelongingToOperation(entity:Entity):Operation
{
return new ListQueryRequest(this, function():Operation
return createOperation(function():Object
{
return createOperation(function():Object
{
return _belongingToBlock == null ? [] : deserialize(_belongingToBlock(entity, _registry.values()));
});
return _belongingToBlock == null ? [] : _belongingToBlock(entity, _registry.values());
});
}

override public function findOne(id:*):QueryRequest
override protected function createFindOneOperation(id:*):Operation
{
return new QueryRequest(this, function():Operation
return createOperation(function():Object
{
return createOperation(function():Object
{
if (_registry.containsKey(id)) {
var entities:Array = deserialize([_registry.grab(id)]);
return entities[0];
}
throw new ArgumentError("Entity not found with ID=" + id);
});
if (_registry.containsKey(id)) {
return _registry.grab(id);
}
throw new ArgumentError("Entity not found with ID=" + id);
});
}

override public function findMany(...ids):ListQueryRequest
override protected function createFindManyOperation(...ids):Operation
{
return new ListQueryRequest(this, function():Operation
return createOperation(function():Object
{
return createOperation(function():Object
{
var objects:Array = [];
for each (var id:int in ids) {
if (_registry.containsKey(id)) {
objects.push(_registry.grab(id));
} else {
throw new ArgumentError("Entity not found with ID=" + id);
}
var objects:Array = [];
for each (var id:int in ids) {
if (_registry.containsKey(id)) {
objects.push(_registry.grab(id));
} else {
throw new ArgumentError("Entity not found with ID=" + id);
}
return deserialize(objects);
});
}
return objects;
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/mesh/services/WhereQueryRequest.as
Expand Up @@ -2,9 +2,9 @@ package mesh.services
{
public dynamic class WhereQueryRequest extends ListQueryRequest
{
public function WhereQueryRequest(service:Service, block:Function)
public function WhereQueryRequest(service:Service, deserializer:Function, block:Function)
{
super(service, block);
super(service, deserializer, block);
}
}
}

0 comments on commit 631deb8

Please sign in to comment.