real_include 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ 4/10/10 version 0.1.0
2
+ * release! This is still beta, not 100% tested yet..but appears to work OK so far
@@ -0,0 +1,34 @@
1
+ Real Include
2
+ --------------
3
+
4
+ (c) John Mair (banisterfiend)
5
+ MIT license
6
+
7
+ Removes the shackles from Module#include - use Module#real_include to
8
+ bring in singleton classes from modules. No more ugly ClassMethods and
9
+ included() hook hacks.
10
+
11
+ example:
12
+
13
+ module M
14
+ # class method
15
+ def self.hello
16
+ puts "hello!"
17
+ end
18
+
19
+ # instance method
20
+ def bye
21
+ puts "bye!"
22
+ end
23
+ end
24
+
25
+ class A
26
+ real_include M
27
+ end
28
+
29
+ # invoke class method
30
+ A.hello #=> hello!
31
+
32
+ # invoke instance method
33
+ A.new.bye #=> bye!
34
+
@@ -0,0 +1,36 @@
1
+ $LOAD_PATH.unshift File.join(File.expand_path(__FILE__), '..')
2
+
3
+ require 'rake/clean'
4
+ require 'rake/gempackagetask'
5
+
6
+ # get the texplay version
7
+ require 'lib/real_include/version'
8
+
9
+ $dlext = Config::CONFIG['DLEXT']
10
+
11
+ CLEAN.include("ext/**/*.#{$dlext}", "ext/**/*.log", "ext/**/*.o", "ext/**/*~", "ext/**/*#*", "ext/**/*.obj", "ext/**/*.def", "ext/**/*.pdb")
12
+ CLOBBER.include("**/*.#{$dlext}", "**/*~", "**/*#*", "**/*.log", "**/*.o")
13
+
14
+ specification = Gem::Specification.new do |s|
15
+ s.name = "real_include"
16
+ s.summary = "Fixing the limitation in traditional Module#include"
17
+ s.version = RealInclude::VERSION
18
+ s.date = Time.now.strftime '%Y-%m-%d'
19
+ s.author = "John Mair (banisterfiend)"
20
+ s.email = 'jrmair@gmail.com'
21
+ s.description = s.summary
22
+ s.require_path = 'lib'
23
+ s.platform = Gem::Platform::RUBY
24
+ s.homepage = "http://banisterfiend.wordpress.com"
25
+ s.has_rdoc = false
26
+
27
+ s.extensions = ["ext/real_include/extconf.rb"]
28
+ s.files = ["Rakefile", "README.markdown", "CHANGELOG",
29
+ "lib/real_include.rb", "lib/real_include/version.rb"] +
30
+ FileList["ext/**/extconf.rb", "ext/**/*.h", "ext/**/*.c"].to_a
31
+ end
32
+
33
+ Rake::GemPackageTask.new(specification) do |package|
34
+ package.need_zip = false
35
+ package.need_tar = false
36
+ end
@@ -0,0 +1,18 @@
1
+ /* contains basic macros to facilitate ruby 1.8 and ruby 1.9 compatibility */
2
+
3
+ #ifndef GUARD_COMPAT_H
4
+ #define GUARD_COMPAT_H
5
+
6
+ #include <ruby.h>
7
+
8
+ /* macros for backwards compatibility with 1.8 */
9
+ #ifndef RUBY_19
10
+ # define RCLASS_M_TBL(c) (RCLASS(c)->m_tbl)
11
+ # define RCLASS_SUPER(c) (RCLASS(c)->super)
12
+ # define RCLASS_IV_TBL(c) (RCLASS(c)->iv_tbl)
13
+ #endif
14
+
15
+ /* a useful macro. cannot use ordinary CLASS_OF as it does not return an lvalue */
16
+ #define KLASS_OF(c) (RBASIC(c)->klass)
17
+
18
+ #endif
@@ -0,0 +1,9 @@
1
+ require 'mkmf'
2
+
3
+ # 1.9 compatibility
4
+ $CFLAGS += " -DRUBY_19" if RUBY_VERSION =~ /1.9/
5
+
6
+ # let's use c99
7
+ $CFLAGS += " -std=c99"
8
+
9
+ create_makefile('real_include')
@@ -0,0 +1,124 @@
1
+ #include "compat.h"
2
+ #include "ruby.h"
3
+
4
+ #define FALSE 0
5
+ #define TRUE 1
6
+
7
+ static VALUE
8
+ class_alloc(VALUE flags, VALUE klass)
9
+ {
10
+ rb_classext_t *ext = ALLOC(rb_classext_t);
11
+ NEWOBJ(obj, struct RClass);
12
+ OBJSETUP(obj, klass, flags);
13
+ obj->ptr = ext;
14
+ RCLASS_IV_TBL(obj) = 0;
15
+ RCLASS_M_TBL(obj) = 0;
16
+ RCLASS_SUPER(obj) = 0;
17
+ RCLASS_IV_INDEX_TBL(obj) = 0;
18
+ return (VALUE)obj;
19
+ }
20
+
21
+ static VALUE
22
+ include_class_new(VALUE module, VALUE super)
23
+ {
24
+ if (module == rb_singleton_class(rb_cModule))
25
+ return module;
26
+
27
+ VALUE klass = class_alloc(T_ICLASS, rb_singleton_class(rb_cModule));
28
+
29
+ /* if (BUILTIN_TYPE(module) == T_ICLASS) { */
30
+ /* module = RBASIC(module)->klass; */
31
+ /* } */
32
+ if (!RCLASS_IV_TBL(module)) {
33
+ RCLASS_IV_TBL(module) = st_init_numtable();
34
+ }
35
+ RCLASS_IV_TBL(klass) = st_init_numtable();
36
+ RCLASS_M_TBL(klass) = RCLASS_M_TBL(module);
37
+ RCLASS_SUPER(klass) = super;
38
+ /*
39
+ if (TYPE(module) == T_ICLASS) {
40
+ RBASIC(klass)->klass = RBASIC(module)->klass;
41
+ }
42
+ */
43
+
44
+ /* create IClass for module's singleton */
45
+
46
+ VALUE meta = include_class_new(KLASS_OF(module), super ? KLASS_OF(super) : rb_cModule);
47
+ if (meta != rb_singleton_class(rb_cModule)) {
48
+ FL_SET(meta, FL_SINGLETON);
49
+
50
+ /* attach singleton to module */
51
+ rb_singleton_class_attached(meta, klass);
52
+ }
53
+ /* assign the metaclass to module's klass */
54
+ KLASS_OF(klass) = meta;
55
+
56
+ OBJ_INFECT(klass, module);
57
+ OBJ_INFECT(klass, super);
58
+
59
+ return (VALUE)klass;
60
+ }
61
+
62
+ VALUE
63
+ rb_real_include_module(VALUE klass, VALUE module)
64
+ {
65
+ VALUE p, c;
66
+ int changed = 0;
67
+
68
+ rb_frozen_class_p(klass);
69
+ if (!OBJ_UNTRUSTED(klass)) {
70
+ rb_secure(4);
71
+ }
72
+
73
+ if (TYPE(module) != T_MODULE) {
74
+ Check_Type(module, T_MODULE);
75
+ }
76
+
77
+ OBJ_INFECT(klass, module);
78
+ c = klass;
79
+
80
+ /* ensure singleton class exists */
81
+ rb_singleton_class(module);
82
+ rb_singleton_class(klass);
83
+
84
+ while (module) {
85
+ int superclass_seen = FALSE;
86
+
87
+ if (RCLASS_M_TBL(klass) == RCLASS_M_TBL(module))
88
+ rb_raise(rb_eArgError, "cyclic include detected");
89
+ /* ignore if the module included already in superclasses */
90
+ for (p = RCLASS_SUPER(klass); p; p = RCLASS_SUPER(p)) {
91
+ switch (BUILTIN_TYPE(p)) {
92
+ case T_ICLASS:
93
+ if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
94
+ if (!superclass_seen) {
95
+ c = p; /* move insertion point */
96
+ }
97
+ goto skip;
98
+ }
99
+ break;
100
+ case T_CLASS:
101
+ superclass_seen = TRUE;
102
+ break;
103
+ }
104
+ }
105
+
106
+ VALUE imod = include_class_new(module, RCLASS_SUPER(c));
107
+ RCLASS_SUPER(c) = imod;
108
+ RCLASS_SUPER(KLASS_OF(c)) = KLASS_OF(imod);
109
+ c = imod;
110
+
111
+ if (RMODULE_M_TBL(module) && RMODULE_M_TBL(module)->num_entries)
112
+ changed = 1;
113
+ skip:
114
+ module = RCLASS_SUPER(module);
115
+ }
116
+ if (changed) rb_clear_cache();
117
+
118
+ return Qnil;
119
+ }
120
+
121
+ void
122
+ Init_real_include() {
123
+ rb_define_method(rb_cModule, "real_include", rb_real_include_module, 1);
124
+ }
@@ -0,0 +1,14 @@
1
+ # bring in user-defined extensions to TexPlay
2
+ direc = File.dirname(__FILE__)
3
+ dlext = Config::CONFIG['DLEXT']
4
+ begin
5
+ if RUBY_VERSION && RUBY_VERSION =~ /1.9/
6
+ require "#{direc}/1.9/real_include.#{dlext}"
7
+ else
8
+ require "#{direc}/1.8/real_include.#{dlext}"
9
+ end
10
+ rescue LoadError => e
11
+ require "#{direc}/real_include.#{dlext}"
12
+ end
13
+
14
+ require "#{direc}/real_include/version"
@@ -0,0 +1,3 @@
1
+ module RealInclude
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: real_include
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - John Mair (banisterfiend)
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-04 00:00:00 +13:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Fixing the limitation in traditional Module#include
23
+ email: jrmair@gmail.com
24
+ executables: []
25
+
26
+ extensions:
27
+ - ext/real_include/extconf.rb
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - Rakefile
32
+ - README.markdown
33
+ - CHANGELOG
34
+ - lib/real_include.rb
35
+ - lib/real_include/version.rb
36
+ - ext/real_include/extconf.rb
37
+ - ext/real_include/compat.h
38
+ - ext/real_include/real_include.c
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: Fixing the limitation in traditional Module#include
73
+ test_files: []
74
+