mixology 0.1.0-mswin32
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +72 -0
- data/ext/mixology/extconf.rb +3 -0
- data/ext/mixology/mixology.c +81 -0
- data/lib/mixology.so +0 -0
- data/test/mixology_test.rb +170 -0
- data/test/test_helper.rb +20 -0
- metadata +51 -0
data/Rakefile
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
require "rake"
|
2
|
+
require 'rake/clean'
|
3
|
+
require 'rake/gempackagetask'
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
desc "clean, compile, test"
|
7
|
+
task :default => %w[clean compile test]
|
8
|
+
|
9
|
+
Rake::TestTask.new("test") do |t|
|
10
|
+
t.pattern = "test/**/*_test.rb"
|
11
|
+
end
|
12
|
+
|
13
|
+
desc "Builds the extension"
|
14
|
+
if RUBY_PLATFORM =~ /java/
|
15
|
+
task :compile => :compile_java
|
16
|
+
else
|
17
|
+
task :compile => %W[ext/mixology/Makefile ext/mixology/mixology.#{Config::CONFIG['DLEXT']}]
|
18
|
+
end
|
19
|
+
|
20
|
+
file "ext/mixology/Makefile" => ["ext/mixology/extconf.rb"] do
|
21
|
+
Dir.chdir("ext/mixology") do
|
22
|
+
ruby "extconf.rb"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
file "ext/mixology/mixology.#{Config::CONFIG['DLEXT']}" do
|
27
|
+
Dir.chdir("ext/mixology") do
|
28
|
+
sh "make"
|
29
|
+
end
|
30
|
+
cp "ext/mixology/mixology.#{Config::CONFIG['DLEXT']}", "lib"
|
31
|
+
end
|
32
|
+
|
33
|
+
CLEAN.include %w[ext/mixology/Makefile ext/mixology/mixology.bundle lib/mixology.bundle]
|
34
|
+
CLEAN.include %w[ext/mixology/MixableService.class ext/mixology/mixable.jar lib/mixology.jar]
|
35
|
+
|
36
|
+
Gem::manage_gems
|
37
|
+
|
38
|
+
specification = Gem::Specification.new do |s|
|
39
|
+
s.name = "mixology"
|
40
|
+
s.summary = "Mixology enables objects to mixin and unmix modules."
|
41
|
+
s.version = "0.1.0"
|
42
|
+
s.author = "anonymous z, Pat Farley, Dan Manges"
|
43
|
+
s.description = s.summary
|
44
|
+
s.homepage = "http://mixology.rubyforge.org"
|
45
|
+
s.rubyforge_project = "mixology"
|
46
|
+
s.has_rdoc = false
|
47
|
+
s.autorequire = "mixology"
|
48
|
+
s.files = FileList['ext/**/*.{c,rb}', '{lib,test}/**/*.rb', '^[A-Z]+$', 'Rakefile'].to_a
|
49
|
+
if true#RUBY_PLATFORM =~ /mswin/
|
50
|
+
s.platform = Gem::Platform::WIN32
|
51
|
+
s.files += ["lib/mixology.so"]
|
52
|
+
elsif RUBY_PLATFORM =~ /java/
|
53
|
+
s.platform = "jruby"
|
54
|
+
s.files += ["lib/mixology.jar"]
|
55
|
+
else
|
56
|
+
s.platform = Gem::Platform::RUBY
|
57
|
+
s.extensions = FileList["ext/**/extconf.rb"].to_a
|
58
|
+
end
|
59
|
+
end
|
60
|
+
Rake::GemPackageTask.new(specification) do |package|
|
61
|
+
package.need_zip = false
|
62
|
+
package.need_tar = false
|
63
|
+
end
|
64
|
+
|
65
|
+
desc "Compiles the JRuby extension"
|
66
|
+
task :compile_java do
|
67
|
+
Dir.chdir("ext/mixology") do
|
68
|
+
sh %{javac -source 1.4 -target 1.4 -classpath $JRUBY_HOME/lib/jruby.jar MixologyService.java}
|
69
|
+
sh %{jar cf mixology.jar MixologyService.class}
|
70
|
+
cp "mixology.jar", "../../lib/mixology.jar"
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
#include "ruby.h"
|
2
|
+
|
3
|
+
static void remove_nested_module(VALUE klass, VALUE include_class) {
|
4
|
+
|
5
|
+
if(RBASIC(RCLASS(klass)->super)->klass != RBASIC(RCLASS(include_class)->super)->klass) {
|
6
|
+
return;
|
7
|
+
}
|
8
|
+
if(RCLASS(RCLASS(include_class)->super)->super && BUILTIN_TYPE(RCLASS(include_class)->super) == T_ICLASS) {
|
9
|
+
remove_nested_module(RCLASS(klass)->super, RCLASS(include_class)->super);
|
10
|
+
}
|
11
|
+
RCLASS(klass)->super = RCLASS(RCLASS(klass)->super)->super;
|
12
|
+
}
|
13
|
+
|
14
|
+
static VALUE rb_unmix(VALUE self, VALUE module) {
|
15
|
+
VALUE klass;
|
16
|
+
for (klass = RBASIC(self)->klass; klass != rb_class_real(klass); klass = RCLASS(klass)->super) {
|
17
|
+
VALUE super = RCLASS(klass)->super;
|
18
|
+
if (BUILTIN_TYPE(super) == T_ICLASS) {
|
19
|
+
if (RBASIC(super)->klass == module) {
|
20
|
+
if(RCLASS(module)->super && BUILTIN_TYPE(RCLASS(module)->super) == T_ICLASS)
|
21
|
+
remove_nested_module(super, module);
|
22
|
+
RCLASS(klass)->super = RCLASS(RCLASS(klass)->super)->super;
|
23
|
+
rb_clear_cache();
|
24
|
+
}
|
25
|
+
}
|
26
|
+
}
|
27
|
+
return self;
|
28
|
+
}
|
29
|
+
|
30
|
+
static void add_module(VALUE self, VALUE module) {
|
31
|
+
VALUE super = RCLASS(rb_singleton_class(self))->super;
|
32
|
+
NEWOBJ(klass, struct RClass);
|
33
|
+
OBJSETUP(klass, rb_cClass, T_ICLASS);
|
34
|
+
|
35
|
+
if (BUILTIN_TYPE(module) == T_ICLASS) {
|
36
|
+
module = RBASIC(module)->klass;
|
37
|
+
}
|
38
|
+
if (!RCLASS(module)->iv_tbl) {
|
39
|
+
RCLASS(module)->iv_tbl = (void*)st_init_numtable();
|
40
|
+
}
|
41
|
+
klass->iv_tbl = RCLASS(module)->iv_tbl;
|
42
|
+
klass->m_tbl = RCLASS(module)->m_tbl;
|
43
|
+
klass->super = super;
|
44
|
+
if (TYPE(module) == T_ICLASS) {
|
45
|
+
RBASIC(klass)->klass = RBASIC(module)->klass;
|
46
|
+
}
|
47
|
+
else {
|
48
|
+
RBASIC(klass)->klass = module;
|
49
|
+
}
|
50
|
+
OBJ_INFECT(klass, module);
|
51
|
+
OBJ_INFECT(klass, super);
|
52
|
+
|
53
|
+
RCLASS(rb_singleton_class(self))->super = (int)klass;
|
54
|
+
|
55
|
+
}
|
56
|
+
|
57
|
+
static VALUE rb_mixin(VALUE self, VALUE module) {
|
58
|
+
VALUE nested_modules;
|
59
|
+
VALUE nested_module;
|
60
|
+
int index;
|
61
|
+
rb_unmix(self, module);
|
62
|
+
|
63
|
+
nested_modules = rb_mod_included_modules(module);
|
64
|
+
//VALUE nested_modules = rb_mod_included_modules(module);
|
65
|
+
for (index = RARRAY(nested_modules)->len; index > 0; index--) {
|
66
|
+
nested_module = RARRAY(nested_modules)->ptr[index-1];
|
67
|
+
add_module(self, nested_module);
|
68
|
+
}
|
69
|
+
|
70
|
+
add_module(self, module);
|
71
|
+
|
72
|
+
rb_clear_cache();
|
73
|
+
return self;
|
74
|
+
}
|
75
|
+
|
76
|
+
void Init_mixology() {
|
77
|
+
VALUE Mixology = rb_define_module("Mixology");
|
78
|
+
rb_define_method(Mixology, "mixin", rb_mixin, 1);
|
79
|
+
rb_define_method(Mixology, "unmix", rb_unmix, 1);
|
80
|
+
rb_include_module(rb_cObject, Mixology);
|
81
|
+
}
|
data/lib/mixology.so
ADDED
Binary file
|
@@ -0,0 +1,170 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/test_helper"
|
2
|
+
|
3
|
+
class MixologyTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
test "mixin" do
|
6
|
+
mixin = Module.new { def foo; "foo"; end }
|
7
|
+
object = Object.new
|
8
|
+
object.mixin mixin
|
9
|
+
assert_equal "foo", object.foo
|
10
|
+
end
|
11
|
+
|
12
|
+
test "unmix" do
|
13
|
+
mixin = Module.new { def foo; "mixin"; end }
|
14
|
+
object = Class.new { def foo; "object"; end }.new
|
15
|
+
object.mixin mixin
|
16
|
+
assert_equal "mixin", object.foo
|
17
|
+
object.unmix mixin
|
18
|
+
assert_equal "object", object.foo
|
19
|
+
end
|
20
|
+
|
21
|
+
test "mixin twice" do
|
22
|
+
first_mixin = Module.new { def foo; "first"; end }
|
23
|
+
second_mixin = Module.new { def foo; "second"; end }
|
24
|
+
object = Object.new
|
25
|
+
object.mixin first_mixin
|
26
|
+
object.mixin second_mixin
|
27
|
+
assert_equal "second", object.foo
|
28
|
+
end
|
29
|
+
|
30
|
+
test "mixin to class" do
|
31
|
+
mix = Module.new { def foo; "foo"; end }
|
32
|
+
klass = Class.new { mixin mix }
|
33
|
+
assert_equal "foo", klass.foo
|
34
|
+
end
|
35
|
+
|
36
|
+
test "can mixin again" do
|
37
|
+
first_mixin = Module.new { def foo; "first"; end }
|
38
|
+
second_mixin = Module.new { def foo; "second"; end }
|
39
|
+
object = Object.new
|
40
|
+
object.mixin first_mixin
|
41
|
+
object.mixin second_mixin
|
42
|
+
object.mixin first_mixin
|
43
|
+
assert_equal "first", object.foo
|
44
|
+
end
|
45
|
+
|
46
|
+
test "unmix effects limited to instance" do
|
47
|
+
mixin = Module.new { def foo; "mixin"; end }
|
48
|
+
object = Class.new {include mixin}.new
|
49
|
+
assert_equal "mixin", object.foo
|
50
|
+
object.unmix mixin
|
51
|
+
assert_equal "mixin", object.foo
|
52
|
+
end
|
53
|
+
|
54
|
+
test "can add mod to an instance even when already included by class" do
|
55
|
+
mixin = Module.new { def foo; "mixin"; end }
|
56
|
+
klass = Class.new {include mixin; def foo; "class"; end }
|
57
|
+
object = klass.new
|
58
|
+
assert_equal "class", object.foo
|
59
|
+
object.mixin mixin
|
60
|
+
assert_equal "mixin", object.foo
|
61
|
+
end
|
62
|
+
|
63
|
+
test "included modules after mixin" do
|
64
|
+
mixin = Module.new
|
65
|
+
object = Object.new
|
66
|
+
object.mixin mixin
|
67
|
+
assert_equal [mixin, Mixology, Kernel], (class << object; self; end).included_modules
|
68
|
+
end
|
69
|
+
|
70
|
+
test "included modules after unmix" do
|
71
|
+
mixin = Module.new
|
72
|
+
object = Object.new
|
73
|
+
object.mixin mixin
|
74
|
+
object.unmix mixin
|
75
|
+
assert_equal [Mixology, Kernel], (class << object; self; end).included_modules
|
76
|
+
end
|
77
|
+
|
78
|
+
test "included modules after remix" do
|
79
|
+
mixin_one = Module.new
|
80
|
+
mixin_two = Module.new
|
81
|
+
object = Object.new
|
82
|
+
object.mixin mixin_one
|
83
|
+
object.mixin mixin_two
|
84
|
+
assert_equal [mixin_two, mixin_one, Mixology, Kernel], (class << object; self; end).included_modules
|
85
|
+
object.mixin mixin_one
|
86
|
+
assert_equal [mixin_one, mixin_two, Mixology, Kernel], (class << object; self; end).included_modules
|
87
|
+
end
|
88
|
+
|
89
|
+
test "mixin returns object" do
|
90
|
+
object = Object.new
|
91
|
+
mixin = Module.new
|
92
|
+
assert_equal object, object.mixin(mixin)
|
93
|
+
end
|
94
|
+
|
95
|
+
test "unmix returns object" do
|
96
|
+
object = Object.new
|
97
|
+
mixin = Module.new
|
98
|
+
object.mixin mixin
|
99
|
+
assert_equal object, object.unmix(mixin)
|
100
|
+
end
|
101
|
+
|
102
|
+
test "nested modules are mixedin" do
|
103
|
+
nested_module = Module.new { def foo; "foo"; end }
|
104
|
+
mixin = Module.new { include nested_module }
|
105
|
+
object = Object.new
|
106
|
+
object.mixin mixin
|
107
|
+
assert_equal [mixin, nested_module, Mixology, Kernel], (class << object; self; end).included_modules
|
108
|
+
end
|
109
|
+
|
110
|
+
test "nested modules are mixedin deeply" do
|
111
|
+
nested_module_ultimate = Module.new
|
112
|
+
nested_module_penultimate = Module.new { include nested_module_ultimate }
|
113
|
+
nested_module = Module.new { include nested_module_penultimate }
|
114
|
+
mixin = Module.new { include nested_module }
|
115
|
+
object = Object.new
|
116
|
+
object.mixin mixin
|
117
|
+
assert_equal [mixin, nested_module, nested_module_penultimate, nested_module_ultimate, Mixology, Kernel], (class << object; self; end).included_modules
|
118
|
+
end
|
119
|
+
|
120
|
+
test "nested modules are mixedin even if alrady mixed in" do
|
121
|
+
nested_module = Module.new { def foo; "foo"; end }
|
122
|
+
mixin = Module.new { include nested_module }
|
123
|
+
object = Object.new
|
124
|
+
object.mixin nested_module
|
125
|
+
object.mixin mixin
|
126
|
+
assert_equal [mixin, nested_module, nested_module, Mixology, Kernel], (class << object; self; end).included_modules
|
127
|
+
end
|
128
|
+
|
129
|
+
test "module is not unmixed if it is outside nested chain" do
|
130
|
+
nested_module = Module.new
|
131
|
+
mixin = Module.new { include nested_module }
|
132
|
+
object = Object.new
|
133
|
+
object.mixin nested_module
|
134
|
+
object.mixin mixin
|
135
|
+
object.unmix mixin
|
136
|
+
assert_equal [nested_module, Mixology, Kernel], (class << object; self; end).included_modules
|
137
|
+
end
|
138
|
+
|
139
|
+
test "nested modules are unmixed" do
|
140
|
+
nested_module = Module.new
|
141
|
+
mixin = Module.new { include nested_module }
|
142
|
+
object = Object.new
|
143
|
+
object.mixin mixin
|
144
|
+
object.unmix mixin
|
145
|
+
assert_equal [Mixology, Kernel], (class << object; self; end).included_modules
|
146
|
+
end
|
147
|
+
|
148
|
+
test "nested modules are unmixed deeply" do
|
149
|
+
nested_module_ultimate = Module.new
|
150
|
+
nested_module_penultimate = Module.new { include nested_module_ultimate }
|
151
|
+
nested_module = Module.new { include nested_module_penultimate }
|
152
|
+
mixin = Module.new { include nested_module }
|
153
|
+
object = Object.new
|
154
|
+
object.mixin mixin
|
155
|
+
object.unmix mixin
|
156
|
+
assert_equal [Mixology, Kernel], (class << object; self; end).included_modules
|
157
|
+
end
|
158
|
+
|
159
|
+
test "unrelated modules are not unmixed" do
|
160
|
+
unrelated = Module.new
|
161
|
+
nested_module = Module.new
|
162
|
+
mixin = Module.new { include nested_module }
|
163
|
+
object = Object.new
|
164
|
+
object.mixin unrelated
|
165
|
+
object.mixin mixin
|
166
|
+
object.unmix mixin
|
167
|
+
assert_equal [unrelated, Mixology, Kernel], (class << object; self; end).included_modules
|
168
|
+
end
|
169
|
+
|
170
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "rubygems"
|
3
|
+
|
4
|
+
if RUBY_PLATFORM =~ /java/
|
5
|
+
Test::Unit::TestCase.class_eval do
|
6
|
+
def self.test(test_name, &block)
|
7
|
+
define_method("test_#{test_name}", &block)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
else
|
11
|
+
begin
|
12
|
+
gem "dust"
|
13
|
+
require "dust"
|
14
|
+
rescue LoadError
|
15
|
+
raise "To run the tests, 'gem install dust'"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
$LOAD_PATH.unshift File.dirname(__FILE__) + "/../lib"
|
20
|
+
require "mixology"
|
metadata
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.3
|
3
|
+
specification_version: 1
|
4
|
+
name: mixology
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.1.0
|
7
|
+
date: 2007-09-01 00:00:00 -04:00
|
8
|
+
summary: Mixology enables objects to mixin and unmix modules.
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email:
|
12
|
+
homepage: http://mixology.rubyforge.org
|
13
|
+
rubyforge_project: mixology
|
14
|
+
description: Mixology enables objects to mixin and unmix modules.
|
15
|
+
autorequire: mixology
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: false
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: mswin32
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- anonymous z, Pat Farley, Dan Manges
|
31
|
+
files:
|
32
|
+
- ext/mixology/mixology.c
|
33
|
+
- ext/mixology/extconf.rb
|
34
|
+
- test/mixology_test.rb
|
35
|
+
- test/test_helper.rb
|
36
|
+
- Rakefile
|
37
|
+
- lib/mixology.so
|
38
|
+
test_files: []
|
39
|
+
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
extra_rdoc_files: []
|
43
|
+
|
44
|
+
executables: []
|
45
|
+
|
46
|
+
extensions: []
|
47
|
+
|
48
|
+
requirements: []
|
49
|
+
|
50
|
+
dependencies: []
|
51
|
+
|