Skip to content

Commit

Permalink
Add find syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
doctorpangloss committed Feb 12, 2016
1 parent e72e3c1 commit d3388ac
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
30 changes: 29 additions & 1 deletion LiveData/Collection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ public Collection (string name) : base ()
throw new ArgumentException (string.Format ("A collection with name {0} already exists", name));
}


Collection<TRecordType>.Create (name, instance: this);
}

Expand All @@ -130,6 +129,35 @@ public Cursor<TRecordType> Find (Func<TRecordType, bool> selector = null)
return new Cursor<TRecordType> (collection: this, selector: selector);
}

public Cursor<TRecordType> Find (string id)
{
return new Cursor<TRecordType> (collection: this, id: id);
}

public Cursor<TRecordType> Find (IEnumerable<string> ids)
{
return new Cursor<TRecordType> (collection: this, ids: ids);
}

public TRecordType FindOne (string id)
{
return this [id];
}

public TRecordType FindOne (Func<TRecordType, bool> selector = null)
{
selector = selector ?? delegate(TRecordType arg) {
return true;
};

foreach (var record in this) {
if (selector (record)) {
return record;
}
}
return null;
}

public static Collection<TRecordType> Create (string name)
{
return Create (name, new Collection<TRecordType> ());
Expand Down
31 changes: 28 additions & 3 deletions LiveData/Cursor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Meteor
public class Cursor<TRecordType>
where TRecordType : MongoDocument, new()
{

public Collection<TRecordType> collection {
get;
protected set;
Expand All @@ -21,23 +22,47 @@ public Func<TRecordType, bool> selector {
protected set;
}

public IEnumerable<string> ids {
get;
protected set;
}

public Cursor (Collection<TRecordType> collection, Func<TRecordType, bool> selector = null)
{
this.collection = collection;
this.selector = selector ?? SelectAll;
}

public Cursor (Collection<TRecordType> collection, string id)
{
this.collection = collection;
this.ids = new string[] { id };
}

public Cursor (Collection<TRecordType> collection, IEnumerable<string> ids)
{
this.collection = collection;
this.ids = ids;
}

public Observe<TRecordType> Observe (Action<string,TRecordType> added = null, Action<string,TRecordType,IDictionary,string[]> changed = null, Action<string> removed = null)
{
return new Observe<TRecordType> (collection: this.collection, added: added, changed: changed, removed: removed, selector: selector);
}

public IEnumerable<TRecordType> Fetch ()
{
foreach (var record in collection) {
if (selector (record)) {
yield return record;
if (ids == null) {
foreach (var record in collection) {
if (selector (record)) {
yield return record;
}
}
yield break;
}

foreach (var id in ids) {
yield return collection [id];
}

yield break;
Expand Down

0 comments on commit d3388ac

Please sign in to comment.