eigenclass 2.0.1 → 2.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +5 -3
- data/Gemfile.lock +1 -1
- data/README.md +126 -0
- data/eigenclass.gemspec +2 -2
- data/lib/eigenclass.rb +4 -0
- data/lib/eigenclass/version.rb +1 -1
- data/spec/spec_helper.rb +4 -88
- metadata +7 -5
- data/README.rdoc +0 -96
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f734fffd3172d7dc6ba61c300bad51357e850525
|
4
|
+
data.tar.gz: 8643dc31c8cd81268ba7871a1965c3e14671d2a3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d1f54bd72ad5a5644826b6740b781c501d04389b144dd32db516701936c6c1caf9c9c87fc23682d8ae78dcd99eb5a2ccaa212df1106df2ab294bb37f8cba6e6e
|
7
|
+
data.tar.gz: 688a8c74bd8539c1bb4413e8fa6d3d62cf27d7c96de76a99b9d8013c0fd790834be9d09d765127e96555f478094ab325d58416b4aa5ecfa455241e5df80952f7
|
data/.travis.yml
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
+
addons:
|
2
|
+
code_climate:
|
3
|
+
repo_token: 6e85e0e345260307d97927e8eee7f52e2cec36c021b792a30bf7ffb9a6f8eea8
|
4
|
+
|
1
5
|
rvm:
|
2
6
|
- 1.9.3
|
3
7
|
- 2.0.0
|
4
8
|
- ruby-head
|
9
|
+
|
5
10
|
script: bundle exec rspec
|
6
|
-
addons:
|
7
|
-
code_climate:
|
8
|
-
repo_token: 6e85e0e345260307d97927e8eee7f52e2cec36c021b792a30bf7ffb9a6f8eea8
|
data/Gemfile.lock
CHANGED
data/README.md
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
# eigenclass - [![Build Status](https://secure.travis-ci.org/shuber/eigenclass.png)](http://travis-ci.org/shuber/eigenclass) [![Code Climate](https://codeclimate.com/github/shuber/eigenclass/badges/gpa.svg)](https://codeclimate.com/github/shuber/eigenclass) [![Coverage](https://codeclimate.com/github/shuber/eigenclass/badges/coverage.svg)](https://codeclimate.com/github/shuber/eigenclass)
|
2
|
+
|
3
|
+
Eigenclasses *aka metaclasses or singleton classes* in Ruby.
|
4
|
+
|
5
|
+
Check out the [metaclass] implementations in other languages for more examples.
|
6
|
+
|
7
|
+
**Note**: This gem was originally written back in 2008. Since then, Ruby has introduced a couple new methods which provide the same functionality as this gem's `eigenclass` and `edefine_method` methods.
|
8
|
+
|
9
|
+
* [Object#singleton_class]
|
10
|
+
* [Object#define_singleton_method]
|
11
|
+
|
12
|
+
[metaclass]: http://en.wikipedia.org/wiki/Metaclass
|
13
|
+
[Object#singleton_class]: http://ruby-doc.org/core-1.9.2/Object.html#method-i-singleton_class
|
14
|
+
[Object#define_singleton_method]: http://ruby-doc.org/core-1.9.2/Object.html#method-i-define_singleton_method
|
15
|
+
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
```
|
20
|
+
gem install eigenclass
|
21
|
+
```
|
22
|
+
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
Everything in Ruby is an object, including classes.
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
class SomeObject
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
Every object has an `eigenclass`.
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
SomeObject.eigenclass #=> #<Class:#<SomeObject:0x007f9611030300>>
|
37
|
+
```
|
38
|
+
|
39
|
+
The implementation of the `eigenclass` method is pretty simple.
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
class Object
|
43
|
+
def eigenclass
|
44
|
+
class << self
|
45
|
+
self
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
Evaluating code within the context of an object's `eigenclass` allows us to do some pretty cool things - like defining class level attributes.
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
SomeObject.eigenclass_eval do
|
55
|
+
attr_accessor :example
|
56
|
+
end
|
57
|
+
|
58
|
+
SomeObject.example = :test
|
59
|
+
SomeObject.example #=> :test
|
60
|
+
```
|
61
|
+
|
62
|
+
The convenience methods for defining class level methods makes this even easier.
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
class SomeObject
|
66
|
+
eattr_accessor :example_accessor
|
67
|
+
eattr_reader :example_reader
|
68
|
+
eattr_writer :example_writer
|
69
|
+
|
70
|
+
edefine_method(:example_class_method) do
|
71
|
+
1 + 1
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
SomeObject.example_class_method #=> 2
|
76
|
+
```
|
77
|
+
|
78
|
+
Since all objects have an `eigenclass`, we can even define methods for a single _instance_ of a class.
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
object = SomeObject.new
|
82
|
+
object.eattr_accessor :example
|
83
|
+
object.example = :test
|
84
|
+
object.example #=> :test
|
85
|
+
|
86
|
+
other_object = SomeObject.new
|
87
|
+
other_object.example #=> NoMethodError undefined method `example' for #<SomeObject:0x007fee348dde00>
|
88
|
+
```
|
89
|
+
|
90
|
+
This is pretty incredible! We can hook in and inject behavior into any and all objects - **at runtime**!
|
91
|
+
|
92
|
+
Ruby is like one big plugin framework - with an awesome standard library and amazing community!
|
93
|
+
|
94
|
+
|
95
|
+
## API
|
96
|
+
|
97
|
+
[YARD Documentation](http://www.rubydoc.info/github/shuber/eigenclass/master)
|
98
|
+
|
99
|
+
* [eattr_accessor](http://ruby-doc.org/core-1.9.3/Module.html#method-i-attr_accessor)
|
100
|
+
* [eattr_reader](http://ruby-doc.org/core-1.9.3/Module.html#method-i-attr_reader)
|
101
|
+
* [eattr_writer](http://ruby-doc.org/core-1.9.3/Module.html#method-i-attr_writer)
|
102
|
+
* [edefine_method](http://ruby-doc.org/core-1.9.2/Object.html#method-i-define_singleton_method)
|
103
|
+
* [eigenclass](http://ruby-doc.org/core-1.9.2/Object.html#method-i-singleton_class)
|
104
|
+
* [eigenclass_eval](http://ruby-doc.org/core-1.9.3/BasicObject.html#method-i-instance_eval)
|
105
|
+
* [eigenclass_exec](http://ruby-doc.org/core-1.9.3/BasicObject.html#method-i-instance_exec)
|
106
|
+
|
107
|
+
|
108
|
+
## Testing
|
109
|
+
|
110
|
+
```
|
111
|
+
bundle exec rspec
|
112
|
+
```
|
113
|
+
|
114
|
+
|
115
|
+
## Contributing
|
116
|
+
|
117
|
+
* Fork the project.
|
118
|
+
* Make your feature addition or bug fix.
|
119
|
+
* Add tests for it. This is important so I don't break it in a future version unintentionally.
|
120
|
+
* Commit, do not mess with Rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
121
|
+
* Send me a pull request. Bonus points for topic branches.
|
122
|
+
|
123
|
+
|
124
|
+
## License
|
125
|
+
|
126
|
+
[MIT](https://github.com/shuber/eigenclass/blob/master/LICENSE) - Copyright © 2008 Sean Huber
|
data/eigenclass.gemspec
CHANGED
@@ -9,10 +9,10 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.homepage = 'https://github.com/shuber/eigenclass'
|
10
10
|
s.license = 'MIT'
|
11
11
|
s.name = 'eigenclass'
|
12
|
-
s.rdoc_options = %w(--charset=UTF-8 --inline-source --line-numbers --main README.
|
12
|
+
s.rdoc_options = %w(--charset=UTF-8 --inline-source --line-numbers --main README.md)
|
13
13
|
s.require_paths = %w(lib)
|
14
14
|
s.summary = 'Eigenclasses in ruby'
|
15
|
-
s.test_files = `git ls-files --
|
15
|
+
s.test_files = `git ls-files -- spec/*`.split("\n")
|
16
16
|
s.version = Eigenclass::VERSION
|
17
17
|
|
18
18
|
s.add_development_dependency 'codeclimate-test-reporter'
|
data/lib/eigenclass.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'forwardable'
|
2
2
|
require 'eigenclass/version'
|
3
3
|
|
4
|
+
# Provides access to an object's {eigenclass} and defines
|
5
|
+
# some convenient helper methods to interact with it.
|
4
6
|
module Eigenclass
|
5
7
|
extend Forwardable
|
6
8
|
|
@@ -11,6 +13,8 @@ module Eigenclass
|
|
11
13
|
def_delegator :eigenclass, :instance_eval, :eigenclass_eval
|
12
14
|
def_delegator :eigenclass, :instance_exec, :eigenclass_exec
|
13
15
|
|
16
|
+
# Alias of {Object#singleton_class}
|
17
|
+
# @see http://ruby-doc.org/core-1.9.2/Object.html#method-i-singleton_class
|
14
18
|
def eigenclass
|
15
19
|
class << self
|
16
20
|
self
|
data/lib/eigenclass/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -1,97 +1,13 @@
|
|
1
1
|
if ENV['CODECLIMATE_REPO_TOKEN']
|
2
2
|
require 'codeclimate-test-reporter'
|
3
3
|
CodeClimate::TestReporter.start
|
4
|
+
else
|
5
|
+
require 'simplecov'
|
6
|
+
SimpleCov.start { add_filter('/vendor/bundle/') }
|
4
7
|
end
|
5
8
|
|
6
9
|
require 'shoulda'
|
7
10
|
|
8
|
-
# This file was generated by the `rspec --init` command. Conventionally, all
|
9
|
-
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
10
|
-
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
11
|
-
# this file to always be loaded, without a need to explicitly require it in any
|
12
|
-
# files.
|
13
|
-
#
|
14
|
-
# Given that it is always loaded, you are encouraged to keep this file as
|
15
|
-
# light-weight as possible. Requiring heavyweight dependencies from this file
|
16
|
-
# will add to the boot time of your test suite on EVERY test run, even for an
|
17
|
-
# individual file that may not need all of that loaded. Instead, consider making
|
18
|
-
# a separate helper file that requires the additional dependencies and performs
|
19
|
-
# the additional setup, and require it from the spec files that actually need
|
20
|
-
# it.
|
21
|
-
#
|
22
|
-
# The `.rspec` file also contains a few flags that are not defaults but that
|
23
|
-
# users commonly want.
|
24
|
-
#
|
25
|
-
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
26
11
|
RSpec.configure do |config|
|
27
|
-
|
28
|
-
# assertion/expectation library such as wrong or the stdlib/minitest
|
29
|
-
# assertions if you prefer.
|
30
|
-
config.expect_with :rspec do |expectations|
|
31
|
-
# This option will default to `true` in RSpec 4. It makes the `description`
|
32
|
-
# and `failure_message` of custom matchers include text for helper methods
|
33
|
-
# defined using `chain`, e.g.:
|
34
|
-
# be_bigger_than(2).and_smaller_than(4).description
|
35
|
-
# # => "be bigger than 2 and smaller than 4"
|
36
|
-
# ...rather than:
|
37
|
-
# # => "be bigger than 2"
|
38
|
-
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
39
|
-
end
|
40
|
-
|
41
|
-
# rspec-mocks config goes here. You can use an alternate test double
|
42
|
-
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
43
|
-
config.mock_with :rspec do |mocks|
|
44
|
-
# Prevents you from mocking or stubbing a method that does not exist on
|
45
|
-
# a real object. This is generally recommended, and will default to
|
46
|
-
# `true` in RSpec 4.
|
47
|
-
mocks.verify_partial_doubles = true
|
48
|
-
end
|
49
|
-
|
50
|
-
# The settings below are suggested to provide a good initial experience
|
51
|
-
# with RSpec, but feel free to customize to your heart's content.
|
52
|
-
|
53
|
-
# These two settings work together to allow you to limit a spec run
|
54
|
-
# to individual examples or groups you care about by tagging them with
|
55
|
-
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
56
|
-
# get run.
|
57
|
-
config.filter_run :focus
|
58
|
-
config.run_all_when_everything_filtered = true
|
59
|
-
|
60
|
-
# Limits the available syntax to the non-monkey patched syntax that is
|
61
|
-
# recommended. For more details, see:
|
62
|
-
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
63
|
-
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
64
|
-
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
65
|
-
config.disable_monkey_patching!
|
66
|
-
|
67
|
-
# This setting enables warnings. It's recommended, but in some cases may
|
68
|
-
# be too noisy due to issues in dependencies.
|
69
|
-
config.warnings = true
|
70
|
-
|
71
|
-
# Many RSpec users commonly either run the entire suite or an individual
|
72
|
-
# file, and it's useful to allow more verbose output when running an
|
73
|
-
# individual spec file.
|
74
|
-
if config.files_to_run.one?
|
75
|
-
# Use the documentation formatter for detailed output,
|
76
|
-
# unless a formatter has already been configured
|
77
|
-
# (e.g. via a command-line flag).
|
78
|
-
config.default_formatter = 'doc'
|
79
|
-
end
|
80
|
-
|
81
|
-
# Print the 10 slowest examples and example groups at the
|
82
|
-
# end of the spec run, to help surface which specs are running
|
83
|
-
# particularly slow.
|
84
|
-
config.profile_examples = 10
|
85
|
-
|
86
|
-
# Run specs in random order to surface order dependencies. If you find an
|
87
|
-
# order dependency and want to debug it, you can fix the order by providing
|
88
|
-
# the seed, which is printed after each run.
|
89
|
-
# --seed 1234
|
90
|
-
config.order = :random
|
91
|
-
|
92
|
-
# Seed global randomization in this process using the `--seed` CLI option.
|
93
|
-
# Setting this allows you to use `--seed` to deterministically reproduce
|
94
|
-
# test failures related to randomization by passing the same `--seed` value
|
95
|
-
# as the one that triggered the failure.
|
96
|
-
Kernel.srand config.seed
|
12
|
+
config.raise_errors_for_deprecations!
|
97
13
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: eigenclass
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sean Huber
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: codeclimate-test-reporter
|
@@ -65,7 +65,7 @@ files:
|
|
65
65
|
- Gemfile
|
66
66
|
- Gemfile.lock
|
67
67
|
- LICENSE
|
68
|
-
- README.
|
68
|
+
- README.md
|
69
69
|
- eigenclass.gemspec
|
70
70
|
- lib/eigenclass.rb
|
71
71
|
- lib/eigenclass/version.rb
|
@@ -81,7 +81,7 @@ rdoc_options:
|
|
81
81
|
- "--inline-source"
|
82
82
|
- "--line-numbers"
|
83
83
|
- "--main"
|
84
|
-
- README.
|
84
|
+
- README.md
|
85
85
|
require_paths:
|
86
86
|
- lib
|
87
87
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -100,4 +100,6 @@ rubygems_version: 2.4.5
|
|
100
100
|
signing_key:
|
101
101
|
specification_version: 4
|
102
102
|
summary: Eigenclasses in ruby
|
103
|
-
test_files:
|
103
|
+
test_files:
|
104
|
+
- spec/eigenclass_spec.rb
|
105
|
+
- spec/spec_helper.rb
|
data/README.rdoc
DELETED
@@ -1,96 +0,0 @@
|
|
1
|
-
= eigenclass - {<img src="https://secure.travis-ci.org/shuber/eigenclass.png"/>}[http://travis-ci.org/shuber/eigenclass] {<img src="https://codeclimate.com/github/shuber/eigenclass/badges/gpa.svg" />}[https://codeclimate.com/github/shuber/eigenclass] {<img src="https://codeclimate.com/github/shuber/eigenclass/badges/coverage.svg" />}[https://codeclimate.com/github/shuber/eigenclass]
|
2
|
-
|
3
|
-
Eigenclasses (aka metaclasses or singleton classes) in ruby.
|
4
|
-
|
5
|
-
Check out the implementations for {metaclasses}[http://en.wikipedia.org/wiki/Metaclass] in other languages for more examples.
|
6
|
-
|
7
|
-
*Note*: This gem was originally written back in 2009. Since then, Ruby has introduced a couple new methods which provide the same functionality as this gem's <tt>eigenclass</tt> and <tt>edefine_method</tt> methods.
|
8
|
-
|
9
|
-
* {Object#singleton_class}[http://ruby-doc.org/core-1.9.2/Object.html#method-i-singleton_class]
|
10
|
-
* {Object#define_singleton_method}[http://ruby-doc.org/core-1.9.2/Object.html#method-i-define_singleton_method]
|
11
|
-
|
12
|
-
|
13
|
-
== Installation
|
14
|
-
|
15
|
-
gem install eigenclass
|
16
|
-
|
17
|
-
|
18
|
-
== Usage
|
19
|
-
|
20
|
-
Everything in Ruby is an object, including classes.
|
21
|
-
|
22
|
-
class SomeObject
|
23
|
-
end
|
24
|
-
|
25
|
-
Every object has an <tt>eigenclass</tt>.
|
26
|
-
|
27
|
-
SomeObject.eigenclass #=> #<Class:#<SomeObject:0x007f9611030300>>
|
28
|
-
|
29
|
-
The implementation of the <tt>eigenclass</tt> method is pretty simple.
|
30
|
-
|
31
|
-
class Object
|
32
|
-
def eigenclass
|
33
|
-
class << self
|
34
|
-
self
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
Evaluating code within the context of an object's <tt>eigenclass</tt> allows us to do some pretty cool things like defining class level attributes.
|
40
|
-
|
41
|
-
SomeObject.eigenclass_eval do
|
42
|
-
attr_accessor :testing
|
43
|
-
end
|
44
|
-
|
45
|
-
SomeObject.testing = :example
|
46
|
-
SomeObject.testing #=> :example
|
47
|
-
|
48
|
-
The convenience methods for defining class level methods makes this even easier.
|
49
|
-
|
50
|
-
class SomeObject
|
51
|
-
eattr_accessor :test_accessor
|
52
|
-
eattr_reader :test_reader
|
53
|
-
eattr_writer :test_writer
|
54
|
-
|
55
|
-
edefine_method(:test_class_method) do
|
56
|
-
:test_define
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
SomeObject.test_class_method #=> :test_define
|
61
|
-
|
62
|
-
Since all objects have an <tt>eigenclass</tt>, we can even define methods on individual _instances_ of a class.
|
63
|
-
|
64
|
-
object = SomeObject.new
|
65
|
-
object.eattr_accessor :example
|
66
|
-
object.example = "cool"
|
67
|
-
object.example #=> cool
|
68
|
-
|
69
|
-
other_object = SomeObject.new
|
70
|
-
other_object.example #=> NoMethodError undefined method `example' for #<SomeObject:0x007fee348dde00>
|
71
|
-
|
72
|
-
This is pretty incredible! Ruby is like one big plugin framework - with an awesome standard library!
|
73
|
-
|
74
|
-
|
75
|
-
== API
|
76
|
-
|
77
|
-
* {eattr_accessor}[http://ruby-doc.org/core-1.9.3/Module.html#method-i-attr_accessor]
|
78
|
-
* {eattr_reader}[http://ruby-doc.org/core-1.9.3/Module.html#method-i-attr_reader]
|
79
|
-
* {eattr_writer}[http://ruby-doc.org/core-1.9.3/Module.html#method-i-attr_writer]
|
80
|
-
* {edefine_method}[http://ruby-doc.org/core-1.9.3/Module.html#method-i-define_method]
|
81
|
-
* {eigenclass_eval}[http://ruby-doc.org/core-1.9.3/BasicObject.html#method-i-instance_eval]
|
82
|
-
* {eigenclass_exec}[http://ruby-doc.org/core-1.9.3/BasicObject.html#method-i-instance_exec]
|
83
|
-
|
84
|
-
|
85
|
-
== Testing
|
86
|
-
|
87
|
-
bundle exec rspec
|
88
|
-
|
89
|
-
|
90
|
-
== Contributing
|
91
|
-
|
92
|
-
* Fork the project.
|
93
|
-
* Make your feature addition or bug fix.
|
94
|
-
* Add tests for it. This is important so I don't break it in a future version unintentionally.
|
95
|
-
* Commit, do not mess with Rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
96
|
-
* Send me a pull request. Bonus points for topic branches.
|