cuts 1.0.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 +84 -0
- data/NOTICE.rdoc +33 -0
- data/RCR.textile +459 -0
- data/README.rdoc +137 -0
- 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 +160 -118
- data/lib/cuts/cut.rb +58 -49
- data/lib/cuts/version.rb +4 -0
- data/lib/cuts.rb +3 -1
- metadata +116 -73
- data/HISTORY +0 -18
- data/LICENSE +0 -168
- data/MANIFEST +0 -23
- data/README +0 -47
- data/RELEASE +0 -24
- data/Rakefile +0 -10
- data/meta/abstract +0 -3
- data/meta/contact +0 -1
- data/meta/created +0 -1
- data/meta/homepage +0 -1
- data/meta/license +0 -1
- data/meta/package +0 -1
- data/meta/project +0 -1
- data/meta/summary +0 -1
- data/meta/version +0 -1
- data/test/test_aop.rb +0 -49
- data/test/test_cut.rb +0 -85
data/lib/cuts/aop.rb
CHANGED
|
@@ -1,22 +1,31 @@
|
|
|
1
|
-
|
|
1
|
+
require 'cuts/cut'
|
|
2
|
+
|
|
3
|
+
# Aspect Oriented Programming for Ruby using Cuts.
|
|
2
4
|
#
|
|
3
|
-
#
|
|
5
|
+
# An aspect holds advice, which are its methods, and for each advice the
|
|
6
|
+
# join-points it applies to.
|
|
4
7
|
#
|
|
5
|
-
#
|
|
8
|
+
# LogAspect = Aspect.new do
|
|
9
|
+
# join :log do |jp|
|
|
10
|
+
# jp == :x
|
|
11
|
+
# end
|
|
6
12
|
#
|
|
13
|
+
# def log(target)
|
|
14
|
+
# result = target.super
|
|
15
|
+
# puts "x gave #{result}"
|
|
16
|
+
# result
|
|
17
|
+
# end
|
|
18
|
+
# end
|
|
7
19
|
#
|
|
8
|
-
#
|
|
20
|
+
# class X
|
|
21
|
+
# def x; "x"; end
|
|
22
|
+
# end
|
|
9
23
|
#
|
|
10
|
-
#
|
|
24
|
+
# X.apply(LogAspect)
|
|
11
25
|
#
|
|
12
|
-
#
|
|
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.
|
|
13
28
|
#
|
|
14
|
-
# - Can JointPoint and Target be the same class?
|
|
15
|
-
|
|
16
|
-
require 'cuts/cut'
|
|
17
|
-
|
|
18
|
-
#
|
|
19
|
-
|
|
20
29
|
class Aspect < Module
|
|
21
30
|
|
|
22
31
|
def initialize(&block)
|
|
@@ -24,10 +33,15 @@ class Aspect < Module
|
|
|
24
33
|
extend self
|
|
25
34
|
end
|
|
26
35
|
|
|
36
|
+
# The join-points of each advice, as a Hash of advice name to a list
|
|
37
|
+
# of matches.
|
|
27
38
|
def points
|
|
28
39
|
@points ||= {}
|
|
29
40
|
end
|
|
30
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
|
+
#
|
|
31
45
|
# TODO Should this accept pattern matches as an alternative to the block too?
|
|
32
46
|
# Eg. join(name, pattern=nil, &block)
|
|
33
47
|
def join(name, &block)
|
|
@@ -36,15 +50,36 @@ class Aspect < Module
|
|
|
36
50
|
|
|
37
51
|
end
|
|
38
52
|
|
|
39
|
-
#
|
|
40
|
-
|
|
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?
|
|
41
57
|
class Joinpoint
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
|
48
83
|
end
|
|
49
84
|
|
|
50
85
|
def ===(match)
|
|
@@ -60,32 +95,16 @@ class Joinpoint
|
|
|
60
95
|
sym.to_sym == @method.to_sym
|
|
61
96
|
end
|
|
62
97
|
|
|
63
|
-
#
|
|
64
|
-
|
|
98
|
+
# Call the method itself.
|
|
65
99
|
def super
|
|
66
|
-
|
|
67
|
-
anc.instance_method(@method).bind(@object).call(*@args, &@block)
|
|
100
|
+
@proceed.call
|
|
68
101
|
end
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
# module LogAspect
|
|
73
|
-
# extend self
|
|
74
|
-
#
|
|
75
|
-
# join :log do |jp|
|
|
76
|
-
# jp.name == :x
|
|
77
|
-
# end
|
|
78
|
-
#
|
|
79
|
-
# def log(target)
|
|
80
|
-
# r = target.super
|
|
81
|
-
# ...
|
|
82
|
-
# return r
|
|
83
|
-
# end
|
|
84
|
-
# end
|
|
85
|
-
#
|
|
86
|
-
# class X
|
|
87
102
|
|
|
103
|
+
alias_method :call, :super
|
|
104
|
+
end
|
|
88
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.
|
|
89
108
|
class Target
|
|
90
109
|
|
|
91
110
|
def initialize(aspect, advice, *target, &block)
|
|
@@ -95,6 +114,7 @@ class Target
|
|
|
95
114
|
@block = block
|
|
96
115
|
end
|
|
97
116
|
|
|
117
|
+
# Call the next advice, or the method itself.
|
|
98
118
|
def super
|
|
99
119
|
@aspect.send(@advice, *@target, &@block)
|
|
100
120
|
end
|
|
@@ -102,106 +122,128 @@ class Target
|
|
|
102
122
|
alias_method :call, :super
|
|
103
123
|
end
|
|
104
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
|
|
105
139
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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
|
|
111
155
|
|
|
112
|
-
|
|
113
|
-
|
|
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
|
|
114
161
|
|
|
115
|
-
|
|
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)
|
|
116
165
|
|
|
117
|
-
|
|
166
|
+
crosscut = self
|
|
167
|
+
base = self.base
|
|
118
168
|
|
|
119
|
-
|
|
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
|
+
)
|
|
120
178
|
|
|
121
|
-
|
|
122
|
-
|
|
179
|
+
define_method(sym) do |*args, **opts, &blk|
|
|
180
|
+
jp = Joinpoint.new(self, base, sym, *args){ super(*args, **opts, &blk) }
|
|
123
181
|
|
|
124
|
-
|
|
125
|
-
unless advices[sym]
|
|
126
|
-
advices[sym] = []
|
|
127
|
-
base.aspects.each do |aspect|
|
|
128
|
-
aspect.points.each do |advice, matches|
|
|
129
|
-
matches.each do |match|
|
|
130
|
-
if jp === match
|
|
131
|
-
advices[sym] << [aspect, advice]
|
|
132
|
-
end
|
|
133
|
-
end
|
|
134
|
-
end
|
|
135
|
-
end
|
|
136
|
-
end
|
|
137
|
-
|
|
138
|
-
if advices[sym].empty?
|
|
139
|
-
super
|
|
140
|
-
else
|
|
141
|
-
target = jp #Target.new(self, sym, *args, &blk) # Target == JoinPoint ?
|
|
142
|
-
advices[sym].each do |(aspect, advice)|
|
|
143
|
-
target = Target.new(aspect, advice, target)
|
|
144
|
-
end
|
|
145
|
-
target.super
|
|
146
|
-
end
|
|
182
|
+
advices = crosscut.advices(jp)
|
|
147
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)
|
|
148
190
|
end
|
|
149
|
-
|
|
191
|
+
target.call
|
|
150
192
|
end
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
__send__(visibility, sym)
|
|
196
|
+
end
|
|
151
197
|
|
|
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))
|
|
152
209
|
end
|
|
210
|
+
list.uniq
|
|
211
|
+
end
|
|
153
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)
|
|
154
223
|
end
|
|
155
224
|
|
|
156
225
|
end
|
|
157
226
|
|
|
227
|
+
# Make the cut that carries a class's aspects.
|
|
228
|
+
def cross_cut(klass)
|
|
229
|
+
CrossCut.new(klass)
|
|
230
|
+
end
|
|
158
231
|
|
|
159
232
|
#
|
|
233
|
+
class Module
|
|
160
234
|
|
|
161
|
-
class
|
|
162
|
-
#def cut; @cut; end
|
|
235
|
+
# The aspects applied to this class, the newest first.
|
|
163
236
|
def aspects; @aspects ||= []; end
|
|
164
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.
|
|
165
242
|
def apply(aspect)
|
|
166
|
-
|
|
167
|
-
cross_cut(self)
|
|
168
|
-
#(class << self;self;end).class_eval do
|
|
169
|
-
# alias_method :__new, :new
|
|
170
|
-
# def new(*args, &block)
|
|
171
|
-
# CrossConcerns.new(self,*args, &block)
|
|
172
|
-
# end
|
|
173
|
-
#end
|
|
174
|
-
end
|
|
243
|
+
@cross_cut ||= CrossCut.new(self)
|
|
175
244
|
aspects.unshift(aspect)
|
|
245
|
+
@cross_cut.reset
|
|
246
|
+
self
|
|
176
247
|
end
|
|
177
248
|
|
|
178
249
|
end
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
=begin demo
|
|
182
|
-
|
|
183
|
-
class X
|
|
184
|
-
def x; "x"; end
|
|
185
|
-
def y; "y"; end
|
|
186
|
-
def q; "<" + x + ">"; end
|
|
187
|
-
end
|
|
188
|
-
|
|
189
|
-
Xa = Aspect.new do
|
|
190
|
-
join :x do |jp|
|
|
191
|
-
jp == :x
|
|
192
|
-
end
|
|
193
|
-
|
|
194
|
-
def x(target); '{' + target.super + '}'; end
|
|
195
|
-
end
|
|
196
|
-
|
|
197
|
-
X.apply(Xa)
|
|
198
|
-
|
|
199
|
-
x1 = X.new
|
|
200
|
-
#print 'X == ' ; p x1
|
|
201
|
-
print 'X == ' ; p x1.class
|
|
202
|
-
print '["q", "y", "x"] == ' ; p x1.public_methods(false)
|
|
203
|
-
print '"{x}" == ' ; p x1.x
|
|
204
|
-
print '"y" == ' ; p x1.y
|
|
205
|
-
print '"<{x}>" == ' ; p x1.q
|
|
206
|
-
|
|
207
|
-
=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