Coder Social home page Coder Social logo

leanmodel's Introduction

This was just an experiment. Nothing much to see here

Leanmodel

Simple ORM for CouchDB. Based on ActiveModel. This a work in progress…

Installation

gem install leanmodel

Usage

require 'leanback'
require 'leanmodel'

Each model class represents a document in a CouchDB database. The class name must be singular to the Couchdb database name. The class below represents a document in a couchDB 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)

Note: you need the leanback rubygem for this line in the above lambda: Couchdb.login(@couchdb_username,@couchdb_password)

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"}]

Developed & Tested On

Ubuntu 11.04, CouchDB 1.0.1, Ruby 1.9.3

Copyright © 2012 obi-a. See LICENSE.txt for further details.

leanmodel's People

Contributors

obi-a avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.