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.
data/README.rdoc ADDED
@@ -0,0 +1,137 @@
1
+ = Cuts - Cut-based AOP for Ruby
2
+
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]
7
+
8
+
9
+ == Description
10
+
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.
17
+
18
+
19
+ == History
20
+
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
39
+
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.
43
+
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.
59
+
60
+
61
+ == Usage
62
+
63
+ Here is a quick and dirty example:
64
+
65
+ require 'cuts'
66
+
67
+ class C
68
+ def f ; "f" ; end
69
+ end
70
+
71
+ cut :G < C do
72
+ def f; '<'+super+'>' ; end
73
+ end
74
+
75
+ c = C.new
76
+
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.
95
+
96
+ require 'cuts/aop'
97
+
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].
115
+
116
+
117
+ == Install
118
+
119
+ Install via RubyGems:
120
+
121
+ $ gem install cuts
122
+
123
+ Cuts needs Ruby 3.1 or newer.
124
+
125
+
126
+ == Special Thanks
127
+
128
+ Special thanks to Peter Vanbroekhoven. The man is a genius!
129
+
130
+
131
+ == Copyrights
132
+
133
+ Cuts, Copyright (c) 2007 Thomas Sawyer & Peter Vanbroekhoven
134
+
135
+ Cuts is distributable in accordance with the *FreeBSD* License.
136
+
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
@@ -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]
@@ -0,0 +1,3 @@
1
+ require 'ae'
2
+ require 'ae/expect'
3
+ require 'cuts'