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
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: a9d41c71514d7b809e1684b205d0076104bc120a3565218d9712985b148299fc
|
|
4
|
+
data.tar.gz: 8fe1162cee69483551ae38f1ca4e29dbbc6c7d1f6355e1e38a5032c8554737d8
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 628c992141197b4607af688f13643e53a71bddefde2b8fcddeabd471aaa7808b78d6e56a695652134c96d3fbd9e390acc5c2c4f35d690453c3e71725c816a7c6
|
|
7
|
+
data.tar.gz: 1596c89bb8ce7b45852d0a1a3fcb0ab0f52ba9cad6112781ea9a0d41571b3343a2f16895fa3204a489b11896303d7dff8fc73cf3b85b2dfbd61cf8ff5d400e57
|
data/HISTORY.rdoc
CHANGED
|
@@ -1,5 +1,32 @@
|
|
|
1
1
|
= Release History
|
|
2
2
|
|
|
3
|
+
== 2.0.0 / 2026-11-30
|
|
4
|
+
|
|
5
|
+
A cut is now a module prepended to its class, with Module#prepend,
|
|
6
|
+
which is what Ruby took from the proposal for cuts. Before, each
|
|
7
|
+
object was extended with its class's cuts as it was made. The effect
|
|
8
|
+
is that cuts are now fully dynamic again: a cut applies to objects
|
|
9
|
+
made before it was, and to those of subclasses. Cuts needs Ruby 3.1
|
|
10
|
+
or newer.
|
|
11
|
+
|
|
12
|
+
Changes:
|
|
13
|
+
|
|
14
|
+
* Build cuts on Module#prepend.
|
|
15
|
+
* Object.new is no longer overridden.
|
|
16
|
+
* Add Cut#base, the class a cut is cut across.
|
|
17
|
+
* Modules can be cut as well as classes.
|
|
18
|
+
* The comparison of two symbols with `<` works again.
|
|
19
|
+
* Aspects apply to objects made before they were applied.
|
|
20
|
+
* Aspects apply to methods defined after they were applied.
|
|
21
|
+
* Advised methods pass on keyword arguments and blocks.
|
|
22
|
+
* Advised private and protected methods stay so.
|
|
23
|
+
* Aspects no longer wrap the methods of Object and Kernel.
|
|
24
|
+
* Add CrossCut, the cut that carries a class's aspects, and
|
|
25
|
+
Module#cross_cut.
|
|
26
|
+
* Add Joinpoint#name, as the old documentation had it.
|
|
27
|
+
* Tell the history of cuts and prepend in the README.
|
|
28
|
+
* Add QED demonstrations; tests run on Spectrum.
|
|
29
|
+
|
|
3
30
|
== 1.1.0 / 2011-10-24
|
|
4
31
|
|
|
5
32
|
This release modernizes the build configuration and
|
data/NOTICE.rdoc
CHANGED
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
== Cuts
|
|
4
4
|
|
|
5
|
-
Copyright:: (c) 2007 Thomas Sawyer, Peter
|
|
5
|
+
Copyright:: (c) 2007 Thomas Sawyer, Peter Vanbroekhoven
|
|
6
6
|
License:: BSD-2-Clause
|
|
7
|
-
Website::
|
|
7
|
+
Website:: https://github.com/rubyworks/cuts
|
|
8
8
|
|
|
9
9
|
Copyright (c) 2007 Thomas Sawyer
|
|
10
|
-
Copyright (c) 2007 Peter
|
|
10
|
+
Copyright (c) 2007 Peter Vanbroekhoven.
|
|
11
11
|
All rights reserved.
|
|
12
12
|
|
|
13
13
|
Redistribution and use in source and binary forms, with or without
|
data/RCR.textile
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
h1. Cut-
|
|
1
|
+
h1. Cut-based AOP
|
|
2
2
|
|
|
3
|
-
by Trans and Peter Vanbroekhoven (rev.
|
|
3
|
+
by Trans and Peter Vanbroekhoven (rev. 71)
|
|
4
4
|
|
|
5
5
|
<a href="http://creativecommons.org/licenses/by-sa/3.0/">© CA BY-SA</a>
|
|
6
6
|
|
|
@@ -11,8 +11,6 @@ This RCR presents <i>cut-based</i> AOP, an efficient and easy-to-use approach to
|
|
|
11
11
|
|
|
12
12
|
The work herein is the culmination of multi-year discussion and inquiry on the topic of AOP for Ruby. It has been carried-out with the ultimate hope of establishing Ruby as a premier AOP language, if not <i>the</i> AOP language of choice. Since AOP is a very powerful paradigm for abstracting programming solutions into <i>separate concerns</i>, and shows great promise for improvements in <i>code maintenance</i> and <i>reusability</i>, it seems only natural that an agile language such as Ruby could provide strong support for this increasing popular pattern of design.
|
|
13
13
|
|
|
14
|
-
IMPORTANT! This is a new edition of Cut-based AOP and is different from previous editions. The significant change, is that it simplifies the definition of a Cut to it's essential character --a transparent subclass. The remaining AOP support structure are transferred to an Aspect class, which is built on top of these pure Cuts.
|
|
15
|
-
|
|
16
14
|
|
|
17
15
|
h2. PROBLEM
|
|
18
16
|
|
|
@@ -210,12 +208,14 @@ But notice the problem that arises. Super will not be directed to <code>f</code>
|
|
|
210
208
|
|
|
211
209
|
This works well enough, though one must be careful to avoid name clashes between advice and methods in classes being cut, but it is a rather brutish; nor does it provide any significant <i>inspection</i>. We can improvement upon this by passing the target method itself, but <i>enhanced</i> to provide the current super context, and usefully, its own name. We might define a method to provide this with something like:
|
|
212
210
|
|
|
211
|
+
<pre>
|
|
213
212
|
def target_method(name,&block)
|
|
214
213
|
m = method(name)
|
|
215
214
|
m.send(:define_method, :name, name)
|
|
216
215
|
m.send(:define_method, :super, &block)
|
|
217
216
|
m
|
|
218
217
|
end
|
|
218
|
+
</pre>
|
|
219
219
|
|
|
220
220
|
Then we can use it as follows.
|
|
221
221
|
|
|
@@ -278,7 +278,7 @@ FIRST. Advising multiple methods with a single advice, as we have done in the ab
|
|
|
278
278
|
end
|
|
279
279
|
</pre>
|
|
280
280
|
|
|
281
|
-
However, it not sufficient for dealing with Ruby's dynamicism. It will only
|
|
281
|
+
However, it not sufficient for dealing with Ruby's dynamicism. It will only handle methods defined in the target class at the moment the cut is defined. Complete AOP support requires the advice always stay in sync even under dynamic alteration of the targeted class. Ruby already provides means for this via the Module#method_added hook, but robust use of this technique is inconvenient at best. So a proper advice-oriented techinique would be preferable.
|
|
282
282
|
|
|
283
283
|
SECOND. When using redirected advice or, more importantly, when using modules as reusable aspects: care must be taken in choosing method names so as not to inadvertently interfere with the methods of the class(es) being cut. This can be a problem because it inhibits code reuse, i.e. the ability to design components without regard to where they may be applied. For example:
|
|
284
284
|
|
|
@@ -303,7 +303,7 @@ SECOND. When using redirected advice or, more importantly, when using modules as
|
|
|
303
303
|
C.new.d #=> "{W}"
|
|
304
304
|
</pre>
|
|
305
305
|
|
|
306
|
-
In this case, #d does not return "W" as expected, but rather "{W}" because the advice in MA caused an unexpected name clash with the #w method in C. To fulfil the true abstraction and re-usability potential of AOP
|
|
306
|
+
In this case, #d does not return "W" as expected, but rather "{W}" because the advice in MA caused an unexpected name clash with the #w method in C. To fulfil the true abstraction and re-usability potential of AOP it would help to remedy this issue.
|
|
307
307
|
|
|
308
308
|
One remedy comes from Ruby's ability to dynamically manipulate class/module definitions on the fly, in other words, "sub-classing" the aspect module and applying any required name revisions to avoid the unwanted name clash.
|
|
309
309
|
|
|
@@ -340,12 +340,12 @@ The @#rename_method@ effectively alias the original method and undefines it in o
|
|
|
340
340
|
end
|
|
341
341
|
</pre>
|
|
342
342
|
|
|
343
|
+
This kind of solution largely address the name clash issue, but it is still less then optimal. Possibly a better approach can be found.
|
|
343
344
|
|
|
344
|
-
This kind of solution largely address the name clash issue, but it is still less then optimal.
|
|
345
345
|
|
|
346
346
|
h3. The Aspect
|
|
347
347
|
|
|
348
|
-
|
|
348
|
+
One way to address the limitation of the Cut, is to take the next natural step in supporting a full AOP system and create the Aspect. Aspects are similar to Cuts, in fact they can be built via delegation to Cuts, but they are a higher-level structure and support all the AOP features most are accustom, such a pointcuts, join-points and multi-class cross-cutting. An Aspect basically takes the Cut class, builds-in all the target features we handled by hand in Cuts, adds flow control methods for handling Ruby's dynamicism and provides a wholly separate area of encapsulation, which avoids any name clashing.
|
|
349
349
|
|
|
350
350
|
The primary distinction of Aspects is the #join method, which identifies which methods are to be advised but what advice.
|
|
351
351
|
|
|
@@ -378,6 +378,7 @@ The #join method would also accept wild cards.
|
|
|
378
378
|
|
|
379
379
|
An it can also take a block which allows us to work with join-points. In the code below, @jp@ is a JoinPoint object.
|
|
380
380
|
|
|
381
|
+
<pre>
|
|
381
382
|
Xa = Aspect.new do
|
|
382
383
|
join :x do |jp|
|
|
383
384
|
jp.name == :f or jp.name == :g
|
|
@@ -385,6 +386,7 @@ An it can also take a block which allows us to work with join-points. In the cod
|
|
|
385
386
|
|
|
386
387
|
def x(target); '{' + target.super + '}'; end
|
|
387
388
|
end
|
|
389
|
+
</pre>
|
|
388
390
|
|
|
389
391
|
A JoinPoint object is very similar to an internal Ruby frame, and provides parameters based on the targeted method plus many of the same parameters that #set_trace_func can use: event, file, line, id, binding, classname. Though some of these may be omitted for performance reasons.
|
|
390
392
|
|
|
@@ -419,7 +421,7 @@ However system-wide effects must by definition be more robust as we can't always
|
|
|
419
421
|
end
|
|
420
422
|
</pre>
|
|
421
423
|
|
|
422
|
-
There are plenty of great applications for broad cross-cutting like this, especially in the way of code inspection, unit testing, debugging, etc. The Aspect is
|
|
424
|
+
There are plenty of great applications for broad cross-cutting like this, especially in the way of code inspection, unit testing, debugging, etc. The Aspect is an important part to AOP as it provides the dynamic flexibility that is required of complete Ruby AOP solution.
|
|
423
425
|
|
|
424
426
|
|
|
425
427
|
h2. ANALYSIS
|
|
@@ -443,7 +445,7 @@ h3. Cons
|
|
|
443
445
|
* Cut syntax is a slightly more verbose and entails more overhead than simple method hooks. [Counterpoint: But the difference <i>is</i> minor. Simple methods hooks could, in point of fact, be implemented via cuts with efficiencies quite close to a simple hook solution.]
|
|
444
446
|
* Cannot advise large numbers of classes in a single <i>dedicated</i> clause. [Counterpoint: Cuts are specifically designed <i>not</i> to do this as the inability also has advantage, not the least of which is performance efficiency. Moreover it has been discovered that such large swaths of cross-cutting are in actuality the uncommon usecases, more suitable to specialized applications like unit-testing and profiling.]
|
|
445
447
|
* AOP traditionalists might be a bit taken aback by the Cut-based approach in that it does not specifically reference join-points and pointcuts. [Counerpoint: As above, cuts provide a better approach for the most common AOP usecases.]
|
|
446
|
-
* Until local instance variables are available, and/or local instance methods, <i>introduction</i> is not as strong as it
|
|
448
|
+
* Until local instance variables are available, and/or local instance methods, <i>introduction</i> is not as strong as it could be for good AOP coverage. [Counterpoint: Simply using well thought out naming schemes can largely take care of this. It might also be more directly addressed in a future version of Ruby via proposed `@_` variables.]
|
|
447
449
|
|
|
448
450
|
|
|
449
451
|
|
data/README.rdoc
CHANGED
|
@@ -1,42 +1,61 @@
|
|
|
1
1
|
= Cuts - Cut-based AOP for Ruby
|
|
2
2
|
|
|
3
|
-
{
|
|
4
|
-
{
|
|
5
|
-
{
|
|
6
|
-
|
|
7
|
-
{<img src="http://travis-ci.org/rubyworks/cuts.png" />}[http://travis-ci.org/rubyworks/cuts]
|
|
3
|
+
{Source Code}[https://github.com/rubyworks/cuts] /
|
|
4
|
+
{Report Issue}[https://github.com/rubyworks/cuts/issues] /
|
|
5
|
+
{API}[https://rubydoc.info/gems/cuts]
|
|
6
|
+
{<img src="https://github.com/rubyworks/cuts/actions/workflows/ci.yml/badge.svg" alt="CI" />}[https://github.com/rubyworks/cuts/actions/workflows/ci.yml]
|
|
8
7
|
|
|
9
8
|
|
|
10
9
|
== Description
|
|
11
10
|
|
|
12
|
-
Cuts is
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
Cuts is a pure-Ruby implementation of cut-based AOP (Aspect Oriented
|
|
12
|
+
Programming). A cut is a transparent subclass: it sits in front of a class,
|
|
13
|
+
its methods are called first, and they reach the class's own with +super+.
|
|
14
|
+
|
|
15
|
+
Cuts are where Ruby's <tt>Module#prepend</tt> came from, and since version 2.0
|
|
16
|
+
of this library a cut is built on it. See "What Became of Cuts", below.
|
|
16
17
|
|
|
17
18
|
|
|
18
19
|
== History
|
|
19
20
|
|
|
20
|
-
Cuts started
|
|
21
|
-
|
|
22
|
-
were three projects
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
21
|
+
Cuts started its life as a discussion about AOP on Ruby-talk, which led to
|
|
22
|
+
an in-depth discussion between Trans and Peter Vanbroekhoven. The outcome of those talks
|
|
23
|
+
were three projects: Suby, an early experimental branch of Ruby; the RCRFoundry, a
|
|
24
|
+
section of the Ruby Garden Wiki dedicated to jointly developing RCRs; and the
|
|
25
|
+
{Cut-based AOP RCR}[https://github.com/rubyworks/cuts/blob/master/RCR.textile].
|
|
26
|
+
Neither Suby nor the RCRFoundry exists any more. The RCR does, in this repository.
|
|
27
|
+
It ultimately led to this project, as well as to an experimental Ruby 1.8.3 core
|
|
28
|
+
implementation, which was part of Suby.
|
|
29
|
+
|
|
30
|
+
Please read the {Cut-based AOP for Ruby RCR}[https://github.com/rubyworks/cuts/blob/master/RCR.textile].
|
|
31
|
+
|
|
32
|
+
This Cuts library comes by way of {Ruby Facets}[https://github.com/rubyworks/facets],
|
|
33
|
+
where the implementation was originally housed. Because of its experimental nature,
|
|
34
|
+
it was deemed best to move it into its own project as part of the work to
|
|
35
|
+
make Facets a rock solid library.
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
== What Became of Cuts
|
|
28
39
|
|
|
29
|
-
|
|
40
|
+
Cuts were designed by Trans and Peter Vanbroekhoven as a general scheme for
|
|
41
|
+
AOP, one that could be fitted to more or less any object-oriented language,
|
|
42
|
+
and were proposed for Ruby in the RCR.
|
|
30
43
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
44
|
+
Matz's reply, at the time, was to suggest something smaller: a method,
|
|
45
|
+
+prepend+, that would put a module in front of a class rather than behind it.
|
|
46
|
+
He asked whether that would be sufficient. We argued for the whole design,
|
|
47
|
+
with the cut as a kind of class in its own right. (That may have been a
|
|
48
|
+
mistake, in that it may have held +prepend+ up a little.)
|
|
49
|
+
|
|
50
|
+
Matz was right. +prepend+ was enough for Ruby. <tt>Module#prepend</tt> arrived
|
|
51
|
+
in Ruby 2.0, in 2013, and with it a module can do what a cut was designed to
|
|
52
|
+
do: wrap the methods of a class, transparently, and call them with +super+.
|
|
53
|
+
|
|
54
|
+
So this library is kept as the record of that idea, and as a working one.
|
|
55
|
+
Until version 2.0 it had to imitate a cut, by extending each object as it was
|
|
56
|
+
made. Now a cut simply is a module prepended to its class, and what the
|
|
57
|
+
library adds is what +prepend+ left out: the cut as a declared and named thing,
|
|
58
|
+
which knows its class and which its class can list.
|
|
40
59
|
|
|
41
60
|
|
|
42
61
|
== Usage
|
|
@@ -55,10 +74,44 @@ Here is a quick and dirty example:
|
|
|
55
74
|
|
|
56
75
|
c = C.new
|
|
57
76
|
|
|
58
|
-
c.f #=> "
|
|
77
|
+
c.f #=> "<f>"
|
|
78
|
+
|
|
79
|
+
A cut knows its class, and a class its cuts.
|
|
80
|
+
|
|
81
|
+
G.base #=> C
|
|
82
|
+
C.cuts #=> [G]
|
|
83
|
+
|
|
84
|
+
Which is the same as, and is built on:
|
|
85
|
+
|
|
86
|
+
module G
|
|
87
|
+
def f; '<'+super+'>' ; end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
class C
|
|
91
|
+
prepend G
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
On top of cuts is a small AOP system, of aspects, advice and join-points.
|
|
59
95
|
|
|
96
|
+
require 'cuts/aop'
|
|
60
97
|
|
|
61
|
-
|
|
98
|
+
Bracket = Aspect.new do
|
|
99
|
+
join :bracket do |joinpoint|
|
|
100
|
+
joinpoint == :f
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def bracket(target)
|
|
104
|
+
'[' + target.super + ']'
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
C.apply(Bracket)
|
|
109
|
+
|
|
110
|
+
c.f #=> "[<f>]"
|
|
111
|
+
|
|
112
|
+
The {demonstrations}[https://github.com/rubyworks/cuts/tree/master/demo]
|
|
113
|
+
have more, and are run as tests with {QED}[https://github.com/rubyworks/qed].
|
|
114
|
+
For the methods, see the {API Documentation}[https://rubydoc.info/gems/cuts].
|
|
62
115
|
|
|
63
116
|
|
|
64
117
|
== Install
|
|
@@ -67,16 +120,18 @@ Install via RubyGems:
|
|
|
67
120
|
|
|
68
121
|
$ gem install cuts
|
|
69
122
|
|
|
123
|
+
Cuts needs Ruby 3.1 or newer.
|
|
124
|
+
|
|
70
125
|
|
|
71
126
|
== Special Thanks
|
|
72
127
|
|
|
73
|
-
Special thanks to Peter
|
|
128
|
+
Special thanks to Peter Vanbroekhoven. The man is a genius!
|
|
74
129
|
|
|
75
130
|
|
|
76
|
-
==
|
|
131
|
+
== Copyrights
|
|
77
132
|
|
|
78
|
-
(
|
|
133
|
+
Cuts, Copyright (c) 2007 Thomas Sawyer & Peter Vanbroekhoven
|
|
79
134
|
|
|
80
|
-
Cuts
|
|
135
|
+
Cuts is distributable in accordance with the *FreeBSD* License.
|
|
81
136
|
|
|
82
137
|
See NOTICE.rdoc for details.
|
data/demo/01_cuts.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# Cuts
|
|
2
|
+
|
|
3
|
+
A cut is a transparent subclass. It sits in front of a class, so its
|
|
4
|
+
methods are called first, and they reach the class's own with `super`.
|
|
5
|
+
|
|
6
|
+
class Greeter
|
|
7
|
+
def greet(name)
|
|
8
|
+
"Hello, #{name}"
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
cut :Polite < Greeter do
|
|
13
|
+
def greet(name)
|
|
14
|
+
super + ", and good day to you."
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
Nothing changes for whoever uses the class. They make a Greeter and get
|
|
19
|
+
a Greeter.
|
|
20
|
+
|
|
21
|
+
greeter = Greeter.new
|
|
22
|
+
|
|
23
|
+
greeter.class #=> Greeter
|
|
24
|
+
|
|
25
|
+
But the cut has had its say.
|
|
26
|
+
|
|
27
|
+
greeter.greet("Peter") #=> "Hello, Peter, and good day to you."
|
|
28
|
+
|
|
29
|
+
The cut is a thing of its own, with a name. It knows what it cuts, and
|
|
30
|
+
the class knows its cuts.
|
|
31
|
+
|
|
32
|
+
Polite.base #=> Greeter
|
|
33
|
+
Greeter.cuts #=> [Polite]
|
|
34
|
+
|
|
35
|
+
## More Than One
|
|
36
|
+
|
|
37
|
+
A class can have any number of cuts. The newest is outermost.
|
|
38
|
+
|
|
39
|
+
cut :Loud < Greeter do
|
|
40
|
+
def greet(name)
|
|
41
|
+
super.upcase
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
Greeter.cuts #=> [Loud, Polite]
|
|
46
|
+
|
|
47
|
+
greeter.greet("Peter") #=> "HELLO, PETER, AND GOOD DAY TO YOU."
|
|
48
|
+
|
|
49
|
+
Notice that `greeter` was made before `Loud` was. A cut applies to every
|
|
50
|
+
object of the class, whenever it was made, and to objects of subclasses
|
|
51
|
+
too.
|
|
52
|
+
|
|
53
|
+
class Butler < Greeter
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
Butler.new.greet("sir") #=> "HELLO, SIR, AND GOOD DAY TO YOU."
|
|
57
|
+
|
|
58
|
+
## Without the Sugar
|
|
59
|
+
|
|
60
|
+
The `cut :Name < Class` form is a convenience. It makes a `Cut`, and
|
|
61
|
+
names it.
|
|
62
|
+
|
|
63
|
+
class Counter
|
|
64
|
+
def count; 1; end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
doubled = Cut.new(Counter) do
|
|
68
|
+
def count; super * 2; end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
Counter.new.count #=> 2
|
|
72
|
+
Counter.cuts #=> [doubled]
|
|
73
|
+
|
|
74
|
+
The trick with the symbol does not disturb the ordinary comparison of
|
|
75
|
+
symbols.
|
|
76
|
+
|
|
77
|
+
(:a < :b) #=> true
|
data/demo/02_prepend.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# Cuts and Prepend
|
|
2
|
+
|
|
3
|
+
When cuts were proposed for Ruby, Matz's answer was a smaller thing: a
|
|
4
|
+
way to put a module in front of a class instead of behind it. That
|
|
5
|
+
became `Module#prepend`, in Ruby 2.0.
|
|
6
|
+
|
|
7
|
+
A cut and a prepended module do the same job. Here is one class, done
|
|
8
|
+
both ways.
|
|
9
|
+
|
|
10
|
+
class WithCut
|
|
11
|
+
def name; "ruby"; end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
cut :Shout < WithCut do
|
|
15
|
+
def name; super.upcase + "!"; end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
And the same with `prepend`.
|
|
19
|
+
|
|
20
|
+
class WithPrepend
|
|
21
|
+
def name; "ruby"; end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
module ShoutModule
|
|
25
|
+
def name; super.upcase + "!"; end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
class WithPrepend
|
|
29
|
+
prepend ShoutModule
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
They behave alike.
|
|
33
|
+
|
|
34
|
+
WithCut.new.name #=> "RUBY!"
|
|
35
|
+
WithPrepend.new.name #=> "RUBY!"
|
|
36
|
+
|
|
37
|
+
And they are built alike. In both, the module comes before the class
|
|
38
|
+
among its ancestors, which is the whole of the trick.
|
|
39
|
+
|
|
40
|
+
WithCut.ancestors.first(2) #=> [Shout, WithCut]
|
|
41
|
+
WithPrepend.ancestors.first(2) #=> [ShoutModule, WithPrepend]
|
|
42
|
+
|
|
43
|
+
That is no coincidence. A cut _is_ a module, prepended to its class.
|
|
44
|
+
|
|
45
|
+
Shout.class #=> Cut
|
|
46
|
+
Cut.superclass #=> Module
|
|
47
|
+
WithCut.include?(Shout) #=> true
|
|
48
|
+
|
|
49
|
+
What the cut keeps of the original proposal is that it is a declared
|
|
50
|
+
thing: it is made for one class, it says so, and the class can list it.
|
|
51
|
+
|
|
52
|
+
Shout.base #=> WithCut
|
|
53
|
+
WithCut.cuts #=> [Shout]
|
|
54
|
+
|
|
55
|
+
A module that is only prepended is not a cut, and is not listed.
|
|
56
|
+
|
|
57
|
+
WithPrepend.cuts #=> []
|
data/demo/03_aop.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# Aspects
|
|
2
|
+
|
|
3
|
+
Cuts were designed as the footing for Aspect Oriented Programming. On
|
|
4
|
+
top of them this library has a small AOP system.
|
|
5
|
+
|
|
6
|
+
require 'cuts/aop'
|
|
7
|
+
|
|
8
|
+
An aspect is a set of advice. Each advice is a method, and is joined to
|
|
9
|
+
the methods it advises by a block that is asked about each one.
|
|
10
|
+
|
|
11
|
+
Bracket = Aspect.new do
|
|
12
|
+
join :bracket do |joinpoint|
|
|
13
|
+
joinpoint == :title
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def bracket(target)
|
|
17
|
+
"[" + target.super + "]"
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
An advice is given a target. Calling `target.super` goes on to the
|
|
22
|
+
method itself.
|
|
23
|
+
|
|
24
|
+
Here is a class with nothing special about it.
|
|
25
|
+
|
|
26
|
+
class Book
|
|
27
|
+
def title; "Walden"; end
|
|
28
|
+
def author; "Thoreau"; end
|
|
29
|
+
def label; title + " by " + author; end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
The aspect is applied to it.
|
|
33
|
+
|
|
34
|
+
Book.apply(Bracket)
|
|
35
|
+
|
|
36
|
+
Now the method joined to is advised, and the others are as they were.
|
|
37
|
+
|
|
38
|
+
book = Book.new
|
|
39
|
+
|
|
40
|
+
book.title #=> "[Walden]"
|
|
41
|
+
book.author #=> "Thoreau"
|
|
42
|
+
|
|
43
|
+
It makes no difference who calls the method. Here the book calls its
|
|
44
|
+
own.
|
|
45
|
+
|
|
46
|
+
book.label #=> "[Walden] by Thoreau"
|
|
47
|
+
|
|
48
|
+
## Cross-Cutting
|
|
49
|
+
|
|
50
|
+
The point of an aspect is that one concern can cut across many methods
|
|
51
|
+
and many classes. A join block can select by any rule.
|
|
52
|
+
|
|
53
|
+
Tracer = Aspect.new do
|
|
54
|
+
def self.log; @log ||= []; end
|
|
55
|
+
|
|
56
|
+
join :trace do |joinpoint|
|
|
57
|
+
joinpoint.name.to_s.end_with?('!')
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def trace(target)
|
|
61
|
+
Tracer.log << :called
|
|
62
|
+
target.super
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
class Door
|
|
67
|
+
def open!; :open; end
|
|
68
|
+
def close!; :closed; end
|
|
69
|
+
def color; :red; end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
class Window
|
|
73
|
+
def open!; :ajar; end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
Door.apply(Tracer)
|
|
77
|
+
Window.apply(Tracer)
|
|
78
|
+
|
|
79
|
+
Door.new.open! #=> :open
|
|
80
|
+
Door.new.close! #=> :closed
|
|
81
|
+
Door.new.color #=> :red
|
|
82
|
+
Window.new.open! #=> :ajar
|
|
83
|
+
|
|
84
|
+
Tracer.log.size #=> 3
|
|
85
|
+
|
|
86
|
+
A class's aspects are carried by a single cut.
|
|
87
|
+
|
|
88
|
+
Door.aspects #=> [Tracer]
|
|
89
|
+
Door.cuts #=> [Door.cross_cut]
|