dm-reflection
For now, this gem only allows you to print the ruby source for any DataMapper::Model, taking all property and relationship definitions into account. However, the plan is to be able to reflect on an existing relational database schema and export the corresponding datamapper model definitions to ruby source files. Think of it as active_record and annotate_models combined to do something useful with legacy databases.
require 'rubygems'
require 'dm-core'
require 'dm-reflection'
class Project
include DataMapper::Resource
property :id, Serial
property :name, String, :required => true, :length => 200
property :created_at, DateTime
property :updated_at, DateTime
has 0..n, :project_tasks
has 0..n, :tasks, :through => :project_tasks
has 0..n, :people, :through => Resource
end
Project.to_ruby # produces this nicely formatted ruby code
<<-RUBY
class Project
include DataMapper::Resource
property :id, Serial
property :name, String, :required => true, :length => 200
property :created_at, DateTime
property :updated_at, DateTime
has 0..n, :project_tasks
has 0..n, :tasks, :through => :project_tasks
has 0..n, :people, :through => Resource
end
RUBY
Also, you can create models from a repository by doing the following:
require 'dm-reflection'
models = DataMapper::Reflection.reflect(:default)
Note on Patches/Pull Requests
-
Fork the project.
-
Make your feature addition or bug fix.
-
Add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
-
Send me a pull request. Bonus points for topic branches.
Future Work
-
Support Associations/Relationships
-
Support Single Table Inheritance
-
Ensure Round tripping is functional ** Create model M in DataMapper ** Migrate M to a repository ** Reflect M’ from the repository ** M should == M’
-
Having round tripping provably working is the first goal
-
Legacy reflection is something to make work after round tripping (because the assumptions DM makes won’t always hold.)
Copyright
See LICENSE for details. Copyright © 2009 Martin Gamsjaeger (snusnu). Copyright © 2010 Montana State University, Bozeman (The Yogo Data Management Team).