paramix 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,87 @@
1
+ = Parametric Include
2
+
3
+ Load the library.
4
+
5
+ require 'paramix'
6
+
7
+ Given a parametric mixin.
8
+
9
+ module M
10
+ include Paramix::Parametric
11
+
12
+ parameterized do |params|
13
+
14
+ public :f do
15
+ params[:p]
16
+ end
17
+
18
+ end
19
+ end
20
+
21
+ We can inlcude the parameteric module in a some classes.
22
+
23
+ class I1
24
+ include M[:p => "mosh"]
25
+ end
26
+
27
+ class I2
28
+ include M[:p => "many"]
29
+ end
30
+
31
+ And the result will vary according to the parameter set.
32
+
33
+ I1.new.f #=> "mosh"
34
+ I2.new.f #=> "many"
35
+
36
+
37
+ = Parametric Extension
38
+
39
+ We can also extend classes witht the mixin.
40
+
41
+ class E1
42
+ extend M[:p => "mosh2"]
43
+ end
44
+
45
+ class E2
46
+ extend M[:p => "many2"]
47
+ end
48
+
49
+ And the results will likewise work as expected.
50
+
51
+ E1.f #=> "mosh2"
52
+ E2.f #=> "many2"
53
+
54
+
55
+ = Dynamically Defined Methods
56
+
57
+ Parametric mixins can be used to define dynamic code.
58
+
59
+ module N
60
+ include Paramix::Parametric
61
+
62
+ parameterized do |params|
63
+ attr_accessor params[:a]
64
+ end
65
+ end
66
+
67
+ Now if we include this module we will have new attributes based on
68
+ the parameter assigned.
69
+
70
+ class D1
71
+ include N[:a => "m1"]
72
+ end
73
+
74
+ class D2
75
+ include N[:a => "m2"]
76
+ end
77
+
78
+ d1 = D1.new
79
+ d1.m1 = :yes1
80
+
81
+ d1.m1 #=> :yes1
82
+
83
+ d2 = D2.new
84
+ d2.m2 = :yes2
85
+
86
+ d2.m2 #=> :yes2
87
+
@@ -0,0 +1,29 @@
1
+ = Works with Namespaces
2
+
3
+ Parametric mixins work regardless of the namespace depth.
4
+
5
+ module R
6
+ module M
7
+ include Paramix::Parametric
8
+
9
+ parameterized do |params|
10
+ public :f do
11
+ params[:p]
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ module Q
18
+ class I
19
+ include R::M[:p => "mosh"]
20
+ end
21
+ class E
22
+ extend R::M[:p => "many"]
23
+ end
24
+ end
25
+
26
+ Q::I.new.f #=> "mosh"
27
+
28
+ Q::E.f #=> "many"
29
+
@@ -0,0 +1,229 @@
1
+ require 'paramix'
2
+
3
+ describe "nested modules where inner most module is parametric" do
4
+
5
+ m = Module.new do
6
+ include Paramix::Parametric
7
+
8
+ parameterized do |params|
9
+ public :f do
10
+ params[:p]
11
+ end
12
+ end
13
+ end
14
+
15
+ n = Module.new do
16
+ include m[:p=>"NMp"]
17
+ end
18
+
19
+ i = Class.new do
20
+ include n
21
+ end
22
+
23
+ e = Class.new do
24
+ extend n
25
+ end
26
+
27
+
28
+ it "should" do
29
+ i.new.f.should == "NMp"
30
+ end
31
+
32
+ it "should" do
33
+ e.f.should == "NMp"
34
+ end
35
+
36
+ end
37
+
38
+ describe "nested parametric mixins with parameters" do
39
+
40
+ m = Module.new do
41
+ include Paramix::Parametric
42
+
43
+ parameterized do |params|
44
+ public :f do
45
+ params[:p]
46
+ end
47
+ end
48
+ end
49
+
50
+ n = Module.new do
51
+ include Paramix::Parametric
52
+ include m[:p=>"NMp"]
53
+
54
+ parameterized do |params|
55
+ public :g do
56
+ params[:p]
57
+ end
58
+ end
59
+ end
60
+
61
+ i = Class.new do
62
+ include n[:p => "INp"]
63
+ end
64
+
65
+ e = Class.new do
66
+ extend n[:p => "ENp"]
67
+ end
68
+
69
+
70
+ it "should" do
71
+ i.new.f.should == "NMp"
72
+ end
73
+
74
+ it "should" do
75
+ i.new.g.should == "INp"
76
+ end
77
+
78
+ it "should" do
79
+ e.f.should == "NMp"
80
+ end
81
+
82
+ it "should" do
83
+ e.g.should == "ENp"
84
+ end
85
+
86
+ end
87
+
88
+ describe "nested parametric mixins without parameters" do
89
+
90
+ m = Module.new do
91
+ include Paramix::Parametric
92
+
93
+ parameterized do |params|
94
+ public :f do
95
+ params[:p]
96
+ end
97
+ end
98
+ end
99
+
100
+ n = Module.new do
101
+ include m
102
+
103
+ parameterized do |params|
104
+ public :g do
105
+ params[:p]
106
+ end
107
+ end
108
+ end
109
+
110
+ i = Class.new do
111
+ include n[:p => "INp"]
112
+ end
113
+
114
+ e = Class.new do
115
+ extend n[:p => "ENp"]
116
+ end
117
+
118
+
119
+ it "should" do
120
+ i.new.f.should == "INp"
121
+ end
122
+
123
+ it "should" do
124
+ i.new.g.should == "INp"
125
+ end
126
+
127
+ it "should" do
128
+ e.f.should == "ENp"
129
+ end
130
+
131
+ it "should" do
132
+ e.g.should == "ENp"
133
+ end
134
+
135
+ end
136
+
137
+ describe "nested parametric mixins where the outer is not parameterized" do
138
+
139
+ m = Module.new do
140
+ include Paramix::Parametric
141
+
142
+ parameterized do |params|
143
+ public :f do
144
+ params[:p]
145
+ end
146
+ end
147
+ end
148
+
149
+ n = Module.new do
150
+ include Paramix::Parametric
151
+ include m[:p=>"NMp"]
152
+ end
153
+
154
+ i = Class.new do ; include n[] ; end
155
+ e = Class.new do ; extend n[] ; end
156
+
157
+ ix = Class.new do ; include n[:p=>"IxNp"] ; end
158
+ ex = Class.new do ; extend n[:p=>"ExNp"] ; end
159
+
160
+
161
+ it "should" do
162
+ i.new.f.should == "NMp"
163
+ end
164
+
165
+ it "should" do
166
+ e.f.should == "NMp"
167
+ end
168
+
169
+ it "should" do
170
+ i.new.f.should == "NMp"
171
+ end
172
+
173
+ it "should" do
174
+ e.f.should == "NMp"
175
+ end
176
+
177
+ end
178
+
179
+
180
+ describe "nested parametric mixins both parameterized" do
181
+
182
+ m = Module.new do
183
+ include Paramix::Parametric
184
+
185
+ parameterized do |params|
186
+ public :f do
187
+ params[:p]
188
+ end
189
+ end
190
+ end
191
+
192
+ n = Module.new do
193
+ include Paramix::Parametric
194
+ include m[:p=>"mood"]
195
+
196
+ parameterized do |params|
197
+ public :g do
198
+ params[:p]
199
+ end
200
+ end
201
+ end
202
+
203
+ i = Class.new do
204
+ include n[]
205
+ end
206
+
207
+ e = Class.new do
208
+ extend n[]
209
+ end
210
+
211
+
212
+ it "should" do
213
+ i.new.f.should == "mood"
214
+ end
215
+
216
+ it "should" do
217
+ i.new.g.should == nil # TODO: or error ?
218
+ end
219
+
220
+ it "should" do
221
+ e.f.should == "mood"
222
+ end
223
+
224
+ it "should" do
225
+ e.g.should == nil
226
+ end
227
+
228
+ end
229
+
@@ -0,0 +1,48 @@
1
+ require 'paramix'
2
+
3
+ describe "dynamic methods using paramtric mixins" do
4
+
5
+ module M
6
+ include Paramix::Parametric
7
+
8
+ parameterized do |params|
9
+
10
+ public :f do
11
+ params[:p]
12
+ end
13
+
14
+ attr_accessor params[:p]
15
+ end
16
+ end
17
+
18
+ class C1
19
+ include M[:p => "c1"]
20
+ end
21
+
22
+ class C2
23
+ include M[:p => "c2"]
24
+ end
25
+
26
+ it "should" do
27
+ c = C1.new
28
+ c.f.should == "c1"
29
+ end
30
+
31
+ it "should" do
32
+ c = C2.new
33
+ c.f.should == "c2"
34
+ end
35
+
36
+ it "should" do
37
+ c = C1.new
38
+ c.c1 = :yes1
39
+ c.c1.should == :yes1
40
+ end
41
+
42
+ it "should" do
43
+ c = C2.new
44
+ c.c2 = :yes2
45
+ c.c2.should == :yes2
46
+ end
47
+
48
+ end
@@ -0,0 +1,21 @@
1
+ require 'paramix'
2
+
3
+ describe "extend with parametric mixins" do
4
+
5
+ # -- fixture ------------------------------
6
+
7
+ module M
8
+ include Paramix::Parametric
9
+
10
+ parameterized do |params|
11
+
12
+ public :f do
13
+ params[:p]
14
+ end
15
+
16
+ end
17
+ end
18
+
19
+
20
+ end
21
+
@@ -0,0 +1,44 @@
1
+ require 'paramix'
2
+
3
+ describe "include with parametric mixins" do
4
+
5
+ module M
6
+ include Paramix::Parametric
7
+
8
+ parameterized do |params|
9
+
10
+ public :f do
11
+ params[:p]
12
+ end
13
+
14
+ end
15
+ end
16
+
17
+ class I1
18
+ include M[:p => "mosh"]
19
+ end
20
+
21
+ class I2
22
+ include M[:p => "many"]
23
+ end
24
+
25
+ class E1
26
+ extend M[:p => "mosh"]
27
+ end
28
+
29
+ class E2
30
+ extend M[:p => "many"]
31
+ end
32
+
33
+ it "should vary the return value of the instance methods" do
34
+ I1.new.f.should == "mosh"
35
+ I2.new.f.should == "many"
36
+ end
37
+
38
+ it "should vary the return value of the class methods " do
39
+ E1.f.should == "mosh"
40
+ E2.f.should == "many"
41
+ end
42
+
43
+ end
44
+