object2module 0.4.5 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,15 +17,15 @@ class_to_s(VALUE self)
17
17
 
18
18
  if (attached) {
19
19
  VALUE val = rb_iv_get(attached, "__module__");
20
- if (NIL_P(val))
21
- return rb_str_new2("Anon");
20
+ /* if (NIL_P(val)) */
21
+ /* return rb_str_new2("Anon"); */
22
22
 
23
23
  name = rb_mod_name(val);
24
24
  }
25
25
  else {
26
26
  VALUE val = rb_iv_get(attached, "__module__");
27
- if (NIL_P(val))
28
- return rb_str_new2("Anon");
27
+ /* if (NIL_P(val)) */
28
+ /* return rb_str_new2("Anon"); */
29
29
 
30
30
  name = rb_mod_name(val);
31
31
  }
@@ -37,6 +37,20 @@ class_to_s(VALUE self)
37
37
  return name;
38
38
  }
39
39
 
40
+ // also returns true for receiver
41
+ static VALUE
42
+ is_meta_singleton_of(VALUE self, VALUE obj)
43
+ {
44
+ if (self == obj)
45
+ return Qtrue;
46
+ else if (!FL_TEST(self, FL_SINGLETON) && self != obj)
47
+ return Qfalse;
48
+ else {
49
+ VALUE attached = rb_iv_get(self, "__attached__");
50
+ return is_meta_singleton_of(attached, obj);
51
+ }
52
+ }
53
+
40
54
  static VALUE
41
55
  include_class_new(VALUE module, VALUE super)
42
56
  {
@@ -51,8 +65,8 @@ include_class_new(VALUE module, VALUE super)
51
65
  module = RBASIC(module)->klass;
52
66
  }
53
67
 
54
- rb_define_singleton_method(module, "to_s", class_to_s, 0);
55
- rb_define_method(module, "to_s", class_to_s, 0);
68
+ // rb_define_singleton_method(module, "to_s", class_to_s, 0);
69
+ // rb_define_method(module, "to_s", class_to_s, 0);
56
70
 
57
71
  if (!RCLASS_IV_TBL(module)) {
58
72
  RCLASS_IV_TBL(module) = st_init_numtable();
@@ -90,7 +104,9 @@ rb_gen_include_one(VALUE klass, VALUE module)
90
104
 
91
105
  OBJ_INFECT(klass, module);
92
106
  c = klass;
93
- while (module) {
107
+
108
+ // loop until superclass is 0 (for modules) or superclass is a meta^n singleton of Object (for classes)
109
+ while (module && !rb_is_meta_singleton_of(module, rb_cObject)) {
94
110
  int superclass_seen = FALSE;
95
111
 
96
112
  if (RCLASS_M_TBL(klass) == RCLASS_M_TBL(module))
@@ -126,6 +142,9 @@ rb_gen_include_one(VALUE klass, VALUE module)
126
142
  void
127
143
  Init_object2module()
128
144
  {
129
- rb_define_method(rb_cModule, "gen_include_one", rb_gen_include_one, 1);
145
+ VALUE mObject2module = rb_define_module("Object2module");
146
+ VALUE mModuleExtensions = rb_define_module_under(mObject2module, "ModuleExtensions");
147
+
148
+ rb_define_method(mModuleExtensions, "gen_include_one", rb_gen_include_one, 1);
130
149
  }
131
150
 
@@ -1,18 +1,17 @@
1
1
  direc = File.dirname(__FILE__)
2
+ dlext = Config::CONFIG['DLEXT']
2
3
 
3
- require 'rbconfig'
4
4
  require "#{direc}/object2module/version"
5
5
 
6
- dlext = Config::CONFIG['DLEXT']
7
-
8
6
  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
7
+ if RUBY_VERSION && RUBY_VERSION =~ /1.9/
8
+ require "#{direc}/1.9/object2module"
9
+ else
10
+ require "#{direc}/1.8/object2module"
11
+ end
14
12
  rescue LoadError => e
15
- require "#{direc}/object2module.#{dlext}"
13
+ require 'rbconfig'
14
+ require "#{direc}/object2module.#{dlext}"
16
15
  end
17
16
 
18
17
  module Kernel
@@ -24,68 +23,76 @@ module Kernel
24
23
  end if !respond_to?(:singleton_class)
25
24
  end
26
25
 
27
- class Object
28
- def __gen_extend_or_include__(extend_or_include, *objs) #:nodoc:
29
- raise ArgumentError, "wrong number of arguments (at least 1)" if objs.empty?
26
+ module Object2module
27
+ module ObjectExtensions
28
+ def __gen_extend_or_include__(extend_or_include, *objs) #:nodoc:
29
+ raise ArgumentError, "wrong number of arguments (at least 1)" if objs.empty?
30
30
 
31
- objs.reverse.each { |mod|
31
+ objs.reverse.each { |mod|
32
32
  send(extend_or_include, mod)
33
- }
33
+ }
34
+
35
+ self
36
+ end
34
37
 
35
- self
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
36
63
  end
64
+
65
+ module ModuleExtensions
37
66
 
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)
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"
85
+ def gen_include(*objs)
86
+ __gen_extend_or_include__(:gen_include_one, *objs)
87
+ end
62
88
  end
63
89
  end
64
90
 
91
+ class Object
92
+ include Object2module::ObjectExtensions
93
+ end
94
+
65
95
  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"
85
- def gen_include(*objs)
86
- __gen_extend_or_include__(:gen_include_one, *objs)
87
- end
96
+ include Object2module::ModuleExtensions
88
97
  end
89
98
 
90
-
91
-
@@ -1,3 +1,3 @@
1
1
  module Object2module
2
- VERSION = "0.4.5"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -99,6 +99,17 @@ describe Object2module do
99
99
  C.new.l.should == :l
100
100
  C.new.n.should == :n
101
101
  end
102
+
103
+ it 'should not not re-include Object when gen_including a class' do
104
+ A.gen_include B
105
+ A.ancestors.count(Object).should == 1
106
+ end
107
+
108
+ it 'should not include singleton of Object when gen_including a singleton class' do
109
+ A.gen_include B.singleton_class
110
+ A.ancestors.include?(B.singleton_class).should == true
111
+ A.ancestors.count(Object.singleton_class).should == 0
112
+ end
102
113
  end
103
114
 
104
115
  describe 'gen_extend' do
@@ -0,0 +1,166 @@
1
+ direc = File.dirname(__FILE__)
2
+ require 'rubygems'
3
+ require 'bacon'
4
+ require "#{direc}/../lib/object2module"
5
+
6
+ class Module
7
+ public :include, :remove_const
8
+ end
9
+
10
+ puts "testing Object2module version #{Object2module::VERSION}..."
11
+
12
+ describe Object2module do
13
+ before do
14
+ class A
15
+ def a
16
+ :a
17
+ end
18
+ end
19
+
20
+ class B
21
+ def b
22
+ :b
23
+ end
24
+ end
25
+
26
+ module M
27
+ def m
28
+ :m
29
+ end
30
+ end
31
+
32
+ O = Object.new
33
+ class << O
34
+ def o
35
+ :o
36
+ end
37
+ end
38
+
39
+ C = Class.new
40
+ end
41
+
42
+ after do
43
+ Object.remove_const(:A)
44
+ Object.remove_const(:B)
45
+ Object.remove_const(:C)
46
+ Object.remove_const(:M)
47
+ Object.remove_const(:O)
48
+ end
49
+
50
+ describe 'gen_include' do
51
+ it 'includes a module' do
52
+ C.gen_include M
53
+ C.new.m.should == :m
54
+ end
55
+
56
+ it 'includes a class' do
57
+ C.gen_include A
58
+ C.new.a.should == :a
59
+ end
60
+
61
+ it 'includes an object' do
62
+ C.gen_include O
63
+ C.new.o.should == :o
64
+ end
65
+
66
+ it 'includes a class that includes a class' do
67
+ A.gen_include B
68
+ C.gen_include A
69
+ C.new.b.should == :b
70
+ C.new.a.should == :a
71
+ end
72
+
73
+ it 'includes an object that includes a class that includes a class' do
74
+ A.gen_include B
75
+ O.gen_extend A
76
+ C.gen_include O
77
+ C.new.o.should == :o
78
+ C.new.a.should == :a
79
+ C.new.b.should == :b
80
+ end
81
+
82
+ it 'includes an object that includes an object' do
83
+ n = Object.new
84
+ class << n
85
+ def n
86
+ :n
87
+ end
88
+ end
89
+
90
+ l = Object.new
91
+ class << l
92
+ def l
93
+ :l
94
+ end
95
+ self
96
+ end.gen_include n
97
+
98
+ C.gen_include l
99
+ C.new.l.should == :l
100
+ C.new.n.should == :n
101
+ end
102
+ end
103
+
104
+ describe 'gen_extend' do
105
+ it 'extends a module' do
106
+ O.gen_extend M
107
+ O.m.should == :m
108
+ end
109
+
110
+ it 'extends a class' do
111
+ O.gen_extend A
112
+ O.a.should == :a
113
+ end
114
+
115
+ it 'extends an object' do
116
+ n = Object.new
117
+ class << n
118
+ def n
119
+ :n
120
+ end
121
+ end
122
+ O.gen_extend n
123
+ O.n.should == :n
124
+ end
125
+
126
+ it 'extends a class that includes a class' do
127
+ A.gen_include B
128
+ O.gen_extend A
129
+ O.b.should == :b
130
+ O.a.should == :a
131
+ end
132
+
133
+ it 'extends an object that includes a class that includes a class' do
134
+ A.gen_include B
135
+ C.gen_include A
136
+ O.gen_extend C
137
+ O.o.should == :o
138
+ O.a.should == :a
139
+ O.b.should == :b
140
+ end
141
+
142
+ it 'extends an object that extends an object' do
143
+ n = Object.new
144
+ class << n
145
+ def n
146
+ :n
147
+ end
148
+ end
149
+
150
+ l = Object.new
151
+ class << l
152
+ def l
153
+ :l
154
+ end
155
+ self
156
+ end
157
+
158
+ l.gen_extend n
159
+
160
+ O.gen_extend l
161
+ O.l.should == :l
162
+ O.n.should == :n
163
+ end
164
+ end
165
+ end
166
+
@@ -0,0 +1,16 @@
1
+ direc = File.dirname(__FILE__)
2
+ require "#{direc}/../lib/object2module"
3
+
4
+
5
+ class C; end
6
+
7
+ class F
8
+ gen_include C
9
+ end
10
+
11
+ class D
12
+ gen_include C.singleton_class.singleton_class
13
+ # gen_include F
14
+ end
15
+
16
+ puts D.ancestors.inspect.display
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 4
8
7
  - 5
9
- version: 0.4.5
8
+ - 0
9
+ version: 0.5.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - John Mair (banisterfiend)
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-11-16 00:00:00 +13:00
17
+ date: 2010-11-17 00:00:00 +13:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -33,13 +33,15 @@ files:
33
33
  - lib/object2module/version.rb
34
34
  - lib/object2module.rb
35
35
  - test/test.rb
36
+ - test/test_flymake.rb
37
+ - test/test_simple.rb
36
38
  - test/test_stress.rb
37
39
  - test/test_with_remix.rb
38
40
  - ext/object2module/extconf.rb
39
41
  - ext/object2module/compat.h
40
42
  - ext/object2module/object2module.h
41
43
  - ext/object2module/object2module.c
42
- has_rdoc: true
44
+ has_rdoc: yard
43
45
  homepage: http://banisterfiend.wordpress.com
44
46
  licenses: []
45
47