Hooks goes without ActiveSupport and still has inheritable attributes

The ongoing discussions about ActiveSupport in Hooks lead me to believe that ActiveSupport is not as popular as i thought it is.

Don’t get me wrong, I didn’t get misleaded by comments like

ActiveSupport sucks and monkey craps on too many classes

although some of this is true šŸ˜€ It has wonderful helper methods and there’s a lot of work done for you already, but ActiveSupport is simply too complex.

After I found out that Class.class_inheritable_array fails when doing

require "activesupport/core_ext/class/inheritable_attributes"

with some undefined method exception (at least with 2.3.9), I finally decided to do it on my own.

Hooks::InheritableAttribute

The current implementation is simple.

You use it like

class Cat
  extend Hooks::InheritableAttribute
  inheritable_attr :lifes
  self.lifes = []

and in subclasses

class Garfield < Cat
  self.lifes << "second"

so it is like ActiveSupport’s class_inheritable_array. Less code, of course.

Where to go?

Not sure if methods like that should be packaged in separate gems? Or in a more generic gem aggregating features?

I mean, it would end up in some big PassiveLaziness gem and we’d have two mainlines of handy ruby feature libraries.

So where to put such code? How can we make it modular and easily useable without automatically monkey patching every second class, as ActiveSupport does?

Any thoughts welcome!

Advertisement

5 thoughts on “Hooks goes without ActiveSupport and still has inheritable attributes

  1. I see no problem with ActiveSupport 3. While prior versions really cluttered object space (although except for some cases, I don’t even see problem with that too), AS3 is modular and allows you to use only extensions you want.

    Like

  2. I like the new version !
    It really fits into the rest of your clear, straightforward code.

    I’d suggest you let it stay that way for a while.
    It’s a helpful peace of code and and as a developer I can clearly see what’s going on, without wasting hours to explore.
    It works on 2.3.9 and 3.0.0 an the practical usage in applications will give you feedback for future directions.
    It’s good the way it is now!

    Like

  3. In a case like this, where you need but one small method for one particular purpose within your own code, just add the method directly — to Hooks::ClassMethods in this case. No need for a dependency or and extra module.

    P.S. Your mail field is buggy.

    Like

  4. I’m trying to use hooks with Mixins. My setup is something like:

    class A
    include Hooks
    end

    module X
    define_hook :bob
    end

    class B < A
    include X
    bob do
    puts “BOB@!
    end
    end

    Unfortunately this does not work. Any hints??

    Like

  5. @Peter: This won’t work as you have to delay the define_hook block until the module is included.

    module X
      extend ActiveSupport::Concern
      included do |base|
        base.define_hook :bob
      end
    end
    

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s