object2module 0.4.0-i386-mingw32 → 0.4.2-i386-mingw32
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/README.markdown +75 -0
- data/Rakefile +3 -10
- data/lib/1.8/object2module.so +0 -0
- data/lib/1.9/object2module.so +0 -0
- data/lib/object2module.rb +1 -1
- data/lib/object2module/version.rb +1 -1
- data/test/test_with_remix.rb +147 -0
- metadata +8 -4
- data/README +0 -18
data/README.markdown
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
Object2module
|
2
|
+
=============
|
3
|
+
|
4
|
+
(C) John Mair (banisterfiend) 2010
|
5
|
+
|
6
|
+
_Enables Classes and Objects to be mixed into ancestor chains_
|
7
|
+
|
8
|
+
Using Object2module you can treat classes and object (their singletons, anyway) as if they were modules and include or extend them into ancestor chains.
|
9
|
+
|
10
|
+
Object2module provides the `gen_include` and `gen_extend` methods which are generalizations of the traditional `include` and `extend`.
|
11
|
+
|
12
|
+
* Install the [gem](https://rubygems.org/gems/object2module): `gem install object2module`
|
13
|
+
* Read the [documentation](http://rdoc.info/github/banister/object2module/master/file/README.markdown)
|
14
|
+
* See the [source code](http://github.com/banister/object2module)
|
15
|
+
|
16
|
+
example: gen_include()
|
17
|
+
--------------------------
|
18
|
+
|
19
|
+
Using `gen_include` we can include a class into another class:
|
20
|
+
|
21
|
+
|
22
|
+
class C
|
23
|
+
def hello
|
24
|
+
:hello
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class D
|
29
|
+
gen_include C
|
30
|
+
end
|
31
|
+
|
32
|
+
D.new.hello #=> :hello
|
33
|
+
D.ancestors #=> [D, C, Object, ...]
|
34
|
+
|
35
|
+
example: gen_extend()
|
36
|
+
--------------------
|
37
|
+
|
38
|
+
`gen_extend` lets us mix objects into objects:
|
39
|
+
|
40
|
+
o = Object.new
|
41
|
+
class << o
|
42
|
+
def bye
|
43
|
+
:bye
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
n = Object.new
|
48
|
+
n.gen_extend o
|
49
|
+
n.bye #=> :bye
|
50
|
+
|
51
|
+
How it works
|
52
|
+
--------------
|
53
|
+
|
54
|
+
Object2module simply removes the check for `T_MODULE` from `rb_include_module()`
|
55
|
+
|
56
|
+
Companion Libraries
|
57
|
+
--------------------
|
58
|
+
|
59
|
+
Remix is one of a series of experimental libraries that mess with
|
60
|
+
the internals of Ruby to bring new and interesting functionality to
|
61
|
+
the language, see also:
|
62
|
+
|
63
|
+
* [Remix](http://github.com/banister/remix) - Makes ancestor chains read/write
|
64
|
+
* [Real Include](http://github.com/banister/real_include) - Brings in
|
65
|
+
module singleton classes during an include. No more ugly ClassMethods and included() hook hacks.
|
66
|
+
* [Prepend](http://github.com/banister/prepend) - Prepends modules in front of a class; so method lookup starts with the module
|
67
|
+
* [GenEval](http://github.com/banister/gen_eval) - A strange new breed of instance_eval
|
68
|
+
|
69
|
+
Contact
|
70
|
+
-------
|
71
|
+
|
72
|
+
Problems or questions contact me at [github](http://github.com/banister)
|
73
|
+
|
74
|
+
|
75
|
+
|
data/Rakefile
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
# Rakefile added by John Mair (banisterfiend)
|
2
2
|
|
3
3
|
require 'rake/gempackagetask'
|
4
|
-
require 'rake/rdoctask'
|
5
4
|
require 'rake/clean'
|
6
5
|
require './lib/object2module/version.rb'
|
7
6
|
|
@@ -11,7 +10,6 @@ direc = File.dirname(__FILE__)
|
|
11
10
|
CLEAN.include("ext/**/*.#{dlext}", "ext/**/.log", "ext/**/.o", "ext/**/*~", "ext/**/*#*", "ext/**/.obj", "ext/**/.def", "ext/**/.pdb")
|
12
11
|
CLOBBER.include("**/*.#{dlext}", "**/*~", "**/*#*", "**/*.log", "**/*.o", "doc/**")
|
13
12
|
|
14
|
-
|
15
13
|
def apply_spec_defaults(s)
|
16
14
|
s.name = "object2module"
|
17
15
|
s.summary = "object2module enables ruby classes and objects to be used as modules"
|
@@ -19,13 +17,12 @@ def apply_spec_defaults(s)
|
|
19
17
|
s.version = Object2module::VERSION
|
20
18
|
s.author = "John Mair (banisterfiend)"
|
21
19
|
s.email = 'jrmair@gmail.com'
|
22
|
-
s.has_rdoc =
|
20
|
+
s.has_rdoc = 'yard'
|
23
21
|
s.date = Time.now.strftime '%Y-%m-%d'
|
24
22
|
s.require_path = 'lib'
|
25
23
|
s.homepage = "http://banisterfiend.wordpress.com"
|
26
24
|
end
|
27
25
|
|
28
|
-
|
29
26
|
task :test do
|
30
27
|
sh "bacon -k #{direc}/test/test.rb"
|
31
28
|
end
|
@@ -35,7 +32,7 @@ end
|
|
35
32
|
spec = Gem::Specification.new do |s|
|
36
33
|
apply_spec_defaults(s)
|
37
34
|
s.platform = "i386-#{v}"
|
38
|
-
s.files = FileList["Rakefile", "README", "LICENSE",
|
35
|
+
s.files = FileList["Rakefile", "README.markdown", "LICENSE",
|
39
36
|
"lib/object2module.rb", "lib/1.8/object2module.#{dlext}",
|
40
37
|
"lib/1.9/object2module.#{dlext}", "lib/object2module/version.rb", "test/*.rb"].to_a
|
41
38
|
end
|
@@ -53,7 +50,7 @@ task :ruby do
|
|
53
50
|
spec = Gem::Specification.new do |s|
|
54
51
|
apply_spec_defaults(s)
|
55
52
|
s.platform = Gem::Platform::RUBY
|
56
|
-
s.files = FileList["Rakefile", "README", "LICENSE",
|
53
|
+
s.files = FileList["Rakefile", "README.markdown", "LICENSE",
|
57
54
|
"lib/object2module.rb","lib/object2module/version.rb",
|
58
55
|
"test/*.rb", "ext/**/extconf.rb", "ext/**/*.h", "ext/**/*.c"].to_a
|
59
56
|
s.extensions = ["ext/object2module/extconf.rb"]
|
@@ -67,7 +64,3 @@ task :ruby do
|
|
67
64
|
Rake::Task[:gem].invoke
|
68
65
|
end
|
69
66
|
|
70
|
-
Rake::RDocTask.new do |rd|
|
71
|
-
rd.main = "README.rdoc"
|
72
|
-
rd.rdoc_files.include("README.rdoc", "lib/object2module.rb")
|
73
|
-
end
|
data/lib/1.8/object2module.so
CHANGED
Binary file
|
data/lib/1.9/object2module.so
CHANGED
Binary file
|
data/lib/object2module.rb
CHANGED
@@ -0,0 +1,147 @@
|
|
1
|
+
direc = File.dirname(__FILE__)
|
2
|
+
require 'rubygems'
|
3
|
+
require 'bacon'
|
4
|
+
require 'remix'
|
5
|
+
require "#{direc}/../lib/object2module"
|
6
|
+
|
7
|
+
|
8
|
+
class Module
|
9
|
+
public :include, :remove_const
|
10
|
+
end
|
11
|
+
|
12
|
+
describe Object2module do
|
13
|
+
before do
|
14
|
+
class A
|
15
|
+
def hello
|
16
|
+
:a
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class B
|
21
|
+
def hello
|
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 two classes and swaps them' do
|
52
|
+
C.gen_include A
|
53
|
+
C.gen_include B
|
54
|
+
C.new.hello.should == :b
|
55
|
+
C.swap_modules A, B
|
56
|
+
C.new.hello.should == :a
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'includes a class into a class and swaps them' do
|
60
|
+
A.gen_include B
|
61
|
+
C.gen_include A
|
62
|
+
C.new.hello.should == :a
|
63
|
+
C.swap_modules A, B
|
64
|
+
C.new.hello.should == :b
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'unincludes a gen_included class' do
|
68
|
+
C.gen_include A
|
69
|
+
C.new.hello.should == :a
|
70
|
+
C.uninclude A
|
71
|
+
lambda { C.new.hello }.should.raise NameError
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'recursively unincludes a gen_included class' do
|
75
|
+
A.gen_include B
|
76
|
+
C.gen_include A
|
77
|
+
C.new.hello.should == :a
|
78
|
+
C.ancestors.should[0..2] == [C, A, B]
|
79
|
+
C.uninclude A, true
|
80
|
+
C.ancestors.should[0..1] == [C, Object]
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'unincludes a singleton class' do
|
84
|
+
o = Object.new
|
85
|
+
class << o
|
86
|
+
def hello
|
87
|
+
:o
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
C.gen_include o
|
92
|
+
C.new.hello.should == :o
|
93
|
+
C.uninclude C.ancestors[1]
|
94
|
+
lambda { C.new.hello }.should.raise NameError
|
95
|
+
C.ancestors[1].should == Object
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe 'gen_extend' do
|
100
|
+
it 'extends two classes into an object and swaps them' do
|
101
|
+
o = Object.new
|
102
|
+
o.gen_extend A, B
|
103
|
+
o.hello.should == :a
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'unextends a class from an object' do
|
107
|
+
o = Object.new
|
108
|
+
o.gen_extend A
|
109
|
+
o.hello.should == :a
|
110
|
+
o.singleton_class.ancestors[0].should == A
|
111
|
+
o.unextend A
|
112
|
+
lambda { o.hello }.should.raise NameError
|
113
|
+
o.singleton_class.ancestors[0].should == Object
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'recursively unextends a class from an object' do
|
117
|
+
o = Object.new
|
118
|
+
A.gen_include B
|
119
|
+
o.gen_extend A
|
120
|
+
o.singleton_class.ancestors[0..2].should == [A, B, Object]
|
121
|
+
o.unextend A, true
|
122
|
+
o.singleton_class.ancestors.first.should == Object
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'recursively unextends a singleton class gen_extended into another singleton class' do
|
126
|
+
o = Object.new
|
127
|
+
def o.hello
|
128
|
+
:o
|
129
|
+
end
|
130
|
+
|
131
|
+
n = Object.new
|
132
|
+
def n.hello
|
133
|
+
:n
|
134
|
+
end
|
135
|
+
|
136
|
+
n.gen_extend o
|
137
|
+
|
138
|
+
v = Object.new
|
139
|
+
v.gen_extend n
|
140
|
+
|
141
|
+
v.hello.should == :n
|
142
|
+
v.unextend n.singleton_class, true
|
143
|
+
lambda { v.hello }.should.raise NameError
|
144
|
+
v.singleton_class.ancestors.first.should == Object
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: object2module
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 11
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 0
|
7
8
|
- 4
|
8
|
-
-
|
9
|
-
version: 0.4.
|
9
|
+
- 2
|
10
|
+
version: 0.4.2
|
10
11
|
platform: i386-mingw32
|
11
12
|
authors:
|
12
13
|
- John Mair (banisterfiend)
|
@@ -28,7 +29,7 @@ extra_rdoc_files: []
|
|
28
29
|
|
29
30
|
files:
|
30
31
|
- Rakefile
|
31
|
-
- README
|
32
|
+
- README.markdown
|
32
33
|
- LICENSE
|
33
34
|
- lib/object2module.rb
|
34
35
|
- lib/1.8/object2module.so
|
@@ -36,7 +37,8 @@ files:
|
|
36
37
|
- lib/object2module/version.rb
|
37
38
|
- test/test.rb
|
38
39
|
- test/test_stress.rb
|
39
|
-
|
40
|
+
- test/test_with_remix.rb
|
41
|
+
has_rdoc: yard
|
40
42
|
homepage: http://banisterfiend.wordpress.com
|
41
43
|
licenses: []
|
42
44
|
|
@@ -50,6 +52,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
50
52
|
requirements:
|
51
53
|
- - ">="
|
52
54
|
- !ruby/object:Gem::Version
|
55
|
+
hash: 3
|
53
56
|
segments:
|
54
57
|
- 0
|
55
58
|
version: "0"
|
@@ -58,6 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
61
|
requirements:
|
59
62
|
- - ">="
|
60
63
|
- !ruby/object:Gem::Version
|
64
|
+
hash: 3
|
61
65
|
segments:
|
62
66
|
- 0
|
63
67
|
version: "0"
|
data/README
DELETED
@@ -1,18 +0,0 @@
|
|
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
|
-
|