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.
All you need to use jrodb is JRuby 1.2.0.
git clone git://github.com/teamon/jrodb.git
cd jrodb
rake install
# app/models/cat.rb
class Cat
include Jrodb::Model
field :name, String
field :age, Fixnum
end
jrodb generate app/models
jrodb compile app/models
require 'rubygems'
require 'jrodb'
Jrodb.load_models
Dir["app/models/*.rb"].each {|f| require f }
Jrodb::Database.setup :dbfile => 'simple.db'
Cat.create(:name => "Kitty", :age => 2)
Cat.create(:name => "Foobar", :age => 1)
Cat.all
This step is not necessary - database gets closed automaticly when process ends. Jrodb::Database.close
Due to classpaths issue the best way to run this program is to use jrodb executable. jrodb run main.rb
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
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.
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
Cat.all.order(:name.desc)
Cat.all.order(:name.asc, :age.desc)
Cat.all.order {|a,b| a.name.size <=> b.name.size }