Class: Cut

Inherits:
Module show all
Defined in:
lib/cuts/cut.rb

Overview

Cut

Cuts are transparent subclasses. Thay are the basis of Cut-based AOP. The general idea of Cut-based AOP is that the Cut can serve a clean container for customized advice on top of which more sophisticated AOP systems can be built.

Examples

The basic usage is:

class X
  def x; "x"; end
end

cut :Z < X do
  def x; '{' + super + '}'; end
end

X.new.x  #=> "{x}"

One way to use this in an AOP fashion is to define an aspect as a class or function module, and tie it together with the Cut.

module LogAspect
  extend self
  def log(meth, result)
    ...
  end
end

cut :Z < X do
  def x
    LogAspect.log(:x, r = super)
    return r
  end
end

Implementation

Cuts act as a “pre-class”. Which depictively is:

ACut < AClass < ASuperClass

Instantiating AClass effecively instantiates ACut instead, but that action is effectively transparent.

This particular implementation create a module for each cut and extends objects as they are created. Given the following example:

class Klass
  def x; "x"; end
end

cut KlassCut < Klass
  def x; '{' + super + '}'; end
end

The effect is essentially:

k = Klass.new
k.extend KlassCut

p k.x

The downside to this approach is a limitation in dynamicism.

Instance Method Summary collapse

Constructor Details

#initialize(klass, &block) ⇒ Cut

Returns a new instance of Cut.



81
82
83
84
# File 'lib/cuts/cut.rb', line 81

def initialize(klass, &block)
  klass.cuts.unshift(self)
  module_eval(&block)
end