Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Ruby Java
Branch: master
Pull request Compare This branch is 87 commits ahead of comboy:master.

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
bin
experiments
lib
spec
validations
.gitignore
LICENSE
README.markdown
Rakefile
TODO
rdb4o.gemspec

README.markdown

Jrodb

see http://teamon.github.com/jrodb

Author: Kacper Cieśla, Tymon Tobolski

Small library I wrote for fun too have even more fun with db4o and jruby :) Lots of thanks for Marcin Mielżyński (lopex) for helping me with this.

Requirements

All you need to use jrodb is JRuby 1.2.0.

Getting started

git clone git://github.com/teamon/jrodb.git
cd jrodb
rake install

Usage

Model file

# app/models/cat.rb
class Cat
  include Jrodb::Model

  field :name, String
  field :age, Fixnum
end

Generate and compile

jrodb generate app/models
jrodb compile app/models

Require jrodb

require 'rubygems'
require 'jrodb'

Load models

Jrodb.load_models
Dir["app/models/*.rb"].each {|f| require f }

Connect to database

Jrodb::Database.setup :dbfile => 'simple.db'

Create some objects

Cat.create(:name => "Kitty", :age => 2)
Cat.create(:name => "Foobar", :age => 1)

Get them from database

Cat.all

Close database

This step is not necessary - database gets closed automaticly when process ends. Jrodb::Database.close

Run program

Due to classpaths issue the best way to run this program is to use jrodb executable. jrodb run main.rb

Relations

One to Many

class Cat
  include Jrodb::Model

  field :name, String
  field :age, Fixnum

  belongs_to :person
  belongs_to :owner, :class => Person
end

class Person
  include Jrodb::Model

  field :name, String

  has_many :cats
  has_many :friends, :class => Cat, :key => :owner
end

p = Person.create(:name => "Foo")
c = Cat.new

p.cats 
p.cats.new
p.cats.create

c = Cat.new
p.cats << c
p.cats.delete(c)
p.cats.destroy_all!

c.person = p

Filtering

Cat.all # all objects
Cat.all(:name => "Kitty") # all cats with name "Kitty"
Cat.all{|c| c.age > 1 } # cats older than 1 year
# etc.

Scope

class Cat
  scope(:young) {|c| c.age <= 2}
  scope(:old) {|c| c.age > 4}
  scope :black, :conditions => {:color => "black"}
  scope :white, :conditions => {:color => "white"}
  scope(:colorful) {|c| c.color != "black" && c.color != "white"}
  scope(:four_letters) {|c| c.name.size == 4}
  scope :kitties, :conditions => {:name => "Kitty"}
end

Cat.young
Cat.old
Cat.kitties.black.young

# works with relations

p = Person.create(:name => "Eric")

p.cats.black
p.cats.black.old.new

Ordering

Cat.all.order(:name.desc)
Cat.all.order(:name.asc, :age.desc)
Cat.all.order {|a,b| a.name.size <=> b.name.size }
Something went wrong with that request. Please try again.