RubyFlow The Ruby and Rails community linklog

Hooks and callbacks done the simple way.

A small post how to use hooks, a new gem on the block. It shows how simple hooks and callbacks (like a before_filter) can be implemented or how you can improve your application code.

Comments

Pretty nice implementation. Hooking techniques give much more power than before/after callbacks as hooks may be injected somewhere in the middle of the method. Your Hooks would be perfect in case when we need something more powerful than filters and yield. Only complaint about name of the gem - you would really use something more fancy than Hooks ;)

Exactly, filters are like a subset of hooks. You can easily implement filters with hooks, but not all kind of hooks with filters. So- fuck filters.

Yeah, the gem should’ve been named hooker, I know…

Nice ! Very straightforward. Does it comes with the price of installing ActiveSupport because of the requirement of inheritable_attributes.rb ?

Internally it uses Class.class_inheritable_array for handling inheritance of hooks. So you need ActiveSupport. Is that ouch?

A funny note: In Rails (even 3.0) and ActiveSupport::Callbacks they put up a separate class-attribute inheritance thing instead of using their own class_inheritable_array. That’s more than 40 LOCs for nothing.

In a Rails context it’s definitely not something to think about - looking at a small Sinatra project it could be.

I am a strong advocate of the kiss and yagni principle. Maybe it’s nitpicking, but you have this fine, small piece of code and it has a dependency which is so much larger and only a minimal part of it is needed.

P.S. The note is an interesting insight.

So, is the problem the class_inheritable_array? Everybody’s complaining about it, so maybe we should push that into the gem itself?

I personally hate doing things twice but ActiveSupport doesn’t seem to be that popular…

ActiveSupport sucks and monkey craps on too many classes

aspect in Java

Post a comment

You can use basic HTML markup (e.g. <a>) or Markdown.

As you are not logged in, you will be
directed via GitHub to signup or sign in