Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Ruby API for CouchDB, designed to work with the Camping micro-framework.
Ruby
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
test
.document
.gitignore
Gemfile
Gemfile.lock
LICENSE.txt
README
Rakefile
ShyCouch.gemspec
VERSION

README

= ShyCouch

ShyCouch is a Ruby library for CouchDB. It's a data persistence layer that uses native objects and lets you write native Ruby blocks that'll be parsed into MapReduce JavaScript functions.

The structure is a bit of a mess, but essentially it provides:

- a database object
- a CouchDocument object, which is a glorified hash
- a Design object representing the Design document for an app in CouchDB

At the moment, all views have to be written manually. I'm currently evaluating the wisdom of building a query language to dynamically write views and cache them in Couch.

= Usage

	require 'ShyCouch'
	settings = {
	  "db"=> {
	      "host" => "ramponeau.local",
	      "port" => 5984,
	      "name" => "food",
	      "user" => "cerales",
	      "password" => "password"
	    },
	}
	db = ShyCouch::CouchDatabase()

== Models

You don't need to define entity relationships or anything. Your models can just look like this:

	module Food::Models
	  class Recipe < CouchDocument; end
	end

You can do a bit more if you want:

	module Food::Models
	  class Recipe < CouchDocument
	    needs :ingredients
	    needs :directions
	    suggests :cost
	  end
	end
	
== Helpers

You define your map & reduce functions - your Couch views - in your Helpers (not entiiiirely working yet)

	module Food::Helpers

	  design do
	    view all_recipes do
	      map do
	        def function(doc)
	          if doc.kind == "recipe"
	            emit(doc)
  	        end
	      end
    
	     reduce do
	        # whatever
	      end

	    end

	    view ingredients do
	      # a different view
	    end
	  end
	end

You can then call your views by name in your controllers:

	module Food::Controllers
	  class Recipes < R '/recipes/'
	    @recipes = View.all_recipes
	    render :recipes
	  end
	end
	
Views give you back a special JSON hash where you can treat the keys as object attributes and all that:
	
	module Food::Views
	  def recipes
	    recipes.each do |recipe|
	      h2 recipe.title
	      recipe.ingredients.each do |ingredient|
	        p "#{ingredient.name}: #{ingredient.quantity}"
	      end
	    end
	  end
	end

= Map/Reduce

ShyCouch uses the related ShyRubyJS library to parse Ruby blocks as JavaScript. This means you can do this:

	map do
	  emit(doc) if doc.kind == "post" and !doc.hidden
	end

and you'll get this:

	function ( doc ) { 
	  if( doc.kind.==("post") && !doc.hidden ) {
	    emit(doc)
	  } 
	}

== Copyright

Copyright (c) 2011 Cerales. See LICENSE.txt for
further details.

Something went wrong with that request. Please try again.