active_helper
active_helper copied to clipboard
Finally - helpers with proper encapsulation, delegation, interfaces and inheritance!
h1. ActiveHelper
Finally - helpers with proper encapsulation, delegation, interfaces and inheritance!
h2. Introduction
Helpers suck. They've always sucked, and they will suck on if we keep them in modules.
ActiveHelper is an attempt to pack helpers into classes. This brings us a few benefits
- inheritance helpers can be derived other helpers
- delegation helpers are no longer mixed into a target- the targets @import@ the helper, where the new methods are delegated to the helper instances
- proper encapsulation helpers don't rely blindly on instance variables - a helper defines its @needs@, the target has to provide readers
- interfaces a helper clearly @provides@ methods and might @import@ additional helpers
Note that ActiveHelper is a generic helper framework. Not coupled to anything like Rails or Merb. Not providing any concrete helpers. Feel free to use clean helpers in any framework (including Rails and friends)!
h2. Installation
> gem install active_helper
h2. Example
Let's use the bloody MVC-View example as we find in Rails or Merb (Sinatra, too?).
We have a view which needs additional methods in order to render bullshit.
h3. Using helpers
The view wants to render tags using the TagHelper.
class View include ActiveHelper end > view = View.new > view.import TagHelper
To pull-in a helper we invoke @import@ on the target instance.
h3. Interfaces
The exemplary #tag method took me days to implement.
class TagHelper " end end
The helper defines a part of its interface (what goes out) as it @provides@ methods.
> view.tag(:form) # => "