-
Notifications
You must be signed in to change notification settings - Fork 80
[PATCH] Discriminator type demodulize feature request #27
Comments
The patch at http://github.com/dmgr/dm-core/commit/58017d02987e9fdbae23750c3ab1079f47b3e5c5 looks good as it will allow to implement your specific needs in dm-types which probably is a better place then dm-core, like you said on IRC. by Martin Gamsjaeger (snusnu) |
I created a repo with my custom discriminator types: In the future they can be moved to dm-types if core developers want it. They will work if you will merge my commit, I prefer this one: by Dawid Marcin Grzesiak |
The commit: http://github.com/dmgr/dm-core/commit/83fba6abcf8bdb2e41371c3d20e78a26950c54dc require ’rubygems’
require ’datamapper’
DataMapper.setup(:default, "sqlite3::memory:")
class Car
include DataMapper::Resource
property :id, Serial
@brand = property :brand, String
@name = property :name, String
class Ford < self
class Torino < self
end
end
class Audi < self
class TT < self
end
end
def self.discrimination(record)
case record[@brand]
when "Audi" then if ’TT’ == record[@name]
Audi::TT
else
Audi
end
when "Ford" then if ’Torino’ == record[@name]
Ford::Torino
else
Ford
end
else self
end
end
end
p Car.new(:brand => ’Opel’, :name => ’Astra’) #<Car @id=nil @brand="Opel" @name="Astra">
p Car.new(:brand => ’Ford’, :name => ’K’) #<Car::Ford @id=nil @brand="Ford" @name="K">
p Car.new(:brand => ’Ford’, :name => ’Torino’) #<Car::Ford::Torino @id=nil @brand="Ford" @name="Torino">
p Car.new(:brand => ’Audi’, :name => ’TT’) #<Car::Audi::TT @id=nil @brand="Audi" @name="TT"> Additionally default scopes need to be updated in derived classes. by Dawid Marcin Grzesiak |
Because I need this behavior in my project I had to implement it on v0.10.2 so the solution I use is there: and the code for master branch which I provided before: by Dawid Marcin Grzesiak |
Yeah, I generally like to hold onto adding new features into the core until there is at least a few people asking for them. Even then I think new features should be put into plugins, and stabilized there. Once it gets enough usage we can talk about moving it into the core. I would suggest packaging this up into a plugin, and then seeing if there is any interest on the DataMapper mailing list. by Dan Kubb (dkubb) |
We all know Discriminator type, but what I wish it to support is some kind of demodulize functionality, let's say:
Normally Discriminator type generate this insert for the code presented in the footer:
with :demodulize => true options it would be:
When you use inheritance in the way I presented below (ie. nested classes), the module A will see and recognize A1 and A2 class, even if you don't prefixe them by "VeryLongModule1::VeryLongModule2::A". I know it could be a problem if you inherit from A class in different Object spaces, in this cases Discriminator needs full path to find them, but it depends on programmer to use it right.
Here is a pull request for a path that will open possibility to create custom discriminator types
http://github.com/dmgr/dm-core/commit/58017d02987e9fdbae23750c3ab1079f47b3e5c5
Created by Dawid Marcin Grzesiak - 2010-03-27 09:00:06 UTC
Original Lighthouse ticket: http://datamapper.lighthouseapp.com/projects/20609/tickets/1226
The text was updated successfully, but these errors were encountered: