DataMapper 0.9 is Released
DataMapper 0.9 is ready for the world. It brings with it a massive overhaul of
the internals of DataMapper, a shift in terminology, a dramatic bump in speed,
improved code-base organization, and support for more than just database
data-stores.
To install it:
sudo gem install addressable english rspec
sudo gem install data_objects do_mysql do_postgres do_sqlite3
sudo gem install dm-core
This is NOT a backwards compatible release. Code written for DataMapper 0.3 will
not function with DataMapper 0.9.* due to syntactical changes and library
improvements.
REPEAT: This is NOT a backwards compatible release.
DataMapper 0.3
DataMapper 0.9
Creating a class
class Post < DataMapper :: Base
end
class Post
include DataMapper :: Resource
end
Keys
# Key was not mandatory
# Automatically added +id+ if missing
#
# Natural Key
property :name , :string , :key => true
#
# Composite Key
property :id , :integer , :key => true
property :slug , :string , :key => true
# keys are now mandatory
property :id , Serial
#
# Natural Key
property :slug , String , :key => true
#
# Composite Key
property :id , Integer , :key => true
property :slug , String , :key => true
Properties
property :title , :string
property :body , :text
property :posted_on , :datetime
property :active , :boolean
property :title , String
property :body , Text
property :posted_on , DateTime
property :active , Boolean
Associations
has_many :comments
belongs_to :blog
has_and_belongs_to_many :categories
has_one :author
has n , :comments
belongs_to :blog
has n , :categories => Resource
has 1 , :author
Finders
Post . first :order => 'created_at DESC'
Post . all
:conditions => [ 'active = ?' , true ]
database . query 'SELECT 1'
database . execute 'UPDATE posts...'
Post . first :order => [ :created_at . desc ]
Post . all
:conditions => [ 'active = ?' , true ]
repository ( :default ). adapter . query 'SELECT 1'
repository ( :default ). adapter . execute 'UPDATE posts...'
Validations
validates_presence_of :title
validates_numericality_of :rating
validates_format_of :email , :with => :email_address
validates_length_of :summary , :within => 1 .. 100
validates_uniqueness_of :slug
validates_present :title
validates_is_number :rating
validates_format :email , :as => :email_address
validates_length :summary , :in => 1 .. 100
validates_is_unique :slug
Callbacks
before_save :categorize
before_create do | post |
# do stuff with post
end
# return false to abort
before :save , :categorize
before :create do
# do stuff with self
end
# throw :halt to abort