cuts 1.1.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/HISTORY.rdoc +27 -0
- data/NOTICE.rdoc +3 -3
- data/RCR.textile +12 -10
- data/README.rdoc +88 -33
- data/demo/01_cuts.md +77 -0
- data/demo/02_prepend.md +57 -0
- data/demo/03_aop.md +89 -0
- data/demo/applique/cuts.rb +3 -0
- data/lib/cuts/aop.rb +169 -121
- data/lib/cuts/cut.rb +58 -49
- data/lib/cuts/version.rb +4 -0
- data/lib/cuts.rb +3 -1
- metadata +80 -47
- data/.ruby +0 -52
- data/test/test_aop.rb +0 -51
- data/test/test_cut.rb +0 -87
data/lib/cuts/aop.rb
CHANGED
|
@@ -1,11 +1,31 @@
|
|
|
1
1
|
require 'cuts/cut'
|
|
2
2
|
|
|
3
|
-
require 'pp'
|
|
4
|
-
|
|
5
|
-
# TODO: Can JointPoint and Target be the same class?
|
|
6
|
-
|
|
7
3
|
# Aspect Oriented Programming for Ruby using Cuts.
|
|
8
4
|
#
|
|
5
|
+
# An aspect holds advice, which are its methods, and for each advice the
|
|
6
|
+
# join-points it applies to.
|
|
7
|
+
#
|
|
8
|
+
# LogAspect = Aspect.new do
|
|
9
|
+
# join :log do |jp|
|
|
10
|
+
# jp == :x
|
|
11
|
+
# end
|
|
12
|
+
#
|
|
13
|
+
# def log(target)
|
|
14
|
+
# result = target.super
|
|
15
|
+
# puts "x gave #{result}"
|
|
16
|
+
# result
|
|
17
|
+
# end
|
|
18
|
+
# end
|
|
19
|
+
#
|
|
20
|
+
# class X
|
|
21
|
+
# def x; "x"; end
|
|
22
|
+
# end
|
|
23
|
+
#
|
|
24
|
+
# X.apply(LogAspect)
|
|
25
|
+
#
|
|
26
|
+
# An advice is given a target, and calls <tt>target.super</tt> to go on
|
|
27
|
+
# to the next advice and, in the end, to the method itself.
|
|
28
|
+
#
|
|
9
29
|
class Aspect < Module
|
|
10
30
|
|
|
11
31
|
def initialize(&block)
|
|
@@ -13,10 +33,15 @@ class Aspect < Module
|
|
|
13
33
|
extend self
|
|
14
34
|
end
|
|
15
35
|
|
|
36
|
+
# The join-points of each advice, as a Hash of advice name to a list
|
|
37
|
+
# of matches.
|
|
16
38
|
def points
|
|
17
39
|
@points ||= {}
|
|
18
40
|
end
|
|
19
41
|
|
|
42
|
+
# Join an advice to the methods the block selects. The block is given
|
|
43
|
+
# a Joinpoint, and returns true if the advice applies to it.
|
|
44
|
+
#
|
|
20
45
|
# TODO Should this accept pattern matches as an alternative to the block too?
|
|
21
46
|
# Eg. join(name, pattern=nil, &block)
|
|
22
47
|
def join(name, &block)
|
|
@@ -25,15 +50,36 @@ class Aspect < Module
|
|
|
25
50
|
|
|
26
51
|
end
|
|
27
52
|
|
|
28
|
-
#
|
|
29
|
-
|
|
53
|
+
# A join-point is a call of a method: the object, its class, the method's
|
|
54
|
+
# name and the arguments.
|
|
55
|
+
#
|
|
56
|
+
# TODO: Can JointPoint and Target be the same class?
|
|
30
57
|
class Joinpoint
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
58
|
+
|
|
59
|
+
# The object the method was called on.
|
|
60
|
+
attr_reader :object
|
|
61
|
+
|
|
62
|
+
# The class the aspect was applied to.
|
|
63
|
+
attr_reader :base
|
|
64
|
+
|
|
65
|
+
# The name of the method.
|
|
66
|
+
attr_reader :method
|
|
67
|
+
|
|
68
|
+
# The arguments of the call.
|
|
69
|
+
attr_reader :args
|
|
70
|
+
|
|
71
|
+
# The block is what calls the method itself.
|
|
72
|
+
def initialize(object, base, method, *args, &proceed)
|
|
73
|
+
@object = object
|
|
74
|
+
@base = base
|
|
75
|
+
@method = method
|
|
76
|
+
@args = args
|
|
77
|
+
@proceed = proceed
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# The name of the method.
|
|
81
|
+
def name
|
|
82
|
+
@method.to_sym
|
|
37
83
|
end
|
|
38
84
|
|
|
39
85
|
def ===(match)
|
|
@@ -49,32 +95,16 @@ class Joinpoint
|
|
|
49
95
|
sym.to_sym == @method.to_sym
|
|
50
96
|
end
|
|
51
97
|
|
|
52
|
-
#
|
|
53
|
-
|
|
98
|
+
# Call the method itself.
|
|
54
99
|
def super
|
|
55
|
-
|
|
56
|
-
anc.instance_method(@method).bind(@object).call(*@args, &@block)
|
|
100
|
+
@proceed.call
|
|
57
101
|
end
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
# module LogAspect
|
|
62
|
-
# extend self
|
|
63
|
-
#
|
|
64
|
-
# join :log do |jp|
|
|
65
|
-
# jp.name == :x
|
|
66
|
-
# end
|
|
67
|
-
#
|
|
68
|
-
# def log(target)
|
|
69
|
-
# r = target.super
|
|
70
|
-
# ...
|
|
71
|
-
# return r
|
|
72
|
-
# end
|
|
73
|
-
# end
|
|
74
|
-
#
|
|
75
|
-
# class X
|
|
76
102
|
|
|
103
|
+
alias_method :call, :super
|
|
104
|
+
end
|
|
77
105
|
|
|
106
|
+
# A target is what an advice is given: the next advice in line, or at
|
|
107
|
+
# the end of the line the join-point.
|
|
78
108
|
class Target
|
|
79
109
|
|
|
80
110
|
def initialize(aspect, advice, *target, &block)
|
|
@@ -84,6 +114,7 @@ class Target
|
|
|
84
114
|
@block = block
|
|
85
115
|
end
|
|
86
116
|
|
|
117
|
+
# Call the next advice, or the method itself.
|
|
87
118
|
def super
|
|
88
119
|
@aspect.send(@advice, *@target, &@block)
|
|
89
120
|
end
|
|
@@ -91,111 +122,128 @@ class Target
|
|
|
91
122
|
alias_method :call, :super
|
|
92
123
|
end
|
|
93
124
|
|
|
125
|
+
# The cut that carries a class's aspects. It has a method for each
|
|
126
|
+
# method of the class, those defined later included, which calls the
|
|
127
|
+
# advice that are joined to it and then the method itself.
|
|
128
|
+
#
|
|
129
|
+
# Methods that every object has, those of Object and Kernel, are left
|
|
130
|
+
# alone.
|
|
131
|
+
class CrossCut < Cut
|
|
132
|
+
|
|
133
|
+
def initialize(base)
|
|
134
|
+
@advices = {}
|
|
135
|
+
super(base)
|
|
136
|
+
cross_methods.each{ |sym| cross(sym) }
|
|
137
|
+
watch
|
|
138
|
+
end
|
|
94
139
|
|
|
95
|
-
|
|
140
|
+
# The advice that apply to a method, as a list of aspect and advice
|
|
141
|
+
# name. They are worked out on first use.
|
|
142
|
+
def advices(joinpoint)
|
|
143
|
+
@advices[joinpoint.name] ||= (
|
|
144
|
+
list = []
|
|
145
|
+
base.aspects.each do |aspect|
|
|
146
|
+
aspect.points.each do |advice, matches|
|
|
147
|
+
matches.each do |match|
|
|
148
|
+
list << [aspect, advice] if joinpoint === match
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
list
|
|
153
|
+
)
|
|
154
|
+
end
|
|
96
155
|
|
|
97
|
-
|
|
156
|
+
# Forget which advice apply to which methods. This is done when
|
|
157
|
+
# another aspect is applied.
|
|
158
|
+
def reset
|
|
159
|
+
@advices.clear
|
|
160
|
+
end
|
|
98
161
|
|
|
99
|
-
|
|
100
|
-
|
|
162
|
+
# Define the method that calls a method's advice.
|
|
163
|
+
def cross(sym)
|
|
164
|
+
return if method_defined?(sym, false) || private_method_defined?(sym, false)
|
|
165
|
+
|
|
166
|
+
crosscut = self
|
|
167
|
+
base = self.base
|
|
168
|
+
|
|
169
|
+
visibility = (
|
|
170
|
+
if base.private_method_defined?(sym)
|
|
171
|
+
:private
|
|
172
|
+
elsif base.protected_method_defined?(sym)
|
|
173
|
+
:protected
|
|
174
|
+
else
|
|
175
|
+
:public
|
|
176
|
+
end
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
define_method(sym) do |*args, **opts, &blk|
|
|
180
|
+
jp = Joinpoint.new(self, base, sym, *args){ super(*args, **opts, &blk) }
|
|
181
|
+
|
|
182
|
+
advices = crosscut.advices(jp)
|
|
183
|
+
|
|
184
|
+
if advices.empty?
|
|
185
|
+
super(*args, **opts, &blk)
|
|
186
|
+
else
|
|
187
|
+
target = jp
|
|
188
|
+
advices.each do |(aspect, advice)|
|
|
189
|
+
target = Target.new(aspect, advice, target)
|
|
190
|
+
end
|
|
191
|
+
target.call
|
|
192
|
+
end
|
|
101
193
|
end
|
|
102
194
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
end
|
|
195
|
+
__send__(visibility, sym)
|
|
196
|
+
end
|
|
106
197
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
# calculate advices on first use.
|
|
122
|
-
unless advices[sym]
|
|
123
|
-
advices[sym] = []
|
|
124
|
-
base.aspects.each do |aspect|
|
|
125
|
-
aspect.points.each do |advice, matches|
|
|
126
|
-
matches.each do |match|
|
|
127
|
-
if jp === match
|
|
128
|
-
advices[sym] << [aspect, advice]
|
|
129
|
-
end
|
|
130
|
-
end
|
|
131
|
-
end
|
|
132
|
-
end
|
|
133
|
-
end
|
|
134
|
-
|
|
135
|
-
if advices[sym].empty?
|
|
136
|
-
super(*args, &blk)
|
|
137
|
-
else
|
|
138
|
-
target = jp #Target.new(self, sym, *args, &blk) # Target == JoinPoint ?
|
|
139
|
-
advices[sym].each do |(aspect, advice)|
|
|
140
|
-
target = Target.new(aspect, advice, target)
|
|
141
|
-
end
|
|
142
|
-
target.call #super
|
|
143
|
-
end
|
|
144
|
-
end #define_method
|
|
145
|
-
end #methods
|
|
146
|
-
end #def
|
|
198
|
+
private
|
|
199
|
+
|
|
200
|
+
# The methods of the class to be crossed: its own and those it
|
|
201
|
+
# inherits, short of Object's.
|
|
202
|
+
def cross_methods
|
|
203
|
+
list = []
|
|
204
|
+
base.ancestors.each do |anc|
|
|
205
|
+
break if anc == Object
|
|
206
|
+
next if Cut === anc
|
|
207
|
+
list.concat(anc.instance_methods(false))
|
|
208
|
+
list.concat(anc.private_instance_methods(false))
|
|
209
|
+
end
|
|
210
|
+
list.uniq
|
|
211
|
+
end
|
|
147
212
|
|
|
213
|
+
# Cross methods as they are added to the class.
|
|
214
|
+
def watch
|
|
215
|
+
crosscut = self
|
|
216
|
+
watcher = Module.new do
|
|
217
|
+
define_method(:method_added) do |sym|
|
|
218
|
+
super(sym)
|
|
219
|
+
crosscut.cross(sym) if equal?(crosscut.base)
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
base.singleton_class.__send__(:prepend, watcher)
|
|
148
223
|
end
|
|
149
224
|
|
|
150
225
|
end
|
|
151
226
|
|
|
227
|
+
# Make the cut that carries a class's aspects.
|
|
228
|
+
def cross_cut(klass)
|
|
229
|
+
CrossCut.new(klass)
|
|
230
|
+
end
|
|
152
231
|
|
|
153
232
|
#
|
|
233
|
+
class Module
|
|
154
234
|
|
|
155
|
-
class
|
|
156
|
-
#def cut; @cut; end
|
|
235
|
+
# The aspects applied to this class, the newest first.
|
|
157
236
|
def aspects; @aspects ||= []; end
|
|
158
237
|
|
|
238
|
+
# The cut that carries this class's aspects, if any have been applied.
|
|
239
|
+
attr_reader :cross_cut
|
|
240
|
+
|
|
241
|
+
# Apply an aspect to this class.
|
|
159
242
|
def apply(aspect)
|
|
160
|
-
|
|
161
|
-
cross_cut(self)
|
|
162
|
-
#(class << self;self;end).class_eval do
|
|
163
|
-
# alias_method :__new, :new
|
|
164
|
-
# def new(*args, &block)
|
|
165
|
-
# CrossConcerns.new(self,*args, &block)
|
|
166
|
-
# end
|
|
167
|
-
#end
|
|
168
|
-
end
|
|
243
|
+
@cross_cut ||= CrossCut.new(self)
|
|
169
244
|
aspects.unshift(aspect)
|
|
245
|
+
@cross_cut.reset
|
|
246
|
+
self
|
|
170
247
|
end
|
|
171
248
|
|
|
172
249
|
end
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
=begin demo
|
|
176
|
-
|
|
177
|
-
class X
|
|
178
|
-
def x; "x"; end
|
|
179
|
-
def y; "y"; end
|
|
180
|
-
def q; "<" + x + ">"; end
|
|
181
|
-
end
|
|
182
|
-
|
|
183
|
-
Xa = Aspect.new do
|
|
184
|
-
join :x do |jp|
|
|
185
|
-
jp == :x
|
|
186
|
-
end
|
|
187
|
-
|
|
188
|
-
def x(target); '{' + target.super + '}'; end
|
|
189
|
-
end
|
|
190
|
-
|
|
191
|
-
X.apply(Xa)
|
|
192
|
-
|
|
193
|
-
x1 = X.new
|
|
194
|
-
#print 'X == ' ; p x1
|
|
195
|
-
print 'X == ' ; p x1.class
|
|
196
|
-
print '["q", "y", "x"] == ' ; p x1.public_methods(false)
|
|
197
|
-
print '"{x}" == ' ; p x1.x
|
|
198
|
-
print '"y" == ' ; p x1.y
|
|
199
|
-
print '"<{x}>" == ' ; p x1.q
|
|
200
|
-
|
|
201
|
-
=end
|
data/lib/cuts/cut.rb
CHANGED
|
@@ -1,18 +1,13 @@
|
|
|
1
1
|
# cut.rb
|
|
2
2
|
# Copyright (c) 2005,2008 Thomas Sawyer
|
|
3
3
|
#
|
|
4
|
-
# Ruby License
|
|
5
|
-
#
|
|
6
|
-
# This module is free software. You may use, modify, and/or redistribute this
|
|
7
|
-
# software under the same terms as Ruby.
|
|
8
|
-
#
|
|
9
4
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
10
5
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
11
6
|
# FOR A PARTICULAR PURPOSE.
|
|
12
7
|
|
|
13
8
|
# = Cut
|
|
14
9
|
#
|
|
15
|
-
# Cuts are transparent subclasses.
|
|
10
|
+
# Cuts are transparent subclasses. They are the basis of
|
|
16
11
|
# Cut-based AOP. The general idea of Cut-based AOP is that
|
|
17
12
|
# the Cut can serve a clean container for customized advice on
|
|
18
13
|
# top of which more sophisticated AOP systems can be built.
|
|
@@ -54,73 +49,91 @@
|
|
|
54
49
|
#
|
|
55
50
|
# ACut < AClass < ASuperClass
|
|
56
51
|
#
|
|
57
|
-
# Instantiating AClass
|
|
58
|
-
# but that action is
|
|
52
|
+
# Instantiating AClass effectively instantiates ACut instead,
|
|
53
|
+
# but that action is transparent.
|
|
59
54
|
#
|
|
60
|
-
#
|
|
61
|
-
#
|
|
55
|
+
# A cut is a module that is prepended to its class, with
|
|
56
|
+
# <tt>Module#prepend</tt>. Given the following example:
|
|
62
57
|
#
|
|
63
58
|
# class Klass
|
|
64
59
|
# def x; "x"; end
|
|
65
60
|
# end
|
|
66
61
|
#
|
|
67
|
-
# cut KlassCut < Klass
|
|
62
|
+
# cut :KlassCut < Klass do
|
|
68
63
|
# def x; '{' + super + '}'; end
|
|
69
64
|
# end
|
|
70
65
|
#
|
|
71
|
-
# The effect is
|
|
66
|
+
# The effect is that of:
|
|
72
67
|
#
|
|
73
|
-
#
|
|
74
|
-
#
|
|
68
|
+
# module KlassCut
|
|
69
|
+
# def x; '{' + super + '}'; end
|
|
70
|
+
# end
|
|
75
71
|
#
|
|
76
|
-
#
|
|
72
|
+
# class Klass
|
|
73
|
+
# prepend KlassCut
|
|
74
|
+
# end
|
|
77
75
|
#
|
|
78
|
-
#
|
|
76
|
+
# As it happens, <tt>Module#prepend</tt> is what Ruby took from the
|
|
77
|
+
# proposal for cuts. What a cut adds to it is a name for the idea and
|
|
78
|
+
# a record: a class knows its cuts, and a cut knows what it cuts.
|
|
79
|
+
#
|
|
80
|
+
# Before Ruby had +prepend+, this library gave each new object its
|
|
81
|
+
# class's cuts with +extend+, which missed objects that already
|
|
82
|
+
# existed and those of subclasses.
|
|
79
83
|
|
|
80
84
|
class Cut < Module
|
|
81
|
-
def initialize(klass, &block)
|
|
82
|
-
klass.cuts.unshift(self)
|
|
83
|
-
module_eval(&block)
|
|
84
|
-
end
|
|
85
|
-
end
|
|
86
85
|
|
|
87
|
-
class
|
|
88
|
-
|
|
89
|
-
|
|
86
|
+
# Make a cut of a class or module. The block is the body of the cut.
|
|
87
|
+
#
|
|
88
|
+
# Cut.new(X) do
|
|
89
|
+
# def x; '{' + super + '}'; end
|
|
90
|
+
# end
|
|
91
|
+
#
|
|
92
|
+
def initialize(base, &block)
|
|
93
|
+
@base = base
|
|
94
|
+
super(&block)
|
|
95
|
+
base.cuts.unshift(self)
|
|
96
|
+
base.__send__(:prepend, self)
|
|
90
97
|
end
|
|
91
|
-
end
|
|
92
98
|
|
|
93
|
-
class
|
|
94
|
-
|
|
95
|
-
alias_method :_new, :new
|
|
99
|
+
# The class or module this cut is cut across.
|
|
100
|
+
attr_reader :base
|
|
96
101
|
|
|
97
|
-
|
|
98
|
-
o = _new(*a, &b)
|
|
99
|
-
if !cuts.empty?
|
|
100
|
-
o.extend *cuts
|
|
101
|
-
end
|
|
102
|
-
o
|
|
103
|
-
end
|
|
102
|
+
end
|
|
104
103
|
|
|
104
|
+
class Module
|
|
105
|
+
# The cuts of this class or module, the newest first, which is the
|
|
106
|
+
# order in which they are called.
|
|
107
|
+
def cuts
|
|
108
|
+
@cuts ||= []
|
|
105
109
|
end
|
|
106
110
|
end
|
|
107
111
|
|
|
108
112
|
class Symbol
|
|
109
|
-
#
|
|
110
|
-
|
|
111
|
-
#
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
113
|
+
# A little trick to simulate the literal syntax of subclassing,
|
|
114
|
+
# <tt>cut :Name < Klass</tt>. It is itself a cut of Symbol, so the
|
|
115
|
+
# comparison of two symbols is as it was.
|
|
116
|
+
Cut.new(self) do
|
|
117
|
+
def <(other)
|
|
118
|
+
if Module === other
|
|
119
|
+
[self, other]
|
|
120
|
+
else
|
|
121
|
+
super
|
|
122
|
+
end
|
|
118
123
|
end
|
|
119
124
|
end
|
|
120
125
|
end
|
|
121
126
|
|
|
122
127
|
module Kernel
|
|
123
|
-
# Cut
|
|
128
|
+
# Cut convenience method.
|
|
129
|
+
#
|
|
130
|
+
# cut :Z < X do
|
|
131
|
+
# def x; '{' + super + '}'; end
|
|
132
|
+
# end
|
|
133
|
+
#
|
|
134
|
+
# With a name, the cut is made a constant of the module it is defined
|
|
135
|
+
# in, or of Object at the toplevel. Without one, as in <tt>cut(X)</tt>,
|
|
136
|
+
# it is anonymous.
|
|
124
137
|
def cut(klass, &block)
|
|
125
138
|
case klass
|
|
126
139
|
when Array
|
|
@@ -131,13 +144,9 @@ module Kernel
|
|
|
131
144
|
|
|
132
145
|
cut = Cut.new(klass, &block)
|
|
133
146
|
|
|
134
|
-
# How to handle main, but not other instance spaces?
|
|
135
|
-
#klass.modspace::const_set(klass.basename, cut)
|
|
136
147
|
mod = (Module === self ? self : Object)
|
|
137
148
|
mod.const_set(name, cut) if name # <<- this is what we don't have in Cut.new
|
|
138
149
|
|
|
139
150
|
return cut
|
|
140
151
|
end
|
|
141
152
|
end
|
|
142
|
-
|
|
143
|
-
|
data/lib/cuts/version.rb
ADDED
data/lib/cuts.rb
CHANGED