object2module 0.1.2 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +2 -2
- data/lib/object2module.rb +10 -6
- data/test/test_object2module.rb +8 -8
- metadata +1 -1
data/README.markdown
CHANGED
@@ -7,5 +7,5 @@ Object2module
|
|
7
7
|
|
8
8
|
How it works:
|
9
9
|
* First creates an IClass for the Class in question and sets the T\_MODULE flag
|
10
|
-
* Recursively
|
11
|
-
* gen\_include/gen\_extend automatically call #
|
10
|
+
* Recursively converts superclasses of the Class to IClasses creating a modulified version of the Class's inheritance chain
|
11
|
+
* gen\_include/gen\_extend automatically call #to\_module on the Class/Object before inclusion/extension.
|
data/lib/object2module.rb
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
# object2module.rb
|
2
|
+
# (C) John Mair 2008
|
3
|
+
# This program is distributed under the terms of the MIT License
|
4
|
+
|
1
5
|
require 'rubygems'
|
2
6
|
require 'inline'
|
3
7
|
|
@@ -61,15 +65,15 @@ class Object
|
|
61
65
|
|
62
66
|
builder.c %{
|
63
67
|
VALUE
|
64
|
-
|
68
|
+
to_module() {
|
65
69
|
VALUE rclass, chain_start, jcur, klass;
|
66
70
|
|
67
|
-
if(BUILTIN_TYPE(self) ==
|
71
|
+
if(BUILTIN_TYPE(self) == T_MODULE)
|
72
|
+
return self;
|
73
|
+
else if(BUILTIN_TYPE(self) == T_CLASS)
|
68
74
|
klass = self;
|
69
75
|
else if(BUILTIN_TYPE(self) == T_OBJECT)
|
70
76
|
klass = rb_singleton_class(self);
|
71
|
-
else if(BUILTIN_TYPE(self) == T_MODULE)
|
72
|
-
return self;
|
73
77
|
else
|
74
78
|
return Qnil;
|
75
79
|
|
@@ -92,13 +96,13 @@ class Object
|
|
92
96
|
end
|
93
97
|
|
94
98
|
def gen_extend(*objs)
|
95
|
-
extend(*objs.map { |o| o.
|
99
|
+
extend(*objs.map { |o| o.to_module })
|
96
100
|
end
|
97
101
|
|
98
102
|
end
|
99
103
|
|
100
104
|
class Module
|
101
105
|
def gen_include(*objs)
|
102
|
-
include(*objs.map { |o| o.
|
106
|
+
include(*objs.map { |o| o.to_module })
|
103
107
|
end
|
104
108
|
end
|
data/test/test_object2module.rb
CHANGED
@@ -43,24 +43,24 @@ end
|
|
43
43
|
|
44
44
|
class Object2ModuleTest < Test::Unit::TestCase
|
45
45
|
def test_class_to_module
|
46
|
-
assert_instance_of(Module, C.
|
46
|
+
assert_instance_of(Module, C.to_module)
|
47
47
|
end
|
48
48
|
|
49
49
|
def test_class_heirarchy
|
50
|
-
h = C.
|
50
|
+
h = C.to_module.ancestors
|
51
51
|
assert_equal(B, h[1])
|
52
52
|
assert_equal(A, h[2])
|
53
53
|
assert_equal(M, h[3])
|
54
54
|
end
|
55
55
|
|
56
56
|
def test_class_extend
|
57
|
-
h = C.
|
57
|
+
h = C.to_module
|
58
58
|
o = Object.new
|
59
59
|
assert_equal(o, o.extend(h))
|
60
60
|
end
|
61
61
|
|
62
62
|
def test_class_extended_methods
|
63
|
-
h = C.
|
63
|
+
h = C.to_module
|
64
64
|
o = Object.new
|
65
65
|
o.extend(h)
|
66
66
|
assert_equal("a", o.a)
|
@@ -71,13 +71,13 @@ class Object2ModuleTest < Test::Unit::TestCase
|
|
71
71
|
|
72
72
|
def test_object_to_module
|
73
73
|
o = C.new
|
74
|
-
assert_instance_of(Module, o.
|
74
|
+
assert_instance_of(Module, o.to_module)
|
75
75
|
|
76
76
|
end
|
77
77
|
|
78
78
|
def test_object_heirarchy
|
79
79
|
o = C.new
|
80
|
-
|
80
|
+
h = o.to_module.ancestors
|
81
81
|
assert_equal(C, h[1])
|
82
82
|
assert_equal(B, h[2])
|
83
83
|
assert_equal(A, h[3])
|
@@ -85,14 +85,14 @@ class Object2ModuleTest < Test::Unit::TestCase
|
|
85
85
|
end
|
86
86
|
|
87
87
|
def test_object_extend
|
88
|
-
h = C.
|
88
|
+
h = C.to_module
|
89
89
|
o = Object.new
|
90
90
|
assert_equal(o, o.extend(h))
|
91
91
|
end
|
92
92
|
|
93
93
|
def test_object_extended_methods
|
94
94
|
o = C.new
|
95
|
-
h = o.
|
95
|
+
h = o.to_module
|
96
96
|
l = Object.new
|
97
97
|
l.extend(h)
|
98
98
|
assert_equal("a", l.a)
|