[Nitro] Nice for facets

TRANS transfire at gmail.com
Wed Jun 28 13:10:50 EDT 2006


On 6/26/06, George Moschovitis <george.moschovitis at gmail.com> wrote:
> Tom,
>
> this may be a worthwhile addition to facets (perhaps with a better name):
>
> http://weblog.rubyonrails.com/2006/04/26/new-in-rails-module-alias_method_chain/

Okay, so:

   alias_method :render_without_layout, :render
   alias_method :render, :render_with_layout

can be written:

  alias_method_chain :render, :layout

Quite interesting. So you use could use it like so:

  class Base
    include Layout
    alias_method_chain :render, :layout
  end

Which assumes Base has a method called #render and that Layout has a
method  called #render_with_layout which calls #render_without_layout.
But of course you don't really want to do this alias manually, it
should be automatic on #include, which means the only way for this to
work is via the #included callback. Is that right?

Okay, I feel two ways about this. 1) I lik eit becuase it's clever and
simple, but 2) I hate it because it's tricky and chincy --chincy AOP
that is.

Yet it gives me an idea.  If we want AOP like this maybe we can do
something a little more versitle.... let me work on it....

Okay, I worked out a soluton but the interface can be done in a few
ways. The first way allows advice to be mixed in with other module
features. This is patterned after #class_extension. Here's an example:

  module A
    # ... normal stuff ...
    advice do
      def render( target )
         ... do stuff ...
         target.super
         ... do stuff ...
      end
    end
  end

  class Base
    include Layout
  end

The second way doesn't allow mixing of feature types. I.e. a module is
*declared* an aspect. But maybe that's more approriate for aspects b/c
they are all about SOC.

  module Layout
    is Aspect

    def render( target )
       ... do stuff ...
       target.super
       ... do stuff ...
    end

  end

  class Base
    include Layout
  end

Alternately, the 'is Aspect' could be left out and 'prepend Layout'
could be used instead of 'include Layout'.

Either of these would satisfy the need more generally since the
implementation I devised is true AOP around advice interception.

General thoughts?

Whether we should still offer #alias_method_chain or not I will
include this aspect feature in a future version of Facets. So which
interface is better?

T.


More information about the Nitro-general mailing list