object2module 0.3.0 → 0.4.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/Rakefile +40 -20
- data/ext/object2module/compat.h +39 -0
- data/ext/object2module/object2module.c +71 -105
- data/lib/object2module.rb +57 -61
- data/lib/object2module/version.rb +1 -1
- data/test/test.rb +165 -0
- data/test/test_stress.rb +6 -0
- metadata +5 -7
- data/test/test_object2module.rb +0 -147
data/Rakefile
CHANGED
@@ -1,10 +1,12 @@
|
|
1
|
-
|
1
|
+
# Rakefile added by John Mair (banisterfiend)
|
2
2
|
|
3
3
|
require 'rake/gempackagetask'
|
4
|
+
require 'rake/rdoctask'
|
4
5
|
require 'rake/clean'
|
5
|
-
require 'lib/object2module/version.rb'
|
6
|
+
require './lib/object2module/version.rb'
|
6
7
|
|
7
8
|
dlext = Config::CONFIG['DLEXT']
|
9
|
+
direc = File.dirname(__FILE__)
|
8
10
|
|
9
11
|
CLEAN.include("ext/**/*.#{dlext}", "ext/**/.log", "ext/**/.o", "ext/**/*~", "ext/**/*#*", "ext/**/.obj", "ext/**/.def", "ext/**/.pdb")
|
10
12
|
CLOBBER.include("**/*.#{dlext}", "**/*~", "**/*#*", "**/*.log", "**/*.o", "doc/**")
|
@@ -24,30 +26,48 @@ def apply_spec_defaults(s)
|
|
24
26
|
end
|
25
27
|
|
26
28
|
|
27
|
-
|
28
|
-
|
29
|
+
task :test do
|
30
|
+
sh "bacon -k #{direc}/test/test.rb"
|
31
|
+
end
|
29
32
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
33
|
+
[:mingw32, :mswin32].each do |v|
|
34
|
+
task v do
|
35
|
+
spec = Gem::Specification.new do |s|
|
36
|
+
apply_spec_defaults(s)
|
37
|
+
s.platform = "i386-#{v}"
|
38
|
+
s.files = FileList["Rakefile", "README", "LICENSE",
|
39
|
+
"lib/object2module.rb", "lib/1.8/object2module.#{dlext}",
|
40
|
+
"lib/1.9/object2module.#{dlext}", "lib/object2module/version.rb", "test/*.rb"].to_a
|
41
|
+
end
|
37
42
|
|
43
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
44
|
+
pkg.need_zip = false
|
45
|
+
pkg.need_tar = false
|
46
|
+
end
|
38
47
|
|
48
|
+
Rake::Task[:gem].invoke
|
49
|
+
end
|
50
|
+
end
|
39
51
|
|
52
|
+
task :ruby do
|
53
|
+
spec = Gem::Specification.new do |s|
|
54
|
+
apply_spec_defaults(s)
|
55
|
+
s.platform = Gem::Platform::RUBY
|
56
|
+
s.files = FileList["Rakefile", "README", "LICENSE",
|
57
|
+
"lib/object2module.rb","lib/object2module/version.rb",
|
58
|
+
"test/*.rb", "ext/**/extconf.rb", "ext/**/*.h", "ext/**/*.c"].to_a
|
59
|
+
s.extensions = ["ext/object2module/extconf.rb"]
|
60
|
+
end
|
40
61
|
|
62
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
63
|
+
pkg.need_zip = false
|
64
|
+
pkg.need_tar = false
|
65
|
+
end
|
41
66
|
|
42
|
-
|
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
|
67
|
+
Rake::Task[:gem].invoke
|
48
68
|
end
|
49
69
|
|
50
|
-
Rake::
|
51
|
-
|
52
|
-
|
70
|
+
Rake::RDocTask.new do |rd|
|
71
|
+
rd.main = "README.rdoc"
|
72
|
+
rd.rdoc_files.include("README.rdoc", "lib/object2module.rb")
|
53
73
|
end
|
data/ext/object2module/compat.h
CHANGED
@@ -5,13 +5,52 @@
|
|
5
5
|
|
6
6
|
#include <ruby.h>
|
7
7
|
|
8
|
+
/* test for 1.9 */
|
9
|
+
#if !defined(RUBY_19) && defined(ROBJECT_EMBED_LEN_MAX)
|
10
|
+
# define RUBY_19
|
11
|
+
#endif
|
12
|
+
|
8
13
|
/* macros for backwards compatibility with 1.8 */
|
9
14
|
#ifndef RUBY_19
|
10
15
|
# define RCLASS_M_TBL(c) (RCLASS(c)->m_tbl)
|
11
16
|
# define RCLASS_SUPER(c) (RCLASS(c)->super)
|
12
17
|
# define RCLASS_IV_TBL(c) (RCLASS(c)->iv_tbl)
|
18
|
+
# define OBJ_UNTRUSTED OBJ_TAINTED
|
19
|
+
# include "st.h"
|
20
|
+
#endif
|
21
|
+
|
22
|
+
#ifdef RUBY_19
|
23
|
+
inline static VALUE
|
24
|
+
class_alloc(VALUE flags, VALUE klass)
|
25
|
+
{
|
26
|
+
rb_classext_t *ext = ALLOC(rb_classext_t);
|
27
|
+
NEWOBJ(obj, struct RClass);
|
28
|
+
OBJSETUP(obj, klass, flags);
|
29
|
+
obj->ptr = ext;
|
30
|
+
RCLASS_IV_TBL(obj) = 0;
|
31
|
+
RCLASS_M_TBL(obj) = 0;
|
32
|
+
RCLASS_SUPER(obj) = 0;
|
33
|
+
RCLASS_IV_INDEX_TBL(obj) = 0;
|
34
|
+
return (VALUE)obj;
|
35
|
+
}
|
13
36
|
#endif
|
14
37
|
|
38
|
+
inline static VALUE
|
39
|
+
create_class(VALUE flags, VALUE klass)
|
40
|
+
{
|
41
|
+
#ifdef RUBY_19
|
42
|
+
VALUE new_klass = class_alloc(flags, klass);
|
43
|
+
#else
|
44
|
+
NEWOBJ(new_klass, struct RClass);
|
45
|
+
OBJSETUP(new_klass, klass, flags);
|
46
|
+
#endif
|
47
|
+
|
48
|
+
return (VALUE)new_klass;
|
49
|
+
}
|
50
|
+
|
51
|
+
# define FALSE 0
|
52
|
+
# define TRUE 1
|
53
|
+
|
15
54
|
/* a useful macro. cannot use ordinary CLASS_OF as it does not return an lvalue */
|
16
55
|
#define KLASS_OF(c) (RBASIC(c)->klass)
|
17
56
|
|
@@ -6,126 +6,92 @@
|
|
6
6
|
#include <ruby.h>
|
7
7
|
#include "compat.h"
|
8
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
9
|
static VALUE
|
34
|
-
|
10
|
+
include_class_new(VALUE module, VALUE super)
|
35
11
|
{
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
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;
|
12
|
+
VALUE klass = create_class(T_ICLASS, rb_cClass);
|
13
|
+
|
14
|
+
if (BUILTIN_TYPE(module) == T_ICLASS) {
|
15
|
+
|
16
|
+
/* for real_include compat */
|
17
|
+
if (!NIL_P(rb_iv_get(module, "__module__")))
|
18
|
+
module = rb_iv_get(module, "__module__");
|
19
|
+
else
|
20
|
+
module = RBASIC(module)->klass;
|
21
|
+
}
|
22
|
+
|
23
|
+
if (!RCLASS_IV_TBL(module)) {
|
24
|
+
RCLASS_IV_TBL(module) = st_init_numtable();
|
25
|
+
}
|
26
|
+
RCLASS_IV_TBL(klass) = RCLASS_IV_TBL(module);
|
27
|
+
RCLASS_M_TBL(klass) = RCLASS_M_TBL(module);
|
28
|
+
RCLASS_SUPER(klass) = super;
|
29
|
+
if (TYPE(module) == T_ICLASS) {
|
30
|
+
RBASIC(klass)->klass = RBASIC(module)->klass;
|
31
|
+
}
|
32
|
+
else {
|
33
|
+
RBASIC(klass)->klass = module;
|
34
|
+
}
|
35
|
+
|
36
|
+
OBJ_INFECT(klass, module);
|
37
|
+
OBJ_INFECT(klass, super);
|
38
|
+
|
39
|
+
return (VALUE)klass;
|
77
40
|
}
|
78
41
|
|
79
42
|
VALUE
|
80
|
-
|
43
|
+
rb_gen_include_one(VALUE klass, VALUE module)
|
81
44
|
{
|
82
|
-
|
45
|
+
VALUE p, c;
|
46
|
+
int changed = 0;
|
83
47
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
klass = self;
|
89
|
-
break;
|
90
|
-
case T_OBJECT:
|
91
|
-
default:
|
92
|
-
klass = rb_singleton_class(self);
|
93
|
-
}
|
48
|
+
rb_frozen_class_p(klass);
|
49
|
+
if (!OBJ_UNTRUSTED(klass)) {
|
50
|
+
rb_secure(4);
|
51
|
+
}
|
94
52
|
|
95
|
-
|
96
|
-
|
53
|
+
/* when including an object, include its singleton class */
|
54
|
+
if (TYPE(module) == T_OBJECT)
|
55
|
+
module = rb_singleton_class(module);
|
97
56
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
57
|
+
OBJ_INFECT(klass, module);
|
58
|
+
c = klass;
|
59
|
+
while (module) {
|
60
|
+
int superclass_seen = FALSE;
|
61
|
+
|
62
|
+
if (RCLASS_M_TBL(klass) == RCLASS_M_TBL(module))
|
63
|
+
rb_raise(rb_eArgError, "cyclic include detected");
|
64
|
+
/* ignore if the module included already in superclasses */
|
65
|
+
for (p = RCLASS_SUPER(klass); p; p = RCLASS_SUPER(p)) {
|
66
|
+
switch (BUILTIN_TYPE(p)) {
|
67
|
+
case T_ICLASS:
|
68
|
+
if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
|
69
|
+
if (!superclass_seen) {
|
70
|
+
c = p; /* move insertion point */
|
71
|
+
}
|
72
|
+
goto skip;
|
73
|
+
}
|
74
|
+
break;
|
75
|
+
case T_CLASS:
|
76
|
+
superclass_seen = TRUE;
|
77
|
+
break;
|
78
|
+
}
|
109
79
|
}
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
80
|
+
c = RCLASS_SUPER(c) = include_class_new(module, RCLASS_SUPER(c));
|
81
|
+
if (RMODULE_M_TBL(module) && RMODULE_M_TBL(module)->num_entries)
|
82
|
+
changed = 1;
|
83
|
+
skip:
|
84
|
+
module = RCLASS_SUPER(module);
|
85
|
+
}
|
86
|
+
if (changed) rb_clear_cache();
|
87
|
+
|
88
|
+
return Qnil;
|
114
89
|
}
|
115
90
|
|
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
91
|
|
125
92
|
void
|
126
93
|
Init_object2module()
|
127
94
|
{
|
128
|
-
|
129
|
-
rb_define_method(rb_cObject, "__reset_tbls__", reset_tbls, 0);
|
95
|
+
rb_define_method(rb_cModule, "gen_include_one", rb_gen_include_one, 1);
|
130
96
|
}
|
131
97
|
|
data/lib/object2module.rb
CHANGED
@@ -15,79 +15,75 @@ rescue LoadError => e
|
|
15
15
|
require "#{direc}/object2module.#{dlext}"
|
16
16
|
end
|
17
17
|
|
18
|
+
module Kernel
|
18
19
|
|
19
|
-
#
|
20
|
-
#
|
20
|
+
# define a `singleton_class` method for the 1.8 kids
|
21
|
+
# @return [Class] The singleton class of the receiver
|
22
|
+
def singleton_class
|
23
|
+
class << self; self; end
|
24
|
+
end if !respond_to?(:singleton_class)
|
25
|
+
end
|
21
26
|
|
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
27
|
class Object
|
42
|
-
def
|
43
|
-
raise ArgumentError, "wrong number of arguments (
|
28
|
+
def __gen_extend_or_include__(extend_or_include, *objs) #:nodoc:
|
29
|
+
raise ArgumentError, "wrong number of arguments (at least 1)" if objs.empty?
|
44
30
|
|
45
|
-
objs.each { |
|
46
|
-
|
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
|
31
|
+
objs.each { |mod|
|
32
|
+
send(extend_or_include, mod)
|
52
33
|
}
|
53
34
|
|
54
35
|
self
|
55
36
|
end
|
56
|
-
end
|
57
37
|
|
38
|
+
# Adds to the singleton class of receiver the instance methods from each object given as a
|
39
|
+
# parameter.
|
40
|
+
#
|
41
|
+
# @param [Array] objs The array of objects to `gen_extend`
|
42
|
+
# @return [Object] The receiver
|
43
|
+
# @example
|
44
|
+
# class C
|
45
|
+
# def hello
|
46
|
+
# "Hello from C.\n"
|
47
|
+
# end
|
48
|
+
# end
|
49
|
+
#
|
50
|
+
# class Klass
|
51
|
+
# def hello
|
52
|
+
# "Hello from Klass.\n"
|
53
|
+
# end
|
54
|
+
# end
|
55
|
+
#
|
56
|
+
# k = Klass.new
|
57
|
+
# k.hello #=> "Hello from Klass.\n"
|
58
|
+
# k.gen_extend(C) #=> #<Klass:0x401b3bc8>
|
59
|
+
# k.hello #=> "Hello from C.\n"
|
60
|
+
def gen_extend(*objs)
|
61
|
+
singleton_class.__gen_extend_or_include__(:gen_include_one, *objs)
|
62
|
+
end
|
63
|
+
end
|
58
64
|
|
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
65
|
class Module
|
66
|
+
|
67
|
+
# Adds to the implied receiver the instance methods from each object given as a
|
68
|
+
# parameter.
|
69
|
+
#
|
70
|
+
# @param [Array] objs The array of objects to `gen_include`
|
71
|
+
# @return [Object] The receiver
|
72
|
+
# @example
|
73
|
+
# class C
|
74
|
+
# def hello
|
75
|
+
# "Hello from C.\n"
|
76
|
+
# end
|
77
|
+
# end
|
78
|
+
#
|
79
|
+
# class Klass
|
80
|
+
# gen_include(C)
|
81
|
+
# end
|
82
|
+
#
|
83
|
+
# k = Klass.new
|
84
|
+
# k.hello #=> "Hello from C.\n"
|
78
85
|
def gen_include(*objs)
|
79
|
-
|
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
|
86
|
+
__gen_extend_or_include__(:gen_include_one, *objs)
|
91
87
|
end
|
92
88
|
end
|
93
89
|
|
data/test/test.rb
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
direc = File.dirname(__FILE__)
|
2
|
+
require 'rubygems'
|
3
|
+
require 'bacon'
|
4
|
+
require "#{direc}/../lib/object2module"
|
5
|
+
|
6
|
+
|
7
|
+
class Module
|
8
|
+
public :include, :remove_const
|
9
|
+
end
|
10
|
+
|
11
|
+
describe Object2module do
|
12
|
+
before do
|
13
|
+
class A
|
14
|
+
def a
|
15
|
+
:a
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class B
|
20
|
+
def b
|
21
|
+
:b
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
module M
|
26
|
+
def m
|
27
|
+
:m
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
O = Object.new
|
32
|
+
class << O
|
33
|
+
def o
|
34
|
+
:o
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
C = Class.new
|
39
|
+
end
|
40
|
+
|
41
|
+
after do
|
42
|
+
Object.remove_const(:A)
|
43
|
+
Object.remove_const(:B)
|
44
|
+
Object.remove_const(:C)
|
45
|
+
Object.remove_const(:M)
|
46
|
+
Object.remove_const(:O)
|
47
|
+
end
|
48
|
+
|
49
|
+
describe 'gen_include' do
|
50
|
+
it 'includes a module' do
|
51
|
+
C.gen_include M
|
52
|
+
C.new.m.should == :m
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'includes a class' do
|
56
|
+
C.gen_include A
|
57
|
+
C.new.a.should == :a
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'includes an object' do
|
61
|
+
C.gen_include O
|
62
|
+
C.new.o.should == :o
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'includes a class that includes a class' do
|
66
|
+
A.gen_include B
|
67
|
+
C.gen_include A
|
68
|
+
C.new.b.should == :b
|
69
|
+
C.new.a.should == :a
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'includes an object that includes a class that includes a class' do
|
73
|
+
A.gen_include B
|
74
|
+
O.gen_extend A
|
75
|
+
C.gen_include O
|
76
|
+
C.new.o.should == :o
|
77
|
+
C.new.a.should == :a
|
78
|
+
C.new.b.should == :b
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'includes an object that includes an object' do
|
82
|
+
n = Object.new
|
83
|
+
class << n
|
84
|
+
def n
|
85
|
+
:n
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
l = Object.new
|
90
|
+
class << l
|
91
|
+
def l
|
92
|
+
:l
|
93
|
+
end
|
94
|
+
self
|
95
|
+
end.gen_include n
|
96
|
+
|
97
|
+
C.gen_include l
|
98
|
+
C.new.l.should == :l
|
99
|
+
C.new.n.should == :n
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe 'gen_extend' do
|
104
|
+
it 'extends a module' do
|
105
|
+
O.gen_extend M
|
106
|
+
O.m.should == :m
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'extends a class' do
|
110
|
+
O.gen_extend A
|
111
|
+
O.a.should == :a
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'extends an object' do
|
115
|
+
n = Object.new
|
116
|
+
class << n
|
117
|
+
def n
|
118
|
+
:n
|
119
|
+
end
|
120
|
+
end
|
121
|
+
O.gen_extend n
|
122
|
+
O.n.should == :n
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'extends a class that includes a class' do
|
126
|
+
A.gen_include B
|
127
|
+
O.gen_extend A
|
128
|
+
O.b.should == :b
|
129
|
+
O.a.should == :a
|
130
|
+
end
|
131
|
+
|
132
|
+
it 'extends an object that includes a class that includes a class' do
|
133
|
+
A.gen_include B
|
134
|
+
C.gen_include A
|
135
|
+
O.gen_extend C
|
136
|
+
O.o.should == :o
|
137
|
+
O.a.should == :a
|
138
|
+
O.b.should == :b
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'extends an object that extends an object' do
|
142
|
+
n = Object.new
|
143
|
+
class << n
|
144
|
+
def n
|
145
|
+
:n
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
l = Object.new
|
150
|
+
class << l
|
151
|
+
def l
|
152
|
+
:l
|
153
|
+
end
|
154
|
+
self
|
155
|
+
end
|
156
|
+
|
157
|
+
l.gen_extend n
|
158
|
+
|
159
|
+
O.gen_extend l
|
160
|
+
O.l.should == :l
|
161
|
+
O.n.should == :n
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
data/test/test_stress.rb
ADDED
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: object2module
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 19
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
|
-
-
|
7
|
+
- 4
|
9
8
|
- 0
|
10
|
-
version: 0.
|
9
|
+
version: 0.4.0
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- John Mair (banisterfiend)
|
@@ -15,7 +14,7 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date: 2010-
|
17
|
+
date: 2010-10-31 00:00:00 +13:00
|
19
18
|
default_executable:
|
20
19
|
dependencies: []
|
21
20
|
|
@@ -33,7 +32,8 @@ files:
|
|
33
32
|
- LICENSE
|
34
33
|
- lib/object2module.rb
|
35
34
|
- lib/object2module/version.rb
|
36
|
-
- test/
|
35
|
+
- test/test.rb
|
36
|
+
- test/test_stress.rb
|
37
37
|
- ext/object2module/extconf.rb
|
38
38
|
- ext/object2module/compat.h
|
39
39
|
- ext/object2module/object2module.h
|
@@ -52,7 +52,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
52
52
|
requirements:
|
53
53
|
- - ">="
|
54
54
|
- !ruby/object:Gem::Version
|
55
|
-
hash: 3
|
56
55
|
segments:
|
57
56
|
- 0
|
58
57
|
version: "0"
|
@@ -61,7 +60,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
60
|
requirements:
|
62
61
|
- - ">="
|
63
62
|
- !ruby/object:Gem::Version
|
64
|
-
hash: 3
|
65
63
|
segments:
|
66
64
|
- 0
|
67
65
|
version: "0"
|
data/test/test_object2module.rb
DELETED
@@ -1,147 +0,0 @@
|
|
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
|