change_class2 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY ADDED
File without changes
@@ -0,0 +1,54 @@
1
+ change_class
2
+ ===========
3
+
4
+ (C) John Mair (banisterfiend) 2011
5
+
6
+ _change the class of an object_
7
+
8
+ * Install the [gem](https://rubygems.org/gems/change_class): `gem install change_class`
9
+ * Read the [documentation](http://rdoc.info/github/banister/change_class/master/file/README.markdown)
10
+ * See the [source code](http://github.com/banister/change_class)
11
+
12
+ Example:
13
+ --------
14
+
15
+ Change the class of an object:
16
+
17
+ class Hello; end
18
+ class Goodbye; end
19
+ o = Hello.new
20
+ o.class #=> Hello
21
+ o.change_class Goodbye
22
+ o.class #=> Goodbye
23
+
24
+ Contact
25
+ -------
26
+
27
+ Problems or questions contact me at [github](http://github.com/banister)
28
+
29
+
30
+ License
31
+ -------
32
+
33
+ (The MIT License)
34
+
35
+ Copyright (c) 2011 (John Mair)
36
+
37
+ Permission is hereby granted, free of charge, to any person obtaining
38
+ a copy of this software and associated documentation files (the
39
+ 'Software'), to deal in the Software without restriction, including
40
+ without limitation the rights to use, copy, modify, merge, publish,
41
+ distribute, sublicense, and/or sell copies of the Software, and to
42
+ permit persons to whom the Software is furnished to do so, subject to
43
+ the following conditions:
44
+
45
+ The above copyright notice and this permission notice shall be
46
+ included in all copies or substantial portions of the Software.
47
+
48
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
49
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
50
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
51
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
52
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
53
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
54
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,99 @@
1
+ dlext = Config::CONFIG['DLEXT']
2
+ direc = File.dirname(__FILE__)
3
+
4
+ PROJECT_NAME = "change_class"
5
+
6
+ require 'rake/clean'
7
+ require 'rake/gempackagetask'
8
+ require "#{direc}/lib/#{PROJECT_NAME}/version"
9
+
10
+ CLOBBER.include("**/*.#{dlext}", "**/*~", "**/*#*", "**/*.log", "**/*.o")
11
+ CLEAN.include("ext/**/*.#{dlext}", "ext/**/*.log", "ext/**/*.o",
12
+ "ext/**/*~", "ext/**/*#*", "ext/**/*.obj", "**/*#*", "**/*#*.*",
13
+ "ext/**/*.def", "ext/**/*.pdb", "**/*_flymake*.*", "**/*_flymake")
14
+
15
+ def apply_spec_defaults(s)
16
+ s.name = "change_class2"
17
+ s.summary = "change the class of an object"
18
+ s.version = ChangeClass::VERSION
19
+ s.date = Time.now.strftime '%Y-%m-%d'
20
+ s.author = "John Mair (banisterfiend)"
21
+ s.email = 'jrmair@gmail.com'
22
+ s.description = s.summary
23
+ s.require_path = 'lib'
24
+ s.add_development_dependency("bacon","~>1.1.0")
25
+ s.homepage = "http://banisterfiend.wordpress.com"
26
+ s.has_rdoc = 'yard'
27
+ s.files = Dir["ext/**/extconf.rb", "ext/**/*.h", "ext/**/*.c", "lib/**/*.rb",
28
+ "test/*.rb", "HISTORY", "README.md", "Rakefile"]
29
+ end
30
+
31
+ desc "run tests"
32
+ task :test do
33
+ sh "bacon -k #{direc}/test/test.rb"
34
+ end
35
+
36
+ [:mingw32, :mswin32].each do |v|
37
+ namespace v do
38
+ spec = Gem::Specification.new do |s|
39
+ apply_spec_defaults(s)
40
+ s.platform = "i386-#{v}"
41
+ s.files += FileList["lib/**/*.#{dlext}"].to_a
42
+ end
43
+
44
+ Rake::GemPackageTask.new(spec) do |pkg|
45
+ pkg.need_zip = false
46
+ pkg.need_tar = false
47
+ end
48
+ end
49
+ end
50
+
51
+ namespace :ruby do
52
+ spec = Gem::Specification.new do |s|
53
+ apply_spec_defaults(s)
54
+ s.platform = Gem::Platform::RUBY
55
+ s.extensions = ["ext/#{PROJECT_NAME}/extconf.rb"]
56
+ end
57
+
58
+ Rake::GemPackageTask.new(spec) do |pkg|
59
+ pkg.need_zip = false
60
+ pkg.need_tar = false
61
+ end
62
+ end
63
+
64
+ directories = ["#{direc}/lib/1.8", "#{direc}/lib/1.9"]
65
+ directories.each { |d| directory d }
66
+
67
+ desc "build the 1.8 and 1.9 binaries from source and copy to lib/"
68
+ task :compile => directories do
69
+ build_for = proc do |pik_ver, ver|
70
+ sh %{ \
71
+ c:\\devkit\\devkitvars.bat && \
72
+ pik #{pik_ver} && \
73
+ ruby extconf.rb && \
74
+ make clean && \
75
+ make && \
76
+ cp *.so #{direc}/lib/#{ver} \
77
+ }
78
+ end
79
+
80
+ chdir("#{direc}/ext/#{PROJECT_NAME}") do
81
+ build_for.call("187", "1.8")
82
+ build_for.call("192", "1.9")
83
+ end
84
+ end
85
+
86
+ desc "build all platform gems at once"
87
+ task :gems => [:clean, :rmgems, "mingw32:gem", "mswin32:gem", "ruby:gem"]
88
+
89
+ desc "remove all platform gems"
90
+ task :rmgems => ["ruby:clobber_package"]
91
+
92
+ desc "build and push latest gems"
93
+ task :pushgems => :gems do
94
+ chdir("#{direc}/pkg") do
95
+ Dir["*.gem"].each do |gemfile|
96
+ sh "gem push #{gemfile}"
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,35 @@
1
+ /* (c) 2011 John Mair (banisterfiend), MIT license */
2
+
3
+ #include <ruby.h>
4
+ #include "compat.h"
5
+
6
+ static VALUE
7
+ rb_change_class(VALUE self, VALUE klass)
8
+ {
9
+ VALUE actual_class = KLASS_OF(self);;
10
+ VALUE cl;
11
+
12
+ if (TYPE(klass) != T_CLASS)
13
+ rb_raise(rb_eArgError, "Must provide a class.");
14
+
15
+ cl = actual_class;
16
+
17
+ if (cl == rb_class_real(cl))
18
+ KLASS_OF(self) = klass;
19
+ else {
20
+ while ((RBASIC(RCLASS_SUPER(cl))->flags & FL_SINGLETON) || BUILTIN_TYPE(RCLASS_SUPER(cl)) == T_ICLASS)
21
+ cl = RCLASS_SUPER(cl);
22
+
23
+ RCLASS_SUPER(cl) = klass;
24
+ }
25
+
26
+ rb_clear_cache();
27
+
28
+ return klass;
29
+ }
30
+
31
+ void
32
+ Init_change_class()
33
+ {
34
+ rb_define_method(rb_cObject, "change_class", rb_change_class, 1);
35
+ }
@@ -0,0 +1,57 @@
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
+ /* test for 1.9 */
9
+ #if !defined(RUBY_19) && defined(ROBJECT_EMBED_LEN_MAX)
10
+ # define RUBY_19
11
+ #endif
12
+
13
+ /* macros for backwards compatibility with 1.8 */
14
+ #ifndef RUBY_19
15
+ # define RCLASS_M_TBL(c) (RCLASS(c)->m_tbl)
16
+ # define RCLASS_SUPER(c) (RCLASS(c)->super)
17
+ # define RCLASS_IV_TBL(c) (RCLASS(c)->iv_tbl)
18
+ # define OBJ_UNTRUSTED OBJ_TAINTED
19
+ # include "st.h"
20
+ #endif
21
+
22
+ #ifdef RUBY_19
23
+ inline static VALUE
24
+ class_alloc(VALUE flags, VALUE klass)
25
+ {
26
+ rb_classext_t *ext = ALLOC(rb_classext_t);
27
+ NEWOBJ(obj, struct RClass);
28
+ OBJSETUP(obj, klass, flags);
29
+ obj->ptr = ext;
30
+ RCLASS_IV_TBL(obj) = 0;
31
+ RCLASS_M_TBL(obj) = 0;
32
+ RCLASS_SUPER(obj) = 0;
33
+ RCLASS_IV_INDEX_TBL(obj) = 0;
34
+ return (VALUE)obj;
35
+ }
36
+ #endif
37
+
38
+ inline static VALUE
39
+ create_class(VALUE flags, VALUE klass)
40
+ {
41
+ #ifdef RUBY_19
42
+ VALUE new_klass = class_alloc(flags, klass);
43
+ #else
44
+ NEWOBJ(new_klass, struct RClass);
45
+ OBJSETUP(new_klass, klass, flags);
46
+ #endif
47
+
48
+ return (VALUE)new_klass;
49
+ }
50
+
51
+ # define FALSE 0
52
+ # define TRUE 1
53
+
54
+ /* a useful macro. cannot use ordinary CLASS_OF as it does not return an lvalue */
55
+ #define KLASS_OF(c) (RBASIC(c)->klass)
56
+
57
+ #endif
@@ -0,0 +1,5 @@
1
+ require 'mkmf'
2
+
3
+ $CFLAGS += " -std=c99"
4
+
5
+ create_makefile('change_class')
@@ -0,0 +1,3 @@
1
+ module ChangeClass
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,8 @@
1
+ # change_class2.rb
2
+ # (C) John Mair (banisterfiend); MIT license
3
+
4
+ require "rbconfig"
5
+ dlext = Config::CONFIG['DLEXT']
6
+ require "change_class.#{dlext}"
7
+
8
+
@@ -0,0 +1,12 @@
1
+ direc = File.dirname(__FILE__)
2
+
3
+ require 'rubygems'
4
+ require "#{direc}/../lib/change_class"
5
+ require 'bacon'
6
+
7
+ puts "Testing change_class version #{ChangeClass::VERSION}..."
8
+ puts "Ruby version: #{RUBY_VERSION}"
9
+
10
+ describe ChangeClass do
11
+ end
12
+
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: change_class2
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - John Mair (banisterfiend)
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-09-29 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bacon
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 1.1.0
24
+ type: :development
25
+ version_requirements: *id001
26
+ description: change the class of an object
27
+ email: jrmair@gmail.com
28
+ executables: []
29
+
30
+ extensions:
31
+ - ext/change_class/extconf.rb
32
+ extra_rdoc_files: []
33
+
34
+ files:
35
+ - ext/change_class/extconf.rb
36
+ - ext/change_class/compat.h
37
+ - ext/change_class/change_class.c
38
+ - lib/change_class/version.rb
39
+ - lib/change_class2.rb
40
+ - test/test.rb
41
+ - HISTORY
42
+ - README.md
43
+ - Rakefile
44
+ homepage: http://banisterfiend.wordpress.com
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options: []
49
+
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.7.2
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: change the class of an object
71
+ test_files: []
72
+