object2module 0.3.0-i386-mswin32
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 +14 -0
- data/README +18 -0
- data/Rakefile +53 -0
- data/lib/1.8/object2module.so +0 -0
- data/lib/1.9/object2module.so +0 -0
- data/lib/object2module/version.rb +3 -0
- data/lib/object2module.rb +95 -0
- data/test/test_object2module.rb +147 -0
- metadata +74 -0
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
|
+
|
data/README
ADDED
@@ -0,0 +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
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
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
|
Binary file
|
Binary file
|
@@ -0,0 +1,95 @@
|
|
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,147 @@
|
|
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
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: object2module
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 3
|
9
|
+
- 0
|
10
|
+
version: 0.3.0
|
11
|
+
platform: i386-mswin32
|
12
|
+
authors:
|
13
|
+
- John Mair (banisterfiend)
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-07-29 00:00:00 +12:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: object2module enables ruby classes and objects to be used as modules
|
23
|
+
email: jrmair@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- Rakefile
|
32
|
+
- README
|
33
|
+
- LICENSE
|
34
|
+
- lib/object2module.rb
|
35
|
+
- lib/1.8/object2module.so
|
36
|
+
- lib/1.9/object2module.so
|
37
|
+
- lib/object2module/version.rb
|
38
|
+
- test/test_object2module.rb
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://banisterfiend.wordpress.com
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
hash: 3
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 3
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.3.7
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: object2module enables ruby classes and objects to be used as modules
|
73
|
+
test_files: []
|
74
|
+
|