= Leanmodel
Simple ORM for CouchDB. This a work in progress...
==Installation
gem install leanmodel
==Usage
require 'leanback'
require 'leanmodel'
Name of the class must be singular to a Couchdb database name .
The class below represents a document in the database called movies
class Movie < LeanModel::Base
attributes :title, :year, :story
end
movie = Movie.new({:title => 'Jurassic Park',:year => '1993', :story => 'Raptors brought back in time'})
movie.attributes
#=> {"title"=>"Jurassic Park", "year"=>"1993", "story"=>"Raptors brought back in time"}
Configure couchDB
@couchdb_username = "obi"
@couchdb_password = "trusted"
token = lambda {
hash = Couchdb.login(@couchdb_username,@couchdb_password)
hash["AuthSession"]
}
movie.config(token)
CouchDB Authsession token
movie.token
#=> b2JpOjRGQTIxODA4Oq42xHwvvoK9ASvtB55ODSdEpEgB
Database name
movie.database
#=> movies
Write this model to the database
movie.save
#=> true
Returns true if the model was written successfully.
This method creates a new couchDB document with the model's attribute values
Delete this model from database
movie.destroy
#=>{"ok"=>true, "id"=>"eb9ecefa-afdd-426a-a21d-f8307fa2b156", "rev"=>"2-98bc85346eff4a663e39395163aaa194"}
This deletes the document that represents this model from the database
Find a movie using it's document id
movie.service.find('9da82f5a-0ce4-46a0-863c-7d8f2ed84439')
#=> {"_id"=>"9da82f5a-0ce4-46a0-863c-7d8f2ed84439", "_rev"=>"6-6367dc24342f1590935a99087a57d8c5",
"title"=>"Jurassic Park", "year"=>"1996", "story"=>"Raptors brought back in time"}
Update a model on the database,
data = {:id => '9da82f5a-0ce4-46a0-863c-7d8f2ed84439', :year => '1993' }
movie.service.update_attributes(data)
#=> true
Returns true if successful, requires that the document ID is included in the data
Retrieve all movies from database
movie.service.all
#=> [{"_id"=>"0fb32571-44c4-48c4-9423-55ea7c95c192", "_rev"=>"1-f76f5d4f431cafbdb3f4d43388a03c89",
"title"=>"Hugo", "year"=>"2011", "story"=>"Film history"},
{"_id"=>"9da82f5a-0ce4-46a0-863c-7d8f2ed84439", "_rev"=>"10-60e258f605801a31e64b928bb81f7ea6",
"title"=>"Jurassic Park", "year"=>"1993", "story"=>"Raptors brought back in time"}]
== Copyright
Copyright (c) 2012 obi-a. See LICENSE.txt for
further details.