Last modified 6 years ago
Og Evaluation
(Old evaluation, 2005-05)
Prerequisites
- Ruby Audit
- SQLite3
- og-evolve.rb (extension for automated table evolution)
Installation
- cmd> gem install og [accept dependencies install]
- cmd> gem install sqlite3 [select latest version for your OS]
Creating the Project
- Create folder cars
- Create file cars.rb
- Create file main.rb
- Copy og-evolve.rb into cars
Creating a Simple Class
- Definition
- Within file cars.rb, use any editor to insert this::
require 'og' require 'og-evolve' class Car prop_accessor :manufacturer, String prop_accessor :model, String prop_accessor :modelYear, Fixnum end
- Within file cars.rb, use any editor to insert this::
- Preparation none
- Deployment none
- Usage
# main.rb require 'cars' db = Og::Database.new( :database => 'jampersist', :adapter => 'sqlite', :user => 'user', :password => 'password' ) if Car.all.size == 0 car1 = Car.new() car1.manufacturer = 'Ford' car1.model = 'Mustang' car1.modelYear = 1990 car1.save() end
Adding an 1:1 Relation
- Declaration
- Add class Owner (within cars.rb)
class Owner prop_accessor :name, String prop_accessor :proficiency, String has_one :car, Car def initialize(name='', proficiency='') @name = name @proficiency = proficiency end end
- Add class Owner (within cars.rb)
- Add a "1:1" relation to Car
class Car belongs_to :owner, Owner end
- Preparation
- none
- Deployment
- none
- Usage
# main.rb if Owner.all.size == 0 Owner.create('Paul') peter = Owner.new('Peter') peter.save() car = Car[1] car.owner = peter car.save() end
Adding a 1:N Relation
- Declaration
- Add class Driver
class Driver prop_accessor :name, String prop_accessor :licenseType, String belongs_to :car, Car def initialize(name='', licenseType=nil) @name = name @licenseType = licenseType end end
- Add class Driver
- Add "1:n" relation to Car.
class Car ... has_many :drivers, Driver
- Preparation
- none
- Deployment
- none
- Usage
# main.rb if Driver.all.size == 0 Driver.new('Fred', 'heavy').save() Driver.create('Frank', 'normal') fred = Driver['Fred'] frank = Driver['Frank'] car = Car[1] car.add_driver(fred) car.add_driver(frank) end
Adding a M:N Relation
- Declaration
- Add class Road
- Add class Trip
- Add "m:n" relation to Cars / Roads.
# main.rb if Driver.all.size == 0 Driver.new('Fred', 'heavy').save() Driver.create('Frank', 'normal') fred = Driver['Fred'] frank = Driver['Frank'] car = Car[1] car.add_driver(fred) car.add_driver(frank) end
- Preparation
- none
- Deployment
- none
- Usage
- cmd>
Mailinglist Topic
Attachments
-
og-evolve.rb
(5.3 KB) -
added by lazaridis_com 6 years ago.
persist.ruby.og schema-evolution support