include_complete 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +2 -0
- data/ext/include_complete/include_complete_one.c +12 -3
- data/lib/include_complete.rb +1 -1
- data/lib/include_complete/version.rb +1 -1
- data/test/test.rb +239 -230
- metadata +2 -5
data/CHANGELOG
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
/* include_complete_one.c */
|
1
2
|
/* (c) 2010 John Mair (banisterfiend), MIT license */
|
2
3
|
/* */
|
3
4
|
/* include a module (and its singleton) into an inheritance chain */
|
@@ -10,11 +11,18 @@ static VALUE
|
|
10
11
|
class_to_s(VALUE self)
|
11
12
|
{
|
12
13
|
VALUE attached = rb_iv_get(self, "__attached__");
|
14
|
+
VALUE name;
|
13
15
|
|
14
16
|
if (attached)
|
15
|
-
|
17
|
+
name = rb_mod_name(rb_iv_get(attached, "__module__"));
|
16
18
|
else
|
17
|
-
|
19
|
+
name = rb_mod_name(rb_iv_get(self, "__module__"));
|
20
|
+
|
21
|
+
/* if module does not have a name, return "Anon" */
|
22
|
+
if (NIL_P(name) || RSTRING_LEN(name) == 0)
|
23
|
+
return rb_str_new2("Anon");
|
24
|
+
else
|
25
|
+
return name;
|
18
26
|
}
|
19
27
|
|
20
28
|
/* totally hacked up version of include_class_new() from class.c; brings in singletons into inheritance chain */
|
@@ -51,7 +59,7 @@ include_class_new(VALUE module, VALUE super)
|
|
51
59
|
RCLASS_SUPER(klass) = super;
|
52
60
|
|
53
61
|
if (TYPE(module) == T_MODULE || FL_TEST(module, FL_SINGLETON))
|
54
|
-
rb_iv_set(
|
62
|
+
rb_iv_set(klass, "__module__", module);
|
55
63
|
|
56
64
|
/* create IClass for module's singleton */
|
57
65
|
/* if super is 0 then we're including into a module (not a class), so treat as special case */
|
@@ -149,3 +157,4 @@ void
|
|
149
157
|
Init_include_complete_one() {
|
150
158
|
rb_define_method(rb_cModule, "include_complete_one", rb_include_complete_module_one, 1);
|
151
159
|
}
|
160
|
+
|
data/lib/include_complete.rb
CHANGED
data/test/test.rb
CHANGED
@@ -7,256 +7,265 @@ require 'bacon'
|
|
7
7
|
puts "Testing IncludeComplete version #{IncludeComplete::VERSION}..."
|
8
8
|
puts "Ruby version #{RUBY_VERSION}"
|
9
9
|
|
10
|
-
describe
|
11
|
-
|
12
|
-
@m = Module.new {
|
13
|
-
def self.class_method
|
14
|
-
:class_method
|
15
|
-
end
|
16
|
-
|
17
|
-
def instance_method
|
18
|
-
:instance_method
|
19
|
-
end
|
20
|
-
}
|
21
|
-
|
22
|
-
@m::CONST = :const
|
23
|
-
|
24
|
-
@c = Class.new
|
25
|
-
|
26
|
-
@c.send(:include_complete, @m)
|
27
|
-
end
|
28
|
-
|
29
|
-
it 'should make class methods accessible to class' do
|
30
|
-
@c.class_method.should.equal :class_method
|
31
|
-
end
|
32
|
-
|
33
|
-
it 'should make instance methods accessible to instances of the class' do
|
34
|
-
obj = @c.new
|
35
|
-
obj.instance_method.should.equal :instance_method
|
36
|
-
end
|
37
|
-
|
38
|
-
it 'should make constants accessible to the class' do
|
39
|
-
lambda { @c::CONST }.should.not.raise NameError
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
describe 'Extending a module into a class using extend_complete' do
|
44
|
-
before do
|
45
|
-
@m = Module.new {
|
46
|
-
def self.class_method
|
47
|
-
:class_method
|
48
|
-
end
|
49
|
-
|
50
|
-
def instance_method
|
51
|
-
:instance_method
|
52
|
-
end
|
53
|
-
}
|
54
|
-
|
55
|
-
@m::CONST = :const
|
56
|
-
|
57
|
-
@c = Class.new
|
58
|
-
|
59
|
-
@c.send(:extend_complete, @m)
|
60
|
-
end
|
61
|
-
|
62
|
-
it 'should make instance methods from the module available as class methods on the class' do
|
63
|
-
@c.instance_method.should.equal :instance_method
|
64
|
-
end
|
65
|
-
|
66
|
-
it 'should make class methods from the module available as class methods on the singleton class' do
|
67
|
-
class << @c; self; end.class_method.should.equal :class_method
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
|
72
|
-
describe 'Including a module into a module and then into a class using include_complete' do
|
10
|
+
describe IncludeComplete do
|
11
|
+
describe 'Including a module into a class using include_complete' do
|
73
12
|
before do
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
13
|
+
@m = Module.new {
|
14
|
+
def self.class_method
|
15
|
+
:class_method
|
16
|
+
end
|
78
17
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
18
|
+
def instance_method
|
19
|
+
:instance_method
|
20
|
+
end
|
21
|
+
}
|
83
22
|
|
84
|
-
|
23
|
+
@m::CONST = :const
|
85
24
|
|
86
|
-
|
87
|
-
def self.class_method2
|
88
|
-
:class_method2
|
89
|
-
end
|
25
|
+
@c = Class.new
|
90
26
|
|
91
|
-
|
92
|
-
|
93
|
-
end
|
94
|
-
}
|
95
|
-
@m2.send(:include_complete, @m1)
|
27
|
+
@c.send(:include_complete, @m)
|
28
|
+
end
|
96
29
|
|
97
|
-
|
30
|
+
it 'should make class methods accessible to class' do
|
31
|
+
@c.class_method.should.equal :class_method
|
32
|
+
end
|
98
33
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
34
|
+
it 'should make instance methods accessible to instances of the class' do
|
35
|
+
obj = @c.new
|
36
|
+
obj.instance_method.should.equal :instance_method
|
37
|
+
end
|
103
38
|
|
104
|
-
|
105
|
-
|
39
|
+
it 'should make constants accessible to the class' do
|
40
|
+
lambda { @c::CONST }.should.not.raise NameError
|
41
|
+
end
|
106
42
|
end
|
107
43
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
it 'should make class methods on modules m1 and m2 accessible to class' do
|
115
|
-
@c.class_method1.should.equal :class_method1
|
116
|
-
@c.class_method2.should.equal :class_method2
|
117
|
-
end
|
118
|
-
|
119
|
-
it 'should make instance methods on modules m1 and m2 accessible to instances of class' do
|
120
|
-
obj = @c.new
|
121
|
-
|
122
|
-
obj.instance_method1.should.equal :instance_method1
|
123
|
-
obj.instance_method2.should.equal :instance_method2
|
124
|
-
end
|
125
|
-
|
126
|
-
it 'should make ancestor chain "look" accurate' do
|
127
|
-
@m1 = Module.new {
|
128
|
-
def self.class_method1
|
129
|
-
:class_method1
|
130
|
-
end
|
131
|
-
|
132
|
-
def instance_method1
|
133
|
-
:instance_method1
|
134
|
-
end
|
135
|
-
}
|
136
|
-
|
137
|
-
Object.const_set(:M1, @m1)
|
138
|
-
|
139
|
-
@m2 = Module.new {
|
140
|
-
def self.class_method2
|
141
|
-
:class_method2
|
142
|
-
end
|
143
|
-
|
144
|
-
def instance_method2
|
145
|
-
:instance_method2
|
146
|
-
end
|
147
|
-
}
|
148
|
-
Object.const_set(:M2, @m2)
|
149
|
-
|
150
|
-
@m2.send(:include_complete, @m1)
|
151
|
-
|
152
|
-
@c = Class.new
|
153
|
-
|
154
|
-
@c.send(:include_complete, @m2)
|
155
|
-
|
156
|
-
@c.ancestors[1].to_s.should.equal M2.name
|
157
|
-
@c.class_method1.should.equal :class_method1
|
158
|
-
@c.class_method2.should.equal :class_method2
|
159
|
-
end
|
160
|
-
|
161
|
-
it 'should work if real_including a module that has another module included using Module#include' do
|
162
|
-
@m1 = Module.new {
|
163
|
-
def self.class_method1
|
164
|
-
:class_method1
|
165
|
-
end
|
166
|
-
|
167
|
-
def instance_method1
|
168
|
-
:instance_method1
|
169
|
-
end
|
170
|
-
}
|
44
|
+
describe 'Extending a module into a class using extend_complete' do
|
45
|
+
before do
|
46
|
+
@m = Module.new {
|
47
|
+
def self.class_method
|
48
|
+
:class_method
|
49
|
+
end
|
171
50
|
|
172
|
-
|
51
|
+
def instance_method
|
52
|
+
:instance_method
|
53
|
+
end
|
54
|
+
}
|
173
55
|
|
174
|
-
|
175
|
-
def self.class_method2
|
176
|
-
:class_method2
|
177
|
-
end
|
56
|
+
@m::CONST = :const
|
178
57
|
|
179
|
-
|
180
|
-
:instance_method2
|
181
|
-
end
|
182
|
-
}
|
183
|
-
Object.const_set(:N2, @m2)
|
58
|
+
@c = Class.new
|
184
59
|
|
185
|
-
|
60
|
+
@c.send(:extend_complete, @m)
|
61
|
+
end
|
186
62
|
|
187
|
-
|
63
|
+
it 'should make instance methods from the module available as class methods on the class' do
|
64
|
+
@c.instance_method.should.equal :instance_method
|
65
|
+
end
|
188
66
|
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
@c.ancestors[2].to_s.should.equal N1.name
|
193
|
-
@c.class_method1.should.equal :class_method1
|
194
|
-
@c.class_method2.should.equal :class_method2
|
67
|
+
it 'should make class methods from the module available as class methods on the singleton class' do
|
68
|
+
class << @c; self; end.class_method.should.equal :class_method
|
69
|
+
end
|
195
70
|
end
|
196
71
|
|
197
|
-
it 'should work if Module#including a module that has another module included using include_complete' do
|
198
|
-
@m1 = Module.new {
|
199
|
-
def self.class_method1
|
200
|
-
:class_method1
|
201
|
-
end
|
202
|
-
|
203
|
-
def instance_method1
|
204
|
-
:instance_method1
|
205
|
-
end
|
206
|
-
}
|
207
|
-
|
208
|
-
Object.const_set(:K1, @m1)
|
209
|
-
|
210
|
-
@m2 = Module.new {
|
211
|
-
def self.class_method2
|
212
|
-
:class_method2
|
213
|
-
end
|
214
|
-
|
215
|
-
def instance_method2
|
216
|
-
:instance_method2
|
217
|
-
end
|
218
|
-
}
|
219
|
-
Object.const_set(:K2, @m2)
|
220
|
-
|
221
|
-
@m2.send(:include_complete, @m1)
|
222
|
-
|
223
|
-
@c = Class.new
|
224
|
-
|
225
|
-
@c.send(:include, @m2)
|
226
|
-
|
227
|
-
@c.ancestors[1].should.equal K2
|
228
|
-
@c.ancestors[2].should.equal K1
|
229
|
-
@c.new.instance_method1.should.equal :instance_method1
|
230
|
-
@c.new.instance_method2.should.equal :instance_method2
|
231
|
-
end
|
232
72
|
|
73
|
+
describe 'Including a module into a module and then into a class using include_complete' do
|
74
|
+
before do
|
75
|
+
@m1 = Module.new {
|
76
|
+
def self.class_method1
|
77
|
+
:class_method1
|
78
|
+
end
|
79
|
+
|
80
|
+
def instance_method1
|
81
|
+
:instance_method1
|
82
|
+
end
|
83
|
+
}
|
84
|
+
|
85
|
+
@m1::CONST1 = :const1
|
86
|
+
|
87
|
+
@m2 = Module.new {
|
88
|
+
def self.class_method2
|
89
|
+
:class_method2
|
90
|
+
end
|
91
|
+
|
92
|
+
def instance_method2
|
93
|
+
:instance_method2
|
94
|
+
end
|
95
|
+
}
|
96
|
+
@m2.send(:include_complete, @m1)
|
97
|
+
|
98
|
+
@m2::CONST2 = :const2
|
99
|
+
|
100
|
+
@c = Class.new
|
101
|
+
|
102
|
+
@c.send(:include_complete, @m2)
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'should make class methods on m1 accessible to m2' do
|
106
|
+
@m2.class_method1.should.equal :class_method1
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'should make constants on m1 and m2 accessible to class' do
|
110
|
+
lambda { @c::CONST1 == :const1 }.should.not.raise NameError
|
111
|
+
lambda { @m2::CONST1 == :const1 }.should.not.raise NameError
|
112
|
+
lambda { @c::CONST2 == :const2 }.should.not.raise NameError
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'should make class methods on modules m1 and m2 accessible to class' do
|
116
|
+
@c.class_method1.should.equal :class_method1
|
117
|
+
@c.class_method2.should.equal :class_method2
|
118
|
+
end
|
119
|
+
|
120
|
+
it 'should make instance methods on modules m1 and m2 accessible to instances of class' do
|
121
|
+
obj = @c.new
|
122
|
+
|
123
|
+
obj.instance_method1.should.equal :instance_method1
|
124
|
+
obj.instance_method2.should.equal :instance_method2
|
125
|
+
end
|
126
|
+
|
127
|
+
it 'should make ancestor chain "look" accurate' do
|
128
|
+
@m1 = Module.new {
|
129
|
+
def self.class_method1
|
130
|
+
:class_method1
|
131
|
+
end
|
132
|
+
|
133
|
+
def instance_method1
|
134
|
+
:instance_method1
|
135
|
+
end
|
136
|
+
}
|
137
|
+
|
138
|
+
Object.const_set(:M1, @m1)
|
139
|
+
|
140
|
+
@m2 = Module.new {
|
141
|
+
def self.class_method2
|
142
|
+
:class_method2
|
143
|
+
end
|
144
|
+
|
145
|
+
def instance_method2
|
146
|
+
:instance_method2
|
147
|
+
end
|
148
|
+
}
|
149
|
+
Object.const_set(:M2, @m2)
|
150
|
+
|
151
|
+
@m2.send(:include_complete, @m1)
|
152
|
+
|
153
|
+
@c = Class.new
|
154
|
+
|
155
|
+
@c.send(:include_complete, @m2)
|
156
|
+
|
157
|
+
@c.ancestors[1].to_s.should.equal M2.name
|
158
|
+
@c.class_method1.should.equal :class_method1
|
159
|
+
@c.class_method2.should.equal :class_method2
|
160
|
+
end
|
161
|
+
|
162
|
+
it 'should work if real_including a module that has another module included using Module#include' do
|
163
|
+
@m1 = Module.new {
|
164
|
+
def self.class_method1
|
165
|
+
:class_method1
|
166
|
+
end
|
167
|
+
|
168
|
+
def instance_method1
|
169
|
+
:instance_method1
|
170
|
+
end
|
171
|
+
}
|
172
|
+
|
173
|
+
Object.const_set(:N1, @m1)
|
174
|
+
|
175
|
+
@m2 = Module.new {
|
176
|
+
def self.class_method2
|
177
|
+
:class_method2
|
178
|
+
end
|
179
|
+
|
180
|
+
def instance_method2
|
181
|
+
:instance_method2
|
182
|
+
end
|
183
|
+
}
|
184
|
+
Object.const_set(:N2, @m2)
|
185
|
+
|
186
|
+
@m2.send(:include, @m1)
|
187
|
+
|
188
|
+
@c = Class.new
|
189
|
+
|
190
|
+
@c.send(:include_complete, @m2)
|
191
|
+
|
192
|
+
@c.ancestors[1].to_s.should.equal N2.name
|
193
|
+
@c.ancestors[2].to_s.should.equal N1.name
|
194
|
+
@c.class_method1.should.equal :class_method1
|
195
|
+
@c.class_method2.should.equal :class_method2
|
196
|
+
end
|
197
|
+
|
198
|
+
it 'should work if Module#including a module that has another module included using include_complete' do
|
199
|
+
@m1 = Module.new {
|
200
|
+
def self.class_method1
|
201
|
+
:class_method1
|
202
|
+
end
|
203
|
+
|
204
|
+
def instance_method1
|
205
|
+
:instance_method1
|
206
|
+
end
|
207
|
+
}
|
208
|
+
|
209
|
+
Object.const_set(:K1, @m1)
|
210
|
+
|
211
|
+
@m2 = Module.new {
|
212
|
+
def self.class_method2
|
213
|
+
:class_method2
|
214
|
+
end
|
215
|
+
|
216
|
+
def instance_method2
|
217
|
+
:instance_method2
|
218
|
+
end
|
219
|
+
}
|
220
|
+
Object.const_set(:K2, @m2)
|
221
|
+
|
222
|
+
@m2.send(:include_complete, @m1)
|
223
|
+
|
224
|
+
@c = Class.new
|
225
|
+
|
226
|
+
@c.send(:include, @m2)
|
227
|
+
|
228
|
+
@c.ancestors[1].should.equal K2
|
229
|
+
@c.ancestors[2].should.equal K1
|
230
|
+
@c.new.instance_method1.should.equal :instance_method1
|
231
|
+
@c.new.instance_method2.should.equal :instance_method2
|
232
|
+
end
|
233
|
+
|
234
|
+
it 'should show an anonymous module as "Anon" in ancestor chain' do
|
235
|
+
c = Class.new
|
236
|
+
c.include_complete Module.new
|
237
|
+
c.ancestors[1].to_s.should == "Anon"
|
238
|
+
end
|
239
|
+
|
233
240
|
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
241
|
+
it 'should work with multiple modules passed to include_complete' do
|
242
|
+
@m1 = Module.new {
|
243
|
+
def self.class_method1
|
244
|
+
:class_method1
|
245
|
+
end
|
246
|
+
|
247
|
+
def instance_method1
|
248
|
+
:instance_method1
|
249
|
+
end
|
250
|
+
}
|
251
|
+
|
252
|
+
@m2 = Module.new {
|
253
|
+
def self.class_method2
|
254
|
+
:class_method2
|
255
|
+
end
|
256
|
+
|
257
|
+
def instance_method2
|
258
|
+
:instance_method2
|
259
|
+
end
|
260
|
+
}
|
261
|
+
|
262
|
+
@c = Class.new
|
263
|
+
@c.send(:include_complete, @m2, @m1)
|
264
|
+
|
265
|
+
@c.class_method1.should.equal :class_method1
|
266
|
+
@c.class_method2.should.equal :class_method2
|
267
|
+
@c.ancestors[1..2].map { |v| v.to_s }.should == ["Anon", "Anon"]
|
268
|
+
end
|
257
269
|
|
258
|
-
@c.class_method1.should.equal :class_method1
|
259
|
-
@c.class_method2.should.equal :class_method2
|
260
270
|
end
|
261
|
-
|
262
271
|
end
|
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: include_complete
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 27
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
7
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
8
|
+
- 2
|
9
|
+
version: 0.1.2
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- John Mair (banisterfiend)
|
@@ -54,7 +53,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
54
53
|
requirements:
|
55
54
|
- - ">="
|
56
55
|
- !ruby/object:Gem::Version
|
57
|
-
hash: 3
|
58
56
|
segments:
|
59
57
|
- 0
|
60
58
|
version: "0"
|
@@ -63,7 +61,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
63
61
|
requirements:
|
64
62
|
- - ">="
|
65
63
|
- !ruby/object:Gem::Version
|
66
|
-
hash: 3
|
67
64
|
segments:
|
68
65
|
- 0
|
69
66
|
version: "0"
|