Introducing Schizo, A DCI gem

Schizo - DCI (data, context and interaction) for Ruby / Rails /
ActiveRecord.

I’ve been interested in DCI, but was disappointed when trying to use it
in a typical Rails app (one that uses ActiveRecord). Often times,
extending a record with methods is not enough; you want to extend a
record with associations, validations, callbacks, etc. Unfortunately
that information exists in the record’s class… so if you modify it,
you modify it for all records of that type. Schizo offers a way to
get around that problem.

Please take a look: https://github.com/cjbottaro/schizo

The README.rdoc contains a clear an concrete example of the problem
Schizo is trying to solve.

Thanks!

Fantastic to see some work being done on this, kudos to you! I have a
dumb
question. In the example in the readme, why can’t you just do
object.singleton_class.class_eval instead of object.class.class_eval?
That
should make the changes only affect that object, right?

Andrew W. wrote in post #1045018:

Fantastic to see some work being done on this, kudos to you! I have a
dumb
question. In the example in the readme, why can’t you just do
object.singleton_class.class_eval instead of object.class.class_eval?
That
should make the changes only affect that object, right?

Hehe, I think I should add this to the README…

Originally, that’s how I tried to use DCI with ActiveRecord. I tried
something like this:

module Poster
def self.extended(object)
object.singleton_class.class_eval do
after_save do
update_posts_or_something
end
end
end

def make_post
# blah, do something
end
end

user = User.find(1)
user.extend(Poster)
user.name = “Callie”
user.save

Well, it doesn’t work. ActiveSupport’s callbacks work by looking at the
object’s class for any defined callbacks, it never looks at the
singleton class.

The good news is that a single, one line change to ActiveSupport fixes
the problem. The bad news is that any object that has a singleton class
defined can’t be marshaled. The Rails core team doesn’t like that (a
long while ago I submitted a patch that passed all tests, but created
singleton classes on ActiveRecord instances… they accepted it
initially, but then backed it out once they found out about the
Marshaling issue).

I think one of the biggest downsides to not being able to marshal an
object is that you can’t cache it.

Ok, so regardless of the marshaling issue, let’s say getting callbacks
to work is as simple as that one line change to ActiveSupport. Great,
let’s move on to associations…

Long story short, it’s far from a one line change. Associations are
complicated, messy and magical.

I gave up trying to get object.singleton_class.class_eval(…) to work
with AS/AR and chose the approach you see in Schizo.

– C

On Fri, Feb 10, 2012 at 2:17 AM, Christopher J. B. [email protected]
wrote:

Please take a look: https://github.com/cjbottaro/schizo

+1. you should teamup w trans.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs