Skip to content

Instantly share code, notes, and snippets.

@mattly
Created April 4, 2009 21:49
Show Gist options
  • Save mattly/90296 to your computer and use it in GitHub Desktop.
Save mattly/90296 to your computer and use it in GitHub Desktop.
# = rCouch (working title)
# a system for working with couchdb documents that works similar to jquery
Collection.new(@db, {})
Collection.new(@db, [{}, {}])
# creates a collection of documents
# == Retrieving from the Database
# Single Document GET, for when you know the id
@db.get('some-document-id')
# => [{'_id' => 'some-document-id', '_rev' => '1-23523'}]
# Raw call to a view
@db.view('design/view', {:key => 'foo'})
# => [{'key' => 'foo', 'value' => nil, 'id' => 'some-document'}]
@db.view('design/reduceview', {:group => true})
# => [{'key' => 'foo', 'value' => 5}, {'key' => 'bar', 'value' => 3}]
# ____mayyyybe_____ the next chunk, idk, i'm of two minds about it even though i already implemented most of it
# For a view defined in the db class
# class Database
# include Exegesis::Database
# design :foos do
# docs :by_foo # sets :include_docs => true, :reduce => false and ensures deletion of :group_level
# hash :count, :view => :by_foo # calls error if no reduce function defined; sets :group => true and ensures deletion of :include_docs
# end
# end
@db.foos.by_foo("key") # returns a wrapper around the 'rows' result of the output
# [{'id' => 'foo', 'key' => 'key', 'value' => nil, 'doc' => {...}}]
@db.foos.count
# [{'foo' => 5, 'bar' => 3}]
@db.foos.count(:group => false)
# 8
# == Attributes
# where @collection is one of results of the above
# on documents
@collection.attrib(key) # where key is a string or symbol
# returns an array of the values of the given key on all documents in the collection
@collection.attrib(key, value)
# sets the key to value on all documents in the collection
@collection.attrib({:key => value, :other_key => other_value})
# sets the key/value pairs on all matched documents
# NOTE: if the given hash contains a _rev attribute it must match the document's rev attribute
@collection.attrib(key, lambda{|doc, key, value|})
# sets the key on all matched document to the value returned by the proc
@collection.remove_attrib(key)
# deletes the key from all documents in the collection
# on view rows
@collection.values
# returns an array of the values on all rows in the collection
@collection.keys
# returns an (non-uniq) array of all the keys in the collection
@collection.ids
# returns an (non-uniq) array of all the document ids for rows in the collection
# == Filtering
# on view rows
@collection.key(value)
@collection.key(lambda{|key, val|})
# reduce the set to rows matching either the key value or returning truthy for the proc
@collection.key_isnt(value)
@collection.key_isnt(lambda{|key,val|})
# reduce the set to rows whose keys do not match the given key or return truthy for the proc
@collection.value(val)
@collection.value(lambda{|val|})
# reduce the set to rows with values matching the given value, or returning truthy for the proc
@collection.value_isnt(val)
@collection.value_isnt(lambda{|key,val|})
# reduce the set to rows whose values do not match the given value or return truthy for the proc
# on documents
@collection.has_attrib(key)
# reduce the set to rows containing documents that have the given key
@collection.is({:key => value})
# reduce the set to rows containing documents that have the given key/value pair
@collection.isnt({:key => value})
# reduce the set to rows containing documents that do not have the given key/value pair
# on the set
@collection.intersect(other_collection)
# reduce the set to rows that are also in other_collection
@collection.exclude(other_collection)
# reduce the set to rows that are not in other_collection
@collection.index(index)
@collection.index(start, length)
@collection.index(range)
# reduce the collection to rows at the given index(es)
@collection.index_isnt(index)
@collection.index_isnt(start, length)
@collection.index_isnt(range)
# remove the rows at the given indexes from the collection
@collection.select(lambda{|key,value,doc|})
# reduce the set to rows that return truthy for the proc
@collection.reject(lambda{|key,value,doc|})
# reduce the set to rows that return falsy for the proc
@collection.errors
# reduce the set to rows containing an error key (most useful post-save)
@collection.__some_filtering__.end
# revert the most recent filtering operation
# == Manipulation
@collection.documents
# does a "multi-key GET" to _all_docs for all rows in the view and loads documents in place
# if all rows have documents does not do anything
@collection.update(lambda{|doc,key,value|})
# calls the proc for all documents in the collection, merging return of the proc into document
@collection.delete
@collection.delete(lambda{|doc,key,value|})
# sets "_deleted: true" in all documents or documents returning truthy for the proc
@collection.save
# performs a bulk update on the collection, updating revs and adding ids as necessary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment