| |
What is DrySQL?
DrySQL is a Ruby plug-in that extends ActiveRecord and applies the DRY
principle to Object-Relational Mapping.
DrySQL reflects on your database's schema and automatically generates your model classes' associations and validations, and correctly identifies your columns and keys.
The philosophy behind DrySQL is that you define columns, keys, constraints, and
relationships on your database, and you shouldn't need to re-define
any of these things in your application code.
Using DrySQL
-
Download from RubyForge
or use RubyGems: gem install drysql
-
Call
require_gem 'drysql'
somewhere in your application code such that it will get processed before you attempt to instantiate a model class (i.e. a subclass of ActiveRecord::Base)
-
That's all!
DrySQL is an extension to ActiveRecord, and is compatible with any plug-ins that ActiveRecord is compatible with. You don't need to "do" anything to use DrySQL other than require the gem in your application code. DrySQL simply removes the need for the remaining bits of Object-Relational Mapping redundancy, namely the definition of associations, validations, and keys.
-
|
Your class definition without DrySQL
|
Your class definition with DrySQL
|
class Employee < ActiveRecord::Base
set_table_name "EMP123"
set_primary_key "EMP_REF_ID"
belongs_to :department, :foreign_key=>'DEP_NAME'
has_many :projects, :foreign_key=>'PROJECT_NAME'
has_one :location
has_many :whatevers, :through=>projects
validates_length_of :NAME, :allow_nil=>true, :maximum=>20
validates_numericality_of :EMP_REF_ID, :allow_nil=>false, only_integer=>true
end
|
class Employee < ActiveRecord::Base
set_table_name "EMP123"
end
|
The only Object-Relational Mapping burden that the programmer has to bear is
the class => table_name mapping:
1. Your table name must be compatible with the ActiveRecord naming conventions
OR
2. You must define a class for your table and call set_table_name inside it.
That's all!
Primary Keys, Foreign Keys, Unique Keys, Column Names, Column Constraints, Table Constraints,
Referential Constraints...it's all there in your database's information schema. DrySQL
gathers this information through extension APIs on the standard ActiveRecord DB adapters,
so that you don't need to re-define any of it in your application code.
|
|