Giter Site home page Giter Site logo

Comments (5)

solnic avatar solnic commented on July 23, 2024

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)

from dm-types.

solnic avatar solnic commented on July 23, 2024

I created a repo with my custom discriminator types:
http://github.com/dmgr/dmg-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:
http://github.com/dmgr/dm-core/commit/6af8cab37dfeb7aa1b0230399fddf239e6aac590

by Dawid Marcin Grzesiak

from dm-types.

solnic avatar solnic commented on July 23, 2024

The commit: http://github.com/dmgr/dm-core/commit/83fba6abcf8bdb2e41371c3d20e78a26950c54dc
will let you do something like this:

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 &rsquo;TT&rsquo; == record[@name]
          Audi::TT
        else
          Audi
        end
      when "Ford" then if &rsquo;Torino&rsquo; == record[@name]
          Ford::Torino
        else
          Ford
        end
      else self
    end
  end
end

p Car.new(:brand => &rsquo;Opel&rsquo;, :name => &rsquo;Astra&rsquo;)  #<Car @id=nil @brand="Opel" @name="Astra">
p Car.new(:brand => &rsquo;Ford&rsquo;, :name => &rsquo;K&rsquo;)      #<Car::Ford @id=nil @brand="Ford" @name="K">
p Car.new(:brand => &rsquo;Ford&rsquo;, :name => &rsquo;Torino&rsquo;) #<Car::Ford::Torino @id=nil @brand="Ford" @name="Torino">
p Car.new(:brand => &rsquo;Audi&rsquo;, :name => &rsquo;TT&rsquo;)     #<Car::Audi::TT @id=nil @brand="Audi" @name="TT">

Additionally default scopes need to be updated in derived classes.

by Dawid Marcin Grzesiak

from dm-types.

solnic avatar solnic commented on July 23, 2024

Because I need this behavior in my project I had to implement it on v0.10.2 so the solution I use is there:
http://github.com/dmgr/dm-core/tree/my0.10.2

and the code for master branch which I provided before:
http://github.com/dmgr/dm-core
is not the actual one.

by Dawid Marcin Grzesiak

from dm-types.

solnic avatar solnic commented on July 23, 2024

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)

from dm-types.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.