Permalink
Browse files

Added basic functionality, tests and documentation.

  • Loading branch information...
1 parent be7c667 commit 13c783b76c33b62dea6be8238cd91a192c30d8cc @floehopper committed Aug 11, 2011
Showing with 68 additions and 7 deletions.
  1. +0 −1 Gemfile
  2. +6 −0 README.md
  3. +10 −0 Rakefile
  4. +3 −3 lib/metaclass.rb
  5. +11 −0 lib/metaclass/object_methods.rb
  6. +2 −3 metaclass.gemspec
  7. +31 −0 test/object_methods_test.rb
  8. +5 −0 test/test_helper.rb
View
@@ -1,4 +1,3 @@
source "http://rubygems.org"
-# Specify your gem's dependencies in metaclass.gemspec
gemspec
View
@@ -0,0 +1,6 @@
+Adds a `__metaclass__` method to all Ruby objects.
+
+Motivations :-
+
+* Even though WhyTheLuckyStiff's [metaid gem](https://rubygems.org/gems/metaid) does something similar, apparently the metaclass method without underscores [doesn't play well with Rails v2.3](https://github.com/floehopper/mocha/commit/f0749d6d291164cc9280aa8ba16f33d652d45fe1#commitcomment-475799).
+* I'm trying to extract code out of the [mocha gem](https://github.com/floehopper/mocha) and this is an obvious candidate.
View
@@ -1 +1,11 @@
require 'bundler/gem_tasks'
+
+
+require "rake/testtask"
+Rake::TestTask.new do |t|
+ t.libs << "test"
+ t.test_files = FileList["test/**/*_test.rb"]
+ t.verbose = true
+end
+
+task :default => :test
View
@@ -1,5 +1,5 @@
-require "metaclass/version"
-
module Metaclass
- # Your code goes here...
end
+
+require "metaclass/version"
+require "metaclass/object_methods"
@@ -0,0 +1,11 @@
+module Metaclass::ObjectMethods
+ def __metaclass__
+ class << self
+ self
+ end
+ end
+end
+
+class Object
+ include Metaclass::ObjectMethods
+end
View
@@ -7,9 +7,8 @@ Gem::Specification.new do |s|
s.version = Metaclass::VERSION
s.authors = ["James Mead"]
s.email = ["james@floehopper.org"]
- s.homepage = ""
- s.summary = %q{TODO: Write a gem summary}
- s.description = %q{TODO: Write a gem description}
+ s.homepage = "http://github.com/floehopper/metaclass"
+ s.summary = %q{Adds a metaclass method to all Ruby objects}
s.rubyforge_project = "metaclass"
@@ -0,0 +1,31 @@
+require "test_helper"
+
+class ObjectMethodsTest < Test::Unit::TestCase
+
+ def setup
+ @klass = Class.new
+ end
+
+ def test_define_method_on_only_one_instance_of_a_class
+ instance = @klass.new
+ assert_raises(NoMethodError) { instance.success? }
+
+ instance.__metaclass__.class_eval { def success?; true; end }
+ assert_nothing_raised(NoMethodError) { assert instance.success? }
+
+ another_instance = @klass.new
+ assert_raises(NoMethodError) { another_instance.success? }
+ end
+
+ def test_metaclass_ancestors
+ instance = @klass.new
+ assert instance.__metaclass__.ancestors.include?(Object)
+ assert instance.__metaclass__.ancestors.include?(Kernel)
+ end
+
+ def test_metaclass_is_itself_a_class
+ instance = @klass.new
+ assert instance.__metaclass__.is_a?(Class)
+ end
+
+end
View
@@ -0,0 +1,5 @@
+require "rubygems"
+require "bundler/setup"
+
+require "metaclass"
+require "test/unit"

0 comments on commit 13c783b

Please sign in to comment.