object2module 0.2.0 → 0.3.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/LICENSE ADDED
@@ -0,0 +1,14 @@
1
+ Copyright (c) 2008 John Mair
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the
4
+ "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute,
5
+ sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following
6
+ conditions:
7
+
8
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9
+
10
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
12
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
13
+ OR OTHER DEALINGS IN THE SOFTWARE.
14
+
@@ -1,18 +1,18 @@
1
- # Object2module
2
-
3
- - converts a Class (or the Singleton of an Object) to a Module
4
- - Includes gen\_extend and gen\_include methods: generalizations
5
- of Object#extend and Module#include that work with Objects and
6
- Classes as well as Modules
7
-
8
- How it works:
9
-
10
- - First creates an IClass for the Class in question and sets the
11
- T\_MODULE flag
12
- - Recursively converts superclasses of the Class to IClasses
13
- creating a modulified version of the Class's inheritance chain
14
- - gen\_include/gen\_extend automatically call #to\_module on the
15
- Class/Object before inclusion/extension.
16
-
17
-
18
-
1
+ # Object2module
2
+
3
+ - converts a Class (or the Singleton of an Object) to a Module
4
+ - Includes gen\_extend and gen\_include methods: generalizations
5
+ of Object#extend and Module#include that work with Objects and
6
+ Classes as well as Modules
7
+
8
+ How it works:
9
+
10
+ - First creates an IClass for the Class in question and sets the
11
+ T\_MODULE flag
12
+ - Recursively converts superclasses of the Class to IClasses
13
+ creating a modulified version of the Class's inheritance chain
14
+ - gen\_include/gen\_extend automatically call #to\_module on the
15
+ Class/Object before inclusion/extension.
16
+
17
+
18
+
data/Rakefile CHANGED
@@ -1,84 +1,53 @@
1
- require 'rake/clean'
2
-
3
- OBJECT2MODULE_VERSION = "0.2.0"
4
-
5
- $dlext = Config::CONFIG['DLEXT']
6
-
7
- CLEAN.include("ext/*.#{$dlext}", "ext/*.log", "ext/*.o", "ext/*~", "ext/*#*", "ext/*.obj", "ext/*.def", "ext/*.pdb")
8
- CLOBBER.include("**/*.#{$dlext}", "**/*~", "**/*#*", "**/*.log", "**/*.o", "doc/**")
9
-
10
- $make_program = if RUBY_PLATFORM =~ /win/
11
- "nmake"
12
- else
13
- "make"
14
- end
15
-
16
- task :default => [:build]
17
-
18
- desc "Build Object2module"
19
- task :build => :clean do
20
- chdir("./ext/") do
21
- ruby "extconf.rb"
22
- sh "#{$make_program}"
23
- cp "cobject2module.#{$dlext}", "../lib" , :verbose => true
24
-
25
- if RUBY_PLATFORM =~ /mswin/
26
- if RUBY_VERSION =~ /1.9/
27
- File.rename("../lib/cobject2module.#{$dlext}",
28
- "../lib/cobject2module.19.#{$dlext}")
29
- else
30
- File.rename("../lib/cobject2module.#{$dlext}",
31
- "../lib/cobject2module.18.#{$dlext}")
32
- end
33
- end
34
- end
35
- end
36
-
37
- require 'rake/gempackagetask'
38
- specification = Gem::Specification.new do |s|
39
- s.name = "object2module"
40
- s.summary = "object2module enables ruby classes and objects to be used as modules"
41
- s.version = OBJECT2MODULE_VERSION
42
- s.date = Time.now.strftime '%Y-%m-%d'
43
- s.author = "John Mair (banisterfiend)"
44
- s.email = 'jrmair@gmail.com'
45
- s.description = s.summary
46
- s.require_path = 'lib'
47
- s.homepage = "http://banisterfiend.wordpress.com"
48
- s.has_rdoc = true
49
- s.extra_rdoc_files = ["README.rdoc", "ext/object2module.c"]
50
- s.rdoc_options << '--main' << 'README.rdoc'
51
- s.files = ["Rakefile", "lib/object2module.rb", "README.rdoc"] +
52
- FileList["ext/*.c", "ext/*.h", "ext/*.rb", "test/*.rb"].to_a
53
-
54
- if RUBY_PLATFORM =~ /mswin/
55
- s.platform = Gem::Platform::CURRENT
56
- s.files += ["lib/cobject2module.18.so", "lib/cobject2module.19.so"]
57
-
58
- else
59
- s.platform = Gem::Platform::RUBY
60
- s.extensions = ["ext/extconf.rb"]
61
- end
62
- end
63
-
64
- # gem, rdoc, and test tasks below
65
-
66
- Rake::GemPackageTask.new(specification) do |package|
67
- package.need_zip = false
68
- package.need_tar = false
69
- end
70
-
71
- require 'rake/rdoctask'
72
- Rake::RDocTask.new do |rd|
73
- rd.main = "README.rdoc"
74
- rd.rdoc_files.include("README.rdoc", "ext/*.c")
75
- end
76
-
77
- require 'rake/testtask'
78
- Rake::TestTask.new do |t|
79
- t.test_files = FileList['test/test*.rb']
80
- t.verbose = true
81
- end
82
-
83
-
84
-
1
+ # Rakefile added by John Mair (banisterfiend)
2
+
3
+ require 'rake/gempackagetask'
4
+ require 'rake/clean'
5
+ require 'lib/object2module/version.rb'
6
+
7
+ dlext = Config::CONFIG['DLEXT']
8
+
9
+ CLEAN.include("ext/**/*.#{dlext}", "ext/**/.log", "ext/**/.o", "ext/**/*~", "ext/**/*#*", "ext/**/.obj", "ext/**/.def", "ext/**/.pdb")
10
+ CLOBBER.include("**/*.#{dlext}", "**/*~", "**/*#*", "**/*.log", "**/*.o", "doc/**")
11
+
12
+
13
+ def apply_spec_defaults(s)
14
+ s.name = "object2module"
15
+ s.summary = "object2module enables ruby classes and objects to be used as modules"
16
+ s.description = s.summary
17
+ s.version = Object2module::VERSION
18
+ s.author = "John Mair (banisterfiend)"
19
+ s.email = 'jrmair@gmail.com'
20
+ s.has_rdoc = true
21
+ s.date = Time.now.strftime '%Y-%m-%d'
22
+ s.require_path = 'lib'
23
+ s.homepage = "http://banisterfiend.wordpress.com"
24
+ end
25
+
26
+
27
+ # common tasks
28
+ task :compile => :clean
29
+
30
+ # spec = Gem::Specification.new do |s|
31
+ # apply_spec_defaults(s)
32
+ # s.platform = 'i386-mswin32'
33
+ # s.files = ["Rakefile", "README", "LICENSE",
34
+ # "lib/object2module.rb", "lib/1.8/object2module.#{dlext}",
35
+ # "lib/1.9/object2module.#{dlext}", "lib/object2module/version.rb", "test/test_object2module.rb"]
36
+ # end
37
+
38
+
39
+
40
+
41
+
42
+ spec = Gem::Specification.new do |s|
43
+ apply_spec_defaults(s)
44
+ s.platform = Gem::Platform::RUBY
45
+ s.extensions = FileList["ext/**/extconf.rb"]
46
+ s.files = ["Rakefile", "README", "LICENSE", "lib/object2module.rb", "lib/object2module/version.rb", "test/test_object2module.rb"] +
47
+ FileList["ext/**/extconf.rb", "ext/**/*.h", "ext/**/*.c"].to_a
48
+ end
49
+
50
+ Rake::GemPackageTask.new(spec) do |pkg|
51
+ pkg.need_zip = false
52
+ pkg.need_tar = false
53
+ end
@@ -1,18 +1,18 @@
1
- /* contains basic macros to facilitate ruby 1.8 and ruby 1.9 compatibility */
2
-
3
- #ifndef GUARD_COMPAT_H
4
- #define GUARD_COMPAT_H
5
-
6
- #include <ruby.h>
7
-
8
- /* macros for backwards compatibility with 1.8 */
9
- #ifndef RUBY_19
10
- # define RCLASS_M_TBL(c) (RCLASS(c)->m_tbl)
11
- # define RCLASS_SUPER(c) (RCLASS(c)->super)
12
- # define RCLASS_IV_TBL(c) (RCLASS(c)->iv_tbl)
13
- #endif
14
-
15
- /* a useful macro. cannot use ordinary CLASS_OF as it does not return an lvalue */
16
- #define KLASS_OF(c) (RBASIC(c)->klass)
17
-
18
- #endif
1
+ /* contains basic macros to facilitate ruby 1.8 and ruby 1.9 compatibility */
2
+
3
+ #ifndef GUARD_COMPAT_H
4
+ #define GUARD_COMPAT_H
5
+
6
+ #include <ruby.h>
7
+
8
+ /* macros for backwards compatibility with 1.8 */
9
+ #ifndef RUBY_19
10
+ # define RCLASS_M_TBL(c) (RCLASS(c)->m_tbl)
11
+ # define RCLASS_SUPER(c) (RCLASS(c)->super)
12
+ # define RCLASS_IV_TBL(c) (RCLASS(c)->iv_tbl)
13
+ #endif
14
+
15
+ /* a useful macro. cannot use ordinary CLASS_OF as it does not return an lvalue */
16
+ #define KLASS_OF(c) (RBASIC(c)->klass)
17
+
18
+ #endif
@@ -1,6 +1,6 @@
1
- require 'mkmf'
2
-
3
- # 1.9 compatibility
4
- $CPPFLAGS += " -DRUBY_19" if RUBY_VERSION =~ /1.9/
5
-
6
- create_makefile('cobject2module')
1
+ require 'mkmf'
2
+
3
+ # 1.9 compatibility
4
+ $CPPFLAGS += " -DRUBY_19" if RUBY_VERSION =~ /1.9/
5
+
6
+ create_makefile('object2module')
@@ -1,215 +1,131 @@
1
- /* object2module.c */
2
- /* (C) John Mair 2009
3
- * This program is distributed under the terms of the MIT License
4
- * */
5
-
6
- #include <ruby.h>
7
- #include "compat.h"
8
-
9
- #ifdef RUBY_19
10
- # include <ruby/st.h>
11
- #else
12
- # include <st.h>
13
- #endif
14
-
15
- /* class creation. from class.c in 1.9.1 */
16
- #ifdef RUBY_19
17
- static VALUE
18
- class_alloc(VALUE flags, VALUE klass)
19
- {
20
- rb_classext_t *ext = ALLOC(rb_classext_t);
21
- NEWOBJ(obj, struct RClass);
22
- OBJSETUP(obj, klass, flags);
23
- obj->ptr = ext;
24
- RCLASS_IV_TBL(obj) = 0;
25
- RCLASS_M_TBL(obj) = 0;
26
- RCLASS_SUPER(obj) = 0;
27
- RCLASS_IV_INDEX_TBL(obj) = 0;
28
- return (VALUE)obj;
29
- }
30
- #endif
31
-
32
- /* a modified version of include_class_new from class.c */
33
- static VALUE
34
- j_class_new(VALUE module, VALUE sup) {
35
-
36
- #ifdef RUBY_19
37
- VALUE klass = class_alloc(T_ICLASS, rb_cClass);
38
- #else
39
- NEWOBJ(klass, struct RClass);
40
- OBJSETUP(klass, rb_cClass, T_ICLASS);
41
- #endif
42
-
43
- if (BUILTIN_TYPE(module) == T_ICLASS) {
44
- module = KLASS_OF(module);
45
- }
46
-
47
- if (!RCLASS_IV_TBL(module)) {
48
-
49
- RCLASS_IV_TBL(module) = (struct st_table *)st_init_numtable();
50
- }
51
-
52
- /* assign iv_tbl, m_tbl and super */
53
- RCLASS_IV_TBL(klass) = RCLASS_IV_TBL(module);
54
- RCLASS_SUPER(klass) = sup;
55
- if(TYPE(module) != T_OBJECT) {
56
-
57
- RCLASS_M_TBL(klass) = RCLASS_M_TBL(module);
58
- }
59
- else {
60
- RCLASS_M_TBL(klass) = RCLASS_M_TBL(CLASS_OF(module));
61
- }
62
-
63
- /* */
64
-
65
- if (TYPE(module) == T_ICLASS) {
66
- KLASS_OF(klass) = KLASS_OF(module);
67
- }
68
- else {
69
- KLASS_OF(klass) = module;
70
- }
71
-
72
-
73
- if(TYPE(module) != T_OBJECT) {
74
- OBJ_INFECT(klass, module);
75
- OBJ_INFECT(klass, sup);
76
- }
77
- return (VALUE)klass;
78
- }
79
-
80
- static VALUE
81
- rb_to_module(VALUE self) {
82
- VALUE rclass, chain_start, jcur, klass;
83
-
84
- switch(BUILTIN_TYPE(self)) {
85
- case T_MODULE:
86
- return self;
87
- case T_CLASS:
88
- klass = self;
89
- break;
90
- case T_OBJECT:
91
- default:
92
- klass = rb_singleton_class(self);
93
- }
94
-
95
- chain_start = j_class_new(klass, rb_cObject);
96
-
97
- KLASS_OF(chain_start) = rb_cModule;
98
- RBASIC(chain_start)->flags = T_MODULE;
99
-
100
- jcur = chain_start;
101
- for(rclass = RCLASS_SUPER(klass); rclass != rb_cObject;
102
- rclass = RCLASS_SUPER(rclass)) {
103
-
104
- RCLASS_SUPER(jcur) = j_class_new(rclass, rb_cObject);
105
- jcur = RCLASS_SUPER(jcur);
106
- }
107
-
108
- RCLASS_SUPER(jcur) = (VALUE)NULL;
109
-
110
- return chain_start;
111
- }
112
-
113
- static VALUE
114
- rb_reset_tbls(VALUE self) {
115
- RCLASS_IV_TBL(self) = (struct st_table *) 0;
116
- RCLASS_M_TBL(self) = (struct st_table *) st_init_numtable();
117
-
118
- return Qnil;
119
- }
120
-
121
- /*
122
- * call-seq:
123
- * obj.gen_extend(other, ...) => obj
124
- *
125
- * Adds to _obj_ the instance methods from each object given as a
126
- * parameter.
127
- *
128
- * class C
129
- * def hello
130
- * "Hello from C.\n"
131
- * end
132
- * end
133
- *
134
- * class Klass
135
- * def hello
136
- * "Hello from Klass.\n"
137
- * end
138
- * end
139
- *
140
- * k = Klass.new
141
- * k.hello #=> "Hello from Klass.\n"
142
- * k.gen_extend(C) #=> #<Klass:0x401b3bc8>
143
- * k.hello #=> "Hello from C.\n"
144
- */
145
-
146
- static VALUE
147
- rb_gen_extend(int argc, VALUE * argv, VALUE self) {
148
- int i;
149
-
150
- if (argc == 0) rb_raise(rb_eArgError, "wrong number of arguments (0 for 1)");
151
-
152
- rb_singleton_class(self);
153
-
154
- for(i = 0; i < argc; i++) {
155
- VALUE mod = rb_to_module(argv[i]);
156
- rb_funcall(mod, rb_intern("extend_object"), 1, self);
157
- rb_funcall(mod, rb_intern("extended"), 1, self);
158
-
159
- /* only redirect if argv[i] is not a module */
160
- if(argv[i] != mod) rb_reset_tbls(mod);
161
- }
162
-
163
- return self;
164
- }
165
-
166
- /*
167
- * call-seq:
168
- * gen_include(other, ...) => self
169
- *
170
- * Adds to the implied receiver the instance methods from each object given as a
171
- * parameter.
172
- *
173
- * class C
174
- * def hello
175
- * "Hello from C.\n"
176
- * end
177
- * end
178
- *
179
- * class Klass
180
- * gen_include(C)
181
- * end
182
- *
183
- * k = Klass.new
184
- * k.hello #=> "Hello from C.\n"
185
- */
186
-
187
- static VALUE
188
- rb_gen_include(int argc, VALUE * argv, VALUE self) {
189
- int i;
190
-
191
- if (argc == 0) rb_raise(rb_eArgError, "wrong number of arguments (0 for 1)");
192
-
193
- for(i = 0; i < argc; i++) {
194
- VALUE mod = rb_to_module(argv[i]);
195
- rb_funcall(mod, rb_intern("append_features"), 1, self);
196
- rb_funcall(mod, rb_intern("included"), 1, self);
197
-
198
- if(argv[i] != mod) rb_reset_tbls(mod);
199
- }
200
-
201
- return self;
202
- }
203
-
204
-
205
- void Init_cobject2module() {
206
-
207
- /* too dangerous as may result in double free. */
208
- rb_define_method(rb_cObject, "to_module", rb_to_module , 0);
209
-
210
- /* these methods are fine */
211
- rb_define_method(rb_cObject, "gen_extend", rb_gen_extend, -1);
212
- rb_define_method(rb_cModule, "gen_include", rb_gen_include, -1);
213
- rb_define_method(rb_cModule, "reset_tbls", rb_reset_tbls, 0);
214
- }
215
-
1
+ /* object2module.c */
2
+ /* (C) John Mair 2009
3
+ * This program is distributed under the terms of the MIT License
4
+ * */
5
+
6
+ #include <ruby.h>
7
+ #include "compat.h"
8
+
9
+ #ifdef RUBY_19
10
+ # include <ruby/st.h>
11
+ #else
12
+ # include <st.h>
13
+ #endif
14
+
15
+ /* class creation. from class.c in 1.9.1 */
16
+ #ifdef RUBY_19
17
+ static VALUE
18
+ class_alloc(VALUE flags, VALUE klass)
19
+ {
20
+ rb_classext_t *ext = ALLOC(rb_classext_t);
21
+ NEWOBJ(obj, struct RClass);
22
+ OBJSETUP(obj, klass, flags);
23
+ obj->ptr = ext;
24
+ RCLASS_IV_TBL(obj) = 0;
25
+ RCLASS_M_TBL(obj) = 0;
26
+ RCLASS_SUPER(obj) = 0;
27
+ RCLASS_IV_INDEX_TBL(obj) = 0;
28
+ return (VALUE)obj;
29
+ }
30
+ #endif
31
+
32
+ /* a modified version of include_class_new from class.c */
33
+ static VALUE
34
+ j_class_new(VALUE module, VALUE sup)
35
+ {
36
+
37
+ #ifdef RUBY_19
38
+ VALUE klass = class_alloc(T_ICLASS, rb_cClass);
39
+ #else
40
+ NEWOBJ(klass, struct RClass);
41
+ OBJSETUP(klass, rb_cClass, T_ICLASS);
42
+ #endif
43
+
44
+ if (TYPE(module) == T_ICLASS) {
45
+ module = KLASS_OF(module);
46
+ }
47
+
48
+ if (!RCLASS_IV_TBL(module)) {
49
+ RCLASS_IV_TBL(module) = (struct st_table *)st_init_numtable();
50
+ }
51
+
52
+ /* assign iv_tbl, m_tbl and super */
53
+ RCLASS_IV_TBL(klass) = RCLASS_IV_TBL(module);
54
+ RCLASS_SUPER(klass) = sup;
55
+ if(TYPE(module) != T_OBJECT) {
56
+ RCLASS_M_TBL(klass) = RCLASS_M_TBL(module);
57
+ }
58
+ else {
59
+ RCLASS_M_TBL(klass) = RCLASS_M_TBL(CLASS_OF(module));
60
+ }
61
+
62
+ /* */
63
+
64
+ if (TYPE(module) == T_ICLASS) {
65
+ KLASS_OF(klass) = KLASS_OF(module);
66
+ }
67
+ else {
68
+ KLASS_OF(klass) = module;
69
+ }
70
+
71
+ if(TYPE(module) != T_OBJECT) {
72
+ OBJ_INFECT(klass, module);
73
+ OBJ_INFECT(klass, sup);
74
+ }
75
+
76
+ return (VALUE)klass;
77
+ }
78
+
79
+ VALUE
80
+ rb_to_module(VALUE self)
81
+ {
82
+ VALUE rclass, chain_start, jcur, klass;
83
+
84
+ switch(BUILTIN_TYPE(self)) {
85
+ case T_MODULE:
86
+ return self;
87
+ case T_CLASS:
88
+ klass = self;
89
+ break;
90
+ case T_OBJECT:
91
+ default:
92
+ klass = rb_singleton_class(self);
93
+ }
94
+
95
+ if (self == rb_cObject || self == rb_cClass || self == rb_cModule)
96
+ rb_raise(rb_eArgError, "cannot convert Object, Class or Module to module.");
97
+
98
+ chain_start = j_class_new(klass, rb_cObject);
99
+
100
+ KLASS_OF(chain_start) = rb_cModule;
101
+ RBASIC(chain_start)->flags = T_MODULE;
102
+
103
+ jcur = chain_start;
104
+ for(rclass = RCLASS_SUPER(klass); rclass != rb_cObject;
105
+ rclass = RCLASS_SUPER(rclass)) {
106
+
107
+ RCLASS_SUPER(jcur) = j_class_new(rclass, rb_cObject);
108
+ jcur = RCLASS_SUPER(jcur);
109
+ }
110
+
111
+ RCLASS_SUPER(jcur) = (VALUE)NULL;
112
+
113
+ return chain_start;
114
+ }
115
+
116
+ VALUE
117
+ reset_tbls(VALUE self)
118
+ {
119
+ RCLASS_IV_TBL(self) = (struct st_table *) 0;
120
+ RCLASS_M_TBL(self) = (struct st_table *) st_init_numtable();
121
+
122
+ return Qnil;
123
+ }
124
+
125
+ void
126
+ Init_object2module()
127
+ {
128
+ rb_define_method(rb_cObject, "__to_module__", rb_to_module, 0);
129
+ rb_define_method(rb_cObject, "__reset_tbls__", reset_tbls, 0);
130
+ }
131
+
@@ -0,0 +1,10 @@
1
+ /* object2module.h */
2
+
3
+ #ifndef GUARD_OBJECT2MODULE_H
4
+ #define GUARD_OBJECT2MODULE_H
5
+
6
+ VALUE rb_to_module(VALUE self);
7
+ VALUE reset_tbls(VALUE self);
8
+ void Init_object2module(void);
9
+
10
+ #endif
data/lib/object2module.rb CHANGED
@@ -1,13 +1,95 @@
1
- require 'rbconfig'
2
-
3
- direc = File.dirname(__FILE__)
4
- dlext = Config::CONFIG['DLEXT']
5
- begin
6
- if RUBY_VERSION && RUBY_VERSION =~ /1.9/
7
- require "#{direc}/cobject2module.19.#{dlext}"
8
- else
9
- require "#{direc}/cobject2module.18.#{dlext}"
10
- end
11
- rescue LoadError => e
12
- require "#{direc}/cobject2module.#{dlext}"
13
- end
1
+ direc = File.dirname(__FILE__)
2
+
3
+ require 'rbconfig'
4
+ require "#{direc}/object2module/version"
5
+
6
+ dlext = Config::CONFIG['DLEXT']
7
+
8
+ begin
9
+ if RUBY_VERSION && RUBY_VERSION =~ /1.9/
10
+ require "#{direc}/1.9/object2module.#{dlext}"
11
+ else
12
+ require "#{direc}/1.8/object2module.#{dlext}"
13
+ end
14
+ rescue LoadError => e
15
+ require "#{direc}/object2module.#{dlext}"
16
+ end
17
+
18
+
19
+ # call-seq:
20
+ # obj.gen_extend(other, ...) => obj
21
+
22
+ # Adds to _obj_ the instance methods from each object given as a
23
+ # parameter.
24
+
25
+ # class C
26
+ # def hello
27
+ # "Hello from C.\n"
28
+ # end
29
+ # end
30
+
31
+ # class Klass
32
+ # def hello
33
+ # "Hello from Klass.\n"
34
+ # end
35
+ # end
36
+
37
+ # k = Klass.new
38
+ # k.hello #=> "Hello from Klass.\n"
39
+ # k.gen_extend(C) #=> #<Klass:0x401b3bc8>
40
+ # k.hello #=> "Hello from C.\n"
41
+ class Object
42
+ def gen_extend(*objs)
43
+ raise ArgumentError, "wrong number of arguments (0 for 1)" if objs.empty?
44
+
45
+ objs.each { |o|
46
+ begin
47
+ mod = o.__to_module__
48
+ extend(mod)
49
+ ensure
50
+ mod.__reset_tbls__ if mod != o &&o != Object && o != Class && o != Module
51
+ end
52
+ }
53
+
54
+ self
55
+ end
56
+ end
57
+
58
+
59
+ # call-seq:
60
+ # gen_include(other, ...) => self
61
+
62
+ # Adds to the implied receiver the instance methods from each object given as a
63
+ # parameter.
64
+
65
+ # class C
66
+ # def hello
67
+ # "Hello from C.\n"
68
+ # end
69
+ # end
70
+
71
+ # class Klass
72
+ # gen_include(C)
73
+ # end
74
+
75
+ # k = Klass.new
76
+ # k.hello #=> "Hello from C.\n"
77
+ class Module
78
+ def gen_include(*objs)
79
+ raise ArgumentError, "wrong number of arguments (0 for 1)" if objs.empty?
80
+
81
+ objs.each { |o|
82
+ begin
83
+ mod = o.__to_module__
84
+ include(mod)
85
+ ensure
86
+ mod.__reset_tbls__ if mod != o && o != Object && o != Class && o != Module
87
+ end
88
+ }
89
+
90
+ self
91
+ end
92
+ end
93
+
94
+
95
+
@@ -0,0 +1,3 @@
1
+ module Object2module
2
+ VERSION = "0.3.0"
3
+ end
@@ -1,145 +1,147 @@
1
- require 'test/unit'
2
- require 'object2module'
3
-
4
- module M
5
- def m
6
- "m"
7
- end
8
- end
9
-
10
- class A
11
- include M
12
-
13
- def a
14
- "a"
15
- end
16
- end
17
-
18
- class B < A
19
- def b
20
- "b"
21
- end
22
- end
23
-
24
- class C < B
25
- def c
26
- "c"
27
- end
28
- end
29
-
30
- # stand-alone class
31
- class K
32
- def k
33
- "k"
34
- end
35
- end
36
-
37
- # another stand-alone class
38
- class J
39
- def j
40
- "j"
41
- end
42
- end
43
-
44
- class Object2ModuleTest < Test::Unit::TestCase
45
- def test_class_to_module
46
- assert_instance_of(Module, C.to_module)
47
- end
48
-
49
- def test_class_heirarchy
50
- c = C.to_module
51
-
52
- h = c.ancestors
53
- assert_equal(B, h[1])
54
- assert_equal(A, h[2])
55
- assert_equal(M, h[3])
56
- end
57
-
58
- def test_class_extend
59
- o = Object.new
60
- assert_equal(o, o.gen_extend(C))
61
- end
62
-
63
- def test_class_extended_methods
64
- h = C.to_module
65
- o = Object.new
66
- o.extend(h)
67
- assert_equal("a", o.a)
68
- assert_equal("b", o.b)
69
- assert_equal("c", o.c)
70
- assert_equal("m", o.m)
71
- end
72
-
73
- def test_object_to_module
74
- o = C.new
75
- assert_instance_of(Module, o.to_module)
76
- end
77
-
78
- def test_object_heirarchy
79
- o = C.new
80
- h = o.to_module.ancestors
81
- assert_equal(C, h[1])
82
- assert_equal(B, h[2])
83
- assert_equal(A, h[3])
84
- assert_equal(M, h[4])
85
- end
86
-
87
- def test_object_extend
88
- h = C.to_module
89
- o = Object.new
90
- assert_equal(o, o.extend(h))
91
- end
92
-
93
- def test_object_extended_methods
94
- o = C.new
95
- h = o.to_module
96
- l = Object.new
97
- l.extend(h)
98
- assert_equal("a", l.a)
99
- assert_equal("b", l.b)
100
- assert_equal("c", l.c)
101
- assert_equal("m", l.m)
102
- end
103
-
104
- def test_gen_extend
105
- o = Object.new
106
- o.gen_extend(C)
107
- assert_equal("a", o.a)
108
- assert_equal("b", o.b)
109
- assert_equal("c", o.c)
110
- assert_equal("m", o.m)
111
- end
112
-
113
- def test_gen_include
114
- k = Class.new
115
- k.gen_include(C)
116
- o = k.new
117
- assert_equal("a", o.a)
118
- assert_equal("b", o.b)
119
- assert_equal("c", o.c)
120
- assert_equal("m", o.m)
121
- end
122
-
123
- def test_gen_extend_multi
124
- o = Object.new
125
- o.gen_extend(C, K, J)
126
- assert_equal("a", o.a)
127
- assert_equal("b", o.b)
128
- assert_equal("c", o.c)
129
- assert_equal("m", o.m)
130
- assert_equal("k", o.k)
131
- assert_equal("j", o.j)
132
- end
133
-
134
- def test_gen_include_multi
135
- k = Class.new
136
- k.gen_include(C, K, J)
137
- o = k.new
138
- assert_equal("a", o.a)
139
- assert_equal("b", o.b)
140
- assert_equal("c", o.c)
141
- assert_equal("m", o.m)
142
- assert_equal("k", o.k)
143
- assert_equal("j", o.j)
144
- end
145
- end
1
+ direc = File.dirname(__FILE__)
2
+
3
+ require 'test/unit'
4
+ require "#{direc}/../lib/object2module"
5
+
6
+ module M
7
+ def m
8
+ "m"
9
+ end
10
+ end
11
+
12
+ class A
13
+ include M
14
+
15
+ def a
16
+ "a"
17
+ end
18
+ end
19
+
20
+ class B < A
21
+ def b
22
+ "b"
23
+ end
24
+ end
25
+
26
+ class C < B
27
+ def c
28
+ "c"
29
+ end
30
+ end
31
+
32
+ # stand-alone class
33
+ class K
34
+ def k
35
+ "k"
36
+ end
37
+ end
38
+
39
+ # another stand-alone class
40
+ class J
41
+ def j
42
+ "j"
43
+ end
44
+ end
45
+
46
+ class Object2ModuleTest < Test::Unit::TestCase
47
+ def test_class_to_module
48
+ assert_instance_of(Module, C.__to_module__)
49
+ end
50
+
51
+ def test_class_heirarchy
52
+ c = C.__to_module__
53
+
54
+ h = c.ancestors
55
+ assert_equal(B, h[1])
56
+ assert_equal(A, h[2])
57
+ assert_equal(M, h[3])
58
+ end
59
+
60
+ def test_class_extend
61
+ o = Object.new
62
+ assert_equal(o, o.gen_extend(C))
63
+ end
64
+
65
+ def test_class_extended_methods
66
+ h = C.__to_module__
67
+ o = Object.new
68
+ o.extend(h)
69
+ assert_equal("a", o.a)
70
+ assert_equal("b", o.b)
71
+ assert_equal("c", o.c)
72
+ assert_equal("m", o.m)
73
+ end
74
+
75
+ def test_object_to_module
76
+ o = C.new
77
+ assert_instance_of(Module, o.__to_module__)
78
+ end
79
+
80
+ def test_object_heirarchy
81
+ o = C.new
82
+ h = o.__to_module__.ancestors
83
+ assert_equal(C, h[1])
84
+ assert_equal(B, h[2])
85
+ assert_equal(A, h[3])
86
+ assert_equal(M, h[4])
87
+ end
88
+
89
+ def test_object_extend
90
+ h = C.__to_module__
91
+ o = Object.new
92
+ assert_equal(o, o.extend(h))
93
+ end
94
+
95
+ def test_object_extended_methods
96
+ o = C.new
97
+ h = o.__to_module__
98
+ l = Object.new
99
+ l.extend(h)
100
+ assert_equal("a", l.a)
101
+ assert_equal("b", l.b)
102
+ assert_equal("c", l.c)
103
+ assert_equal("m", l.m)
104
+ end
105
+
106
+ def test_gen_extend
107
+ o = Object.new
108
+ o.gen_extend(C)
109
+ assert_equal("a", o.a)
110
+ assert_equal("b", o.b)
111
+ assert_equal("c", o.c)
112
+ assert_equal("m", o.m)
113
+ end
114
+
115
+ def test_gen_include
116
+ k = Class.new
117
+ k.gen_include(C)
118
+ o = k.new
119
+ assert_equal("a", o.a)
120
+ assert_equal("b", o.b)
121
+ assert_equal("c", o.c)
122
+ assert_equal("m", o.m)
123
+ end
124
+
125
+ def test_gen_extend_multi
126
+ o = Object.new
127
+ o.gen_extend(C, K, J)
128
+ assert_equal("a", o.a)
129
+ assert_equal("b", o.b)
130
+ assert_equal("c", o.c)
131
+ assert_equal("m", o.m)
132
+ assert_equal("k", o.k)
133
+ assert_equal("j", o.j)
134
+ end
135
+
136
+ def test_gen_include_multi
137
+ k = Class.new
138
+ k.gen_include(C, K, J)
139
+ o = k.new
140
+ assert_equal("a", o.a)
141
+ assert_equal("b", o.b)
142
+ assert_equal("c", o.c)
143
+ assert_equal("m", o.m)
144
+ assert_equal("k", o.k)
145
+ assert_equal("j", o.j)
146
+ end
147
+ end
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: object2module
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ hash: 19
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
5
11
  platform: ruby
6
12
  authors:
7
13
  - John Mair (banisterfiend)
@@ -9,7 +15,7 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2009-09-25 00:00:00 +12:00
18
+ date: 2010-07-29 00:00:00 +12:00
13
19
  default_executable:
14
20
  dependencies: []
15
21
 
@@ -18,45 +24,53 @@ email: jrmair@gmail.com
18
24
  executables: []
19
25
 
20
26
  extensions:
21
- - ext/extconf.rb
22
- extra_rdoc_files:
23
- - README.rdoc
24
- - ext/object2module.c
27
+ - ext/object2module/extconf.rb
28
+ extra_rdoc_files: []
29
+
25
30
  files:
26
31
  - Rakefile
32
+ - README
33
+ - LICENSE
27
34
  - lib/object2module.rb
28
- - README.rdoc
29
- - ext/object2module.c
30
- - ext/object2module.h
31
- - ext/compat.h
32
- - ext/extconf.rb
35
+ - lib/object2module/version.rb
33
36
  - test/test_object2module.rb
37
+ - ext/object2module/extconf.rb
38
+ - ext/object2module/compat.h
39
+ - ext/object2module/object2module.h
40
+ - ext/object2module/object2module.c
34
41
  has_rdoc: true
35
42
  homepage: http://banisterfiend.wordpress.com
43
+ licenses: []
44
+
36
45
  post_install_message:
37
- rdoc_options:
38
- - --main
39
- - README.rdoc
46
+ rdoc_options: []
47
+
40
48
  require_paths:
41
49
  - lib
42
50
  required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
43
52
  requirements:
44
53
  - - ">="
45
54
  - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
46
58
  version: "0"
47
- version:
48
59
  required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
49
61
  requirements:
50
62
  - - ">="
51
63
  - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
52
67
  version: "0"
53
- version:
54
68
  requirements: []
55
69
 
56
70
  rubyforge_project:
57
- rubygems_version: 1.3.1
71
+ rubygems_version: 1.3.7
58
72
  signing_key:
59
- specification_version: 2
73
+ specification_version: 3
60
74
  summary: object2module enables ruby classes and objects to be used as modules
61
75
  test_files: []
62
76
 
data/ext/object2module.h DELETED
@@ -1,11 +0,0 @@
1
- /* object2module.h */
2
-
3
- #ifndef GUARD_OBJECT2MODULE_H
4
- #define GUARD_OBJECT2MODULE_H
5
-
6
- VALUE rb_gen_include(int argc, VALUE * argv, VALUE self);
7
- VALUE rb_gen_extend(int argc, VALUE * argv, VALUE self);
8
- VALUE rb_to_module(VALUE self);
9
- VALUE rb_reset_tbls(VALUE self);
10
-
11
- #endif