eigenclass 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +6 -0
- data/MIT-LICENSE +20 -0
- data/README.markdown +55 -0
- data/Rakefile +22 -0
- data/lib/eigenclass.rb +79 -0
- data/test/eigenclass_test.rb +90 -0
- metadata +62 -0
data/CHANGELOG
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Sean Huber - shuber@huberry.com
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
Eigenclass
|
2
|
+
==========
|
3
|
+
|
4
|
+
Utility methods for modifying a ruby object's eigenclass/metaclass
|
5
|
+
|
6
|
+
|
7
|
+
Installation
|
8
|
+
------------
|
9
|
+
|
10
|
+
gem install shuber-eigenclass --source http://gems.github.com
|
11
|
+
|
12
|
+
|
13
|
+
Usage
|
14
|
+
-----
|
15
|
+
|
16
|
+
This gem allows you to define class level accessors, readers, and writers
|
17
|
+
|
18
|
+
class SomeClass
|
19
|
+
eattr_accessor :test_accessor
|
20
|
+
eattr_reader :test_reader
|
21
|
+
eattr_writer :test_writer
|
22
|
+
end
|
23
|
+
|
24
|
+
SomeClass.test_accessor = 'testing'
|
25
|
+
|
26
|
+
|
27
|
+
You can also dynamically create class methods
|
28
|
+
|
29
|
+
SomeClass.class_eval do
|
30
|
+
define_class_method 'test_class_method' do
|
31
|
+
'test'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
SomeClass.test_class_method # returns 'test'
|
36
|
+
|
37
|
+
|
38
|
+
You can even evaluate a block of code inside the scope of an object's eigenclass
|
39
|
+
|
40
|
+
SomeClass.eigenclass_eval do
|
41
|
+
attr_accessor :test_eigenclass_eval
|
42
|
+
end
|
43
|
+
|
44
|
+
SomeClass.test_eigenclass_eval = true
|
45
|
+
|
46
|
+
|
47
|
+
A reference to any object's eigenclass can be accessed by calling
|
48
|
+
|
49
|
+
SomeObject.eigenclass
|
50
|
+
|
51
|
+
|
52
|
+
Contact
|
53
|
+
-------
|
54
|
+
|
55
|
+
Problems, comments, and suggestions all welcome: [shuber@huberry.com](mailto:shuber@huberry.com)
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the eigenclass gem.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the eigenclass gem.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'Eigenclass'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README.markdown')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
data/lib/eigenclass.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
module Huberry
|
2
|
+
module Eigenclass
|
3
|
+
# Uses <tt>eigenclass_eval</tt> to define attr_accessors in an object's <tt>eigenclass</tt>
|
4
|
+
# which can be called like a class methods
|
5
|
+
#
|
6
|
+
# Example
|
7
|
+
#
|
8
|
+
# class User
|
9
|
+
# eattr_accessor :testing
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# User.testing = true
|
13
|
+
def eattr_accessor(*attrs)
|
14
|
+
eigenclass_eval { attr_accessor *attrs }
|
15
|
+
end
|
16
|
+
|
17
|
+
# Uses <tt>eigenclass_eval</tt> to define attr_readers in an object's <tt>eigenclass</tt>
|
18
|
+
# which can be called like a class methods
|
19
|
+
#
|
20
|
+
# Example
|
21
|
+
#
|
22
|
+
# class User
|
23
|
+
# eattr_reader :testing
|
24
|
+
# @testing = true
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# User.testing # returns true
|
28
|
+
def eattr_reader(*attrs)
|
29
|
+
eigenclass_eval { attr_reader *attrs }
|
30
|
+
end
|
31
|
+
|
32
|
+
# Uses <tt>eigenclass_eval</tt> to define attr_writers in an object's <tt>eigenclass</tt>
|
33
|
+
# which can be called like a class methods
|
34
|
+
#
|
35
|
+
# Example
|
36
|
+
#
|
37
|
+
# class User
|
38
|
+
# eattr_writer :testing
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
# User.testing = true
|
42
|
+
def eattr_writer(*attrs)
|
43
|
+
eigenclass_eval { attr_writer *attrs }
|
44
|
+
end
|
45
|
+
|
46
|
+
# Uses <tt>eigenclass_eval</tt> to define a method in an object's <tt>eigenclass</tt>
|
47
|
+
# which can be called like a class method
|
48
|
+
#
|
49
|
+
# Example
|
50
|
+
#
|
51
|
+
# User.class_eval do
|
52
|
+
# define_class_method 'testing' do
|
53
|
+
# 'test'
|
54
|
+
# end
|
55
|
+
# end
|
56
|
+
#
|
57
|
+
# User.testing # returns 'test'
|
58
|
+
def define_class_method(name, &block)
|
59
|
+
eigenclass_eval { define_method name, &block }
|
60
|
+
end
|
61
|
+
|
62
|
+
# Returns an object's eigenclass
|
63
|
+
def eigenclass
|
64
|
+
class << self; self; end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Accepts a block to evaluate inside the scope of an object's <tt>eigenclass</tt>
|
68
|
+
#
|
69
|
+
# Example
|
70
|
+
#
|
71
|
+
# User.eigenclass_eval { attr_accessor :testing }
|
72
|
+
# User.testing = true
|
73
|
+
def eigenclass_eval(&block)
|
74
|
+
eigenclass.instance_eval &block
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
Object.send :include, Huberry::Eigenclass
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require File.dirname(__FILE__) + '/../lib/eigenclass'
|
3
|
+
|
4
|
+
class TestClass; end
|
5
|
+
class ChildTestClass < TestClass; end
|
6
|
+
class AnotherTestClass; end
|
7
|
+
|
8
|
+
class EigenclassTest < Test::Unit::TestCase
|
9
|
+
|
10
|
+
def test_should_return_the_eigenclass
|
11
|
+
assert_equal (class << TestClass; self; end), TestClass.eigenclass
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_should_return_the_same_instance_of_the_eigenclass
|
15
|
+
assert_equal TestClass.eigenclass.object_id, TestClass.eigenclass.object_id
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_should_eigenclass_eval_correctly
|
19
|
+
assert !TestClass.respond_to?(:testing)
|
20
|
+
TestClass.eigenclass_eval do
|
21
|
+
define_method 'testing' do
|
22
|
+
'test'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
assert TestClass.respond_to?(:testing)
|
26
|
+
assert_equal 'test', TestClass.testing
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_should_define_class_method
|
30
|
+
assert !TestClass.respond_to?(:testing_again)
|
31
|
+
TestClass.class_eval do
|
32
|
+
define_class_method 'testing_again' do
|
33
|
+
'test'
|
34
|
+
end
|
35
|
+
end
|
36
|
+
assert TestClass.respond_to?(:testing_again)
|
37
|
+
assert_equal 'test', TestClass.testing_again
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_child_class_should_inherit_defined_class_methods
|
41
|
+
assert !ChildTestClass.respond_to?(:testing_yet_again)
|
42
|
+
TestClass.class_eval do
|
43
|
+
define_class_method 'testing_yet_again' do
|
44
|
+
'test'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
assert ChildTestClass.respond_to?(:testing_yet_again)
|
48
|
+
assert_equal 'test', ChildTestClass.testing_yet_again
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_should_not_define_methods_accross_all_classes
|
52
|
+
assert !TestClass.respond_to?(:testing_one_more_time)
|
53
|
+
assert !AnotherTestClass.respond_to?(:testing_one_more_time)
|
54
|
+
TestClass.class_eval do
|
55
|
+
define_class_method 'testing_one_more_time' do
|
56
|
+
'test'
|
57
|
+
end
|
58
|
+
end
|
59
|
+
assert TestClass.respond_to?(:testing_one_more_time)
|
60
|
+
assert !AnotherTestClass.respond_to?(:testing_one_more_time)
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_cattr_accessor
|
64
|
+
assert !TestClass.respond_to?(:this_is_a_test)
|
65
|
+
TestClass.eattr_accessor :this_is_a_test
|
66
|
+
assert TestClass.respond_to?(:this_is_a_test)
|
67
|
+
assert TestClass.this_is_a_test.nil?
|
68
|
+
TestClass.this_is_a_test = true
|
69
|
+
assert TestClass.this_is_a_test
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_cattr_reader
|
73
|
+
assert !TestClass.respond_to?(:this_is_another_test)
|
74
|
+
TestClass.eattr_reader :this_is_another_test
|
75
|
+
assert TestClass.respond_to?(:this_is_another_test)
|
76
|
+
assert TestClass.this_is_another_test.nil?
|
77
|
+
TestClass.instance_variable_set('@this_is_another_test', true)
|
78
|
+
assert TestClass.this_is_another_test
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_cattr_writer
|
82
|
+
assert !TestClass.respond_to?(:this_is_yet_another_test=)
|
83
|
+
TestClass.eattr_writer :this_is_yet_another_test
|
84
|
+
assert TestClass.respond_to?(:this_is_yet_another_test=)
|
85
|
+
assert TestClass.instance_variable_get('@this_is_yet_another_test').nil?
|
86
|
+
TestClass.this_is_yet_another_test = true
|
87
|
+
assert TestClass.instance_variable_get('@this_is_yet_another_test')
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: eigenclass
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sean Huber
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-01-07 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Utility methods for modifying a ruby object's eigenclass/metaclass
|
17
|
+
email: shuber@huberry.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- CHANGELOG
|
26
|
+
- lib/eigenclass.rb
|
27
|
+
- MIT-LICENSE
|
28
|
+
- Rakefile
|
29
|
+
- README.markdown
|
30
|
+
has_rdoc: false
|
31
|
+
homepage: http://github.com/shuber/eigenclass
|
32
|
+
licenses: []
|
33
|
+
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options:
|
36
|
+
- --line-numbers
|
37
|
+
- --inline-source
|
38
|
+
- --main
|
39
|
+
- README.markdown
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
requirements: []
|
55
|
+
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 1.3.5
|
58
|
+
signing_key:
|
59
|
+
specification_version: 3
|
60
|
+
summary: Utility methods for modifying a ruby object's eigenclass/metaclass
|
61
|
+
test_files:
|
62
|
+
- test/eigenclass_test.rb
|