Skip to content

Commit

Permalink
Materialize ResultList entities from store
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultz committed Oct 18, 2011
1 parent 8d55c16 commit f10b3ff
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 14 deletions.
6 changes: 4 additions & 2 deletions src/mesh/model/source/FixtureSource.as
Expand Up @@ -9,6 +9,7 @@ package mesh.model.source
import mesh.model.store.Query;
import mesh.model.store.ResultList;
import mesh.model.store.Snapshot;
import mesh.model.store.Store;

public class FixtureSource extends Source
{
Expand Down Expand Up @@ -58,11 +59,12 @@ package mesh.model.source
/**
* @inheritDoc
*/
override public function fetch(query:Query, results:ResultList):void
override public function fetch(query:Query, store:Store, results:ResultList):void
{
for each (var hash:Object in _fixtures) {
//request.loaded(_entityType, hash);
results.addKey(store.insert(_entityType, hash));
}
results.complete();
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/mesh/model/source/MultiSource.as
Expand Up @@ -9,6 +9,7 @@ package mesh.model.source
import mesh.model.store.Query;
import mesh.model.store.ResultList;
import mesh.model.store.Snapshot;
import mesh.model.store.Store;

/**
* An entity source that maps a type of entity to its source.
Expand Down Expand Up @@ -83,9 +84,9 @@ package mesh.model.source
/**
* @inheritDoc
*/
override public function fetch(query:Query, results:ResultList):void
override public function fetch(query:Query, store:Store, results:ResultList):void
{
sourceFor(query.entityType).fetch(query, results);
sourceFor(query.entityType).fetch(query, store, results);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/mesh/model/source/Source.as
Expand Up @@ -8,6 +8,7 @@ package mesh.model.source
import mesh.model.store.Query;
import mesh.model.store.ResultList;
import mesh.model.store.Snapshot;
import mesh.model.store.Store;

/**
* The <code>Source</code> class is responsible for persisting an <code>Entity</code>.
Expand Down Expand Up @@ -50,7 +51,7 @@ package mesh.model.source
}
}

public function fetch(query:Query, results:ResultList):void
public function fetch(query:Query, store:Store, results:ResultList):void
{
throw new IllegalOperationError("Source.fetch() is not implemented.");
}
Expand Down
10 changes: 4 additions & 6 deletions src/mesh/model/store/ResultList.as
Expand Up @@ -104,7 +104,6 @@ package mesh.model.store
}

import mesh.model.Entity;
import mesh.model.store.EntityIndex;
import mesh.model.store.Store;

import mx.collections.ArrayList;
Expand All @@ -117,26 +116,25 @@ import mx.collections.ArrayList;
*/
class KeyList extends ArrayList
{
private var _entities:EntityIndex;
private var _store:Store;

/**
* Constructor.
*
* @param store The store to retrieve entities from.
*/
public function KeyList(entities:EntityIndex)
public function KeyList(store:Store)
{
super();
_entities = entities;

_store = store;
}

/**
* @inheritDoc
*/
override public function getItemAt(index:int, prefetch:int=0):Object
{
return _entities.findByKey(getItemAt(index, prefetch));
return _store.materialize(super.getItemAt(index, prefetch));
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/mesh/model/store/SourceData.as
@@ -1,5 +1,10 @@
import mesh.model.Entity;

package mesh.model.store
{
import mesh.core.reflection.newInstance;
import mesh.model.Entity;

public class SourceData
{
public function SourceData(storeKey:Object, entityType:Class, data:Object, id:Object)
Expand All @@ -10,6 +15,13 @@ package mesh.model.store
_id = id;
}

public function materialize():Entity
{
var entity:Entity = new _storeKey();
entity.fromObject(data);
return entity;
}

private var _data:Object;
public function get data():Object
{
Expand Down
23 changes: 20 additions & 3 deletions src/mesh/model/store/Store.as
Expand Up @@ -5,15 +5,12 @@ package mesh.model.store
import flash.errors.IllegalOperationError;
import flash.events.EventDispatcher;

import mesh.core.reflection.newInstance;
import mesh.core.state.StateEvent;
import mesh.model.Entity;
import mesh.model.source.Source;

import mx.events.PropertyChangeEvent;

import org.flexunit.runner.Result;

/**
* The store represents a repository for all <code>Entity</code>s in your application. The store
* is assigned a data source, which is responsible for persisting the changes to each entity.
Expand Down Expand Up @@ -207,6 +204,26 @@ package mesh.model.store
return key;
}

/**
* Materializes the data hash that's mapped to the given key into an <code>Entity</code>.
*
* @param key The key to materialize.
* @return An entity.
*/
public function materialize(key:Object):Entity
{
var entity:Entity = entities.findByKey(key);

if (entity == null) {
var source:SourceData = data.findByKey(key);
if (source == null) throw new ArgumentError("Data undefined for key '" + key + "'");
entity = source.materialize();
register(entity);
}

return entity;
}

private function register(entity:Entity):void
{
entity.storeKey = generateStoreKey();
Expand Down
5 changes: 5 additions & 0 deletions src/mesh/model/store/StoreIndex.as
Expand Up @@ -34,6 +34,11 @@ package mesh.model.store
return _keyToData[storeKey] != null;
}

public function findByKey(key:Object):SourceData
{
return _keyToData[key];
}

public function findByType(type:Object):List
{
type = reflect(type).clazz;
Expand Down

0 comments on commit f10b3ff

Please sign in to comment.