Prev | Current Page 53 | Next

Brad Ediger

"Advanced Rails"

Not only would the code be complex, it would be tightly coupled to either the
existing code or the code that calls it.
Aspect-oriented programming
Aspect-oriented programming (AOP) is one technique that attempts to solve the
issues of cross-cutting concerns. There has been much talk about the applicability of
AOPto Ruby, since many of the advantages that AOPprovides can already be
Metaprogramming Techniques | 33
obtained through metaprogramming. There is a Ruby proposal for cut-based AOP,*
but it may be months or years before this is incorporated.
In cut-based AOP, cuts are sometimes called ???transparent subclasses??? because they
extend a class??™s functionality in a modular way. Cuts act as subclasses but without
the need to instantiate the subclass rather than the parent class.
The Ruby Facets library (facets.rubyforge.org) includes a pure-Ruby cut-based AOP
library.??  It has some limitations due to being written purely in Ruby, but the usage is
fairly clean:
class Person
def say_hi
puts "Hello!"
end
end
cut :Tracer < Person do
def say_hi
puts "Before method"
super
puts "After method"
end
end
Person.new.say_hi
# >> Before method
# >> Hello!
# >> After method
Here we see that the Tracer cut is a transparent subclass: when we create an instance
of Person, it is affected by Tracer without having to know about Tracer. We can also
change Person#say_hi without disrupting our cut.


Pages:
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65