-
Notifications
You must be signed in to change notification settings - Fork 17
Ruby Persistence API
qertoip edited this page Mar 2, 2012
·
6 revisions
This is a very early draft of the Ruby Persistence API. The aim is to define an API layer universal for all persistence frameworks. We need that abstraction to isolate our apps from persistence frameworks.
- generic
- simple
- entities are PORO and should know nothing about persistence
- mimic ActiveRecord whenever possible
Creating new object in a database:
db[User].create!( :name => 'Piotr Włodarek', :email => 'whatever@dev.null' ) # => User instance | raises ObjectInvalid
db[User].create( :name => 'Piotr Włodarek', :email => 'whatever@dev.null' ) # => User instance
Saving existing object to a database:
db[user].save # => true | false
db[user].save! # => true | raises ObjectInvalid
Finding an object in a database by id:
db[User].find( id ) # => User instance
Updating attributes of an object in a database:
db[user].update_attributes( :name => 'New name', :email => 'new.email@dev.null' ) # => true | false
db[user].update_attributes!( :name => 'New name', :email => 'new.email@dev.null' ) # => true | raises ObjectInvalid
The "db" is a persistence backend, i.e. adapter to ActiveRecord.