Class: Cut

Inherits:
Object
  • Object
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 :C < X do
  def x; '{' + super + '}'; end
end

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

To use this in an AOP fashion you can 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 :C < 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 is the basic model of this particluar implementation:

class Klass
  def x; "x"; end
end

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

We cut it like so:

Klass = KlassCut

p Klass.new.x

This is simple and relatvely robust, but not 100% transparent. So we add some redirection methods to the cut to improve the transparency.

Due to limitation in meta-programming Ruby as this level, the transparency isn’t perfect, but it’s fairly close.

Defined Under Namespace

Modules: MetaTransparency, Transparency

Class Method Summary collapse

Class Method Details

.new(klass, &block) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cuts/cut.rb', line 85

def self.new(klass, &block)
  cut = Class.new(klass, &block)  # <-- This is the actual cut.

  #cut.class_eval(&block)

  cut.send(:include, Transparency)
  cut.extend MetaTransparency

  v = $VERBOSE
  $VERBOSE = false
  klass.modspace::const_set(klass.basename, cut)
  $VERBOSE = v

  return cut
end