equalizer 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +37 -0
- data/.rvmrc +1 -0
- data/.travis.yml +18 -0
- data/Gemfile +56 -0
- data/Guardfile +18 -0
- data/LICENSE +21 -0
- data/README.md +105 -0
- data/Rakefile +9 -0
- data/TODO +0 -0
- data/config/flay.yml +3 -0
- data/config/flog.yml +2 -0
- data/config/roodi.yml +18 -0
- data/config/site.reek +91 -0
- data/config/yardstick.yml +2 -0
- data/equalizer.gemspec +24 -0
- data/lib/equalizer.rb +121 -0
- data/lib/equalizer/version.rb +5 -0
- data/spec/rcov.opts +7 -0
- data/spec/shared/command_method_behavior.rb +7 -0
- data/spec/shared/each_method_behaviour.rb +15 -0
- data/spec/shared/hash_method_behavior.rb +17 -0
- data/spec/shared/idempotent_method_behavior.rb +7 -0
- data/spec/shared/invertible_method_behaviour.rb +9 -0
- data/spec/spec.opts +3 -0
- data/spec/spec_helper.rb +11 -0
- data/spec/support/config_alias.rb +3 -0
- data/spec/unit/equalizer/class_method/new_spec.rb +134 -0
- data/spec/unit/equalizer/methods/eql_spec.rb +49 -0
- data/spec/unit/equalizer/methods/equal_value_spec.rb +85 -0
- data/tasks/metrics/ci.rake +9 -0
- data/tasks/metrics/flay.rake +45 -0
- data/tasks/metrics/flog.rake +49 -0
- data/tasks/metrics/heckle.rake +208 -0
- data/tasks/metrics/metric_fu.rake +31 -0
- data/tasks/metrics/reek.rake +21 -0
- data/tasks/metrics/roodi.rake +19 -0
- data/tasks/metrics/yardstick.rake +25 -0
- data/tasks/spec.rake +60 -0
- data/tasks/yard.rake +11 -0
- metadata +172 -0
data/.gitignore
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
## MAC OS
|
2
|
+
.DS_Store
|
3
|
+
|
4
|
+
## TEXTMATE
|
5
|
+
*.tmproj
|
6
|
+
tmtags
|
7
|
+
|
8
|
+
## EMACS
|
9
|
+
*~
|
10
|
+
\#*
|
11
|
+
.\#*
|
12
|
+
|
13
|
+
## VIM
|
14
|
+
*.swp
|
15
|
+
|
16
|
+
## Rubinius
|
17
|
+
*.rbc
|
18
|
+
.rbx
|
19
|
+
|
20
|
+
## PROJECT::GENERAL
|
21
|
+
*.gem
|
22
|
+
coverage
|
23
|
+
profiling
|
24
|
+
turbulence
|
25
|
+
rdoc
|
26
|
+
pkg
|
27
|
+
tmp
|
28
|
+
doc
|
29
|
+
log
|
30
|
+
.yardoc
|
31
|
+
measurements
|
32
|
+
|
33
|
+
## BUNDLER
|
34
|
+
.bundle
|
35
|
+
Gemfile.lock
|
36
|
+
|
37
|
+
## PROJECT::SPECIFIC
|
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use @$(basename `pwd`) --create
|
data/.travis.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
language: ruby
|
2
|
+
bundler_args: --without yard guard
|
3
|
+
script: "bundle exec rake spec"
|
4
|
+
rvm:
|
5
|
+
- 1.8.7
|
6
|
+
- 1.9.2
|
7
|
+
- 1.9.3
|
8
|
+
- jruby-18mode
|
9
|
+
- jruby-19mode
|
10
|
+
- rbx-18mode
|
11
|
+
- rbx-19mode
|
12
|
+
- ree
|
13
|
+
- ruby-head
|
14
|
+
- jruby-head
|
15
|
+
notifications:
|
16
|
+
email:
|
17
|
+
- dan.kubb@gmail.com
|
18
|
+
- mbj@seonic.net
|
data/Gemfile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
gemspec
|
6
|
+
|
7
|
+
group :yard do
|
8
|
+
gem 'yard', '~> 0.8.3'
|
9
|
+
gem 'redcarpet', '~> 2.2.2', :platforms => [ :mri, :rbx ]
|
10
|
+
end
|
11
|
+
|
12
|
+
group :guard do
|
13
|
+
gem 'guard', '~> 1.5.4'
|
14
|
+
gem 'guard-bundler', '~> 1.0.0'
|
15
|
+
gem 'guard-rspec', '~> 1.2.1'
|
16
|
+
end
|
17
|
+
|
18
|
+
group :benchmarks do
|
19
|
+
gem 'rbench', '~> 0.2.3'
|
20
|
+
end
|
21
|
+
|
22
|
+
platform :jruby do
|
23
|
+
group :jruby do
|
24
|
+
gem 'jruby-openssl', '~> 0.7.4'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
group :metrics do
|
29
|
+
gem 'flay', '~> 1.4.3'
|
30
|
+
gem 'flog', '~> 2.5.3'
|
31
|
+
gem 'reek', '~> 1.2.8', :github => 'dkubb/reek'
|
32
|
+
gem 'roodi', '~> 2.1.0'
|
33
|
+
gem 'yardstick', '~> 0.8.0'
|
34
|
+
gem 'simplecov'
|
35
|
+
|
36
|
+
platforms :ruby_18, :ruby_19 do
|
37
|
+
# this indirectly depends on ffi which does not build on ruby-head
|
38
|
+
gem 'yard-spellcheck', '~> 0.1.5'
|
39
|
+
end
|
40
|
+
|
41
|
+
platforms :mri_18 do
|
42
|
+
gem 'arrayfields', '~> 4.7.4' # for metric_fu
|
43
|
+
gem 'fattr', '~> 2.2.0' # for metric_fu
|
44
|
+
gem 'heckle', '~> 1.4.3'
|
45
|
+
gem 'json', '~> 1.7.3' # for metric_fu rake task
|
46
|
+
gem 'map', '~> 6.2.0' # for metric_fu
|
47
|
+
gem 'metric_fu', '~> 2.1.1'
|
48
|
+
gem 'mspec', '~> 1.5.17'
|
49
|
+
gem 'rcov', '~> 1.0.0'
|
50
|
+
gem 'ruby2ruby', '= 1.2.2' # for heckle
|
51
|
+
end
|
52
|
+
|
53
|
+
platforms :rbx do
|
54
|
+
gem 'pelusa', '~> 0.2.1'
|
55
|
+
end
|
56
|
+
end
|
data/Guardfile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
guard :bundler do
|
4
|
+
watch('Gemfile')
|
5
|
+
end
|
6
|
+
|
7
|
+
guard :rspec do
|
8
|
+
# run all specs if the spec_helper or supporting files files are modified
|
9
|
+
watch('spec/spec_helper.rb') { 'spec' }
|
10
|
+
watch(%r{\Aspec/(?:lib|support|shared)/.+\.rb\z}) { 'spec' }
|
11
|
+
|
12
|
+
# run unit specs if associated lib code is modified
|
13
|
+
watch(%r{\Alib/(.+)\.rb\z}) { |m| Dir["spec/unit/#{m[1]}"] }
|
14
|
+
watch("lib/#{File.basename(File.expand_path('../', __FILE__))}.rb") { 'spec' }
|
15
|
+
|
16
|
+
# run a spec if it is modified
|
17
|
+
watch(%r{\Aspec/(?:unit|integration)/.+_spec\.rb\z})
|
18
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2009-2012 Dan Kubb
|
2
|
+
Copyright (c) 2012 Markus Schirp (packaging)
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
a copy of this software and associated documentation files (the
|
6
|
+
"Software"), to deal in the Software without restriction, including
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
equalizer
|
2
|
+
=========
|
3
|
+
|
4
|
+
[![Build Status](https://secure.travis-ci.org/dkubb/equalizer.png?branch=master)](http://travis-ci.org/dkubb/equalizer)
|
5
|
+
[![Dependency Status](https://gemnasium.com/dkubb/equalizer.png)](https://gemnasium.com/dkubb/equalizer)
|
6
|
+
[![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/dkubb/equalizer)
|
7
|
+
|
8
|
+
Module to define equality, equivalence and inspection methods
|
9
|
+
|
10
|
+
Installation
|
11
|
+
------------
|
12
|
+
|
13
|
+
With Rubygems:
|
14
|
+
|
15
|
+
```bash
|
16
|
+
$ gem install equalizer
|
17
|
+
$ irb -rubygems
|
18
|
+
>> require 'equalizer'
|
19
|
+
=> true
|
20
|
+
```
|
21
|
+
|
22
|
+
With git and local working copy:
|
23
|
+
|
24
|
+
```bash
|
25
|
+
$ git clone git://github.com/dkubb/equalizer.git
|
26
|
+
$ cd equalizer
|
27
|
+
$ rake install
|
28
|
+
$ irb -rubygems
|
29
|
+
>> require 'equalizer'
|
30
|
+
=> true
|
31
|
+
```
|
32
|
+
|
33
|
+
Examples
|
34
|
+
--------
|
35
|
+
|
36
|
+
``` ruby
|
37
|
+
class GeoLocation
|
38
|
+
include Equalizer.new(:latitude, :longitude)
|
39
|
+
|
40
|
+
attr_reader :latitidue, :longitude
|
41
|
+
|
42
|
+
def initialize(latitude, longitude)
|
43
|
+
@latitude, @longitude = latitude, longitude
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
point_a = GeoLocation.new(1, 2)
|
48
|
+
point_b = GeoLocation.new(1, 2)
|
49
|
+
point_c = GeoLocation.new(2, 2)
|
50
|
+
|
51
|
+
point_a.inspect # => "#<GeoLocation latitude=1 longitude=2>"
|
52
|
+
|
53
|
+
point_a == point_b # => true
|
54
|
+
point_a.hash == point_b.hash # => true
|
55
|
+
point_a.eql?(point_b) # => true
|
56
|
+
point_a.equal?(point_b) # => false
|
57
|
+
|
58
|
+
point_a == point_c # => false
|
59
|
+
point_a.hash == point_c.hash # => false
|
60
|
+
point_a.eql?(point_c) # => false
|
61
|
+
point_a.equal?(point_c) # => false
|
62
|
+
```
|
63
|
+
|
64
|
+
Credits
|
65
|
+
-------
|
66
|
+
|
67
|
+
* Dan Kubb ([dkubb](https://github.com/dkubb))
|
68
|
+
* Piotr Solnica ([solnic](https://github.com/solnic))
|
69
|
+
* Markus Schirp ([mbj](https://github.com/mbj))
|
70
|
+
|
71
|
+
Contributing
|
72
|
+
------------
|
73
|
+
|
74
|
+
* If you want your code merged into the mainline, please discuss the proposed changes with me before doing any work on it. This library is still in early development, and the direction it is going may not always be clear. Some features may not be appropriate yet, may need to be deferred until later when the foundation for them is laid, or may be more applicable in a plugin.
|
75
|
+
* Fork the project.
|
76
|
+
* Make your feature addition or bug fix.
|
77
|
+
* Follow this [style guide](https://github.com/dkubb/styleguide).
|
78
|
+
* Add specs for it. This is important so I don't break it in a future version unintentionally. Tests must cover all branches within the code, and code must be fully covered.
|
79
|
+
* 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)
|
80
|
+
* Run "rake ci". This must pass and not show any regressions in the metrics for the code to be merged.
|
81
|
+
* Send me a pull request. Bonus points for topic branc
|
82
|
+
|
83
|
+
License
|
84
|
+
-------
|
85
|
+
|
86
|
+
Copyright (c) 2011-2012 Dan Kubb
|
87
|
+
|
88
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
89
|
+
a copy of this software and associated documentation files (the
|
90
|
+
"Software"), to deal in the Software without restriction, including
|
91
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
92
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
93
|
+
permit persons to whom the Software is furnished to do so, subject to
|
94
|
+
the following conditions:
|
95
|
+
|
96
|
+
The above copyright notice and this permission notice shall be
|
97
|
+
included in all copies or substantial portions of the Software.
|
98
|
+
|
99
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
100
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
101
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
102
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
103
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
104
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
105
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/TODO
ADDED
File without changes
|
data/config/flay.yml
ADDED
data/config/flog.yml
ADDED
data/config/roodi.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
---
|
2
|
+
AbcMetricMethodCheck: { score: 10.3 }
|
3
|
+
AssignmentInConditionalCheck: { }
|
4
|
+
CaseMissingElseCheck: { }
|
5
|
+
ClassLineCountCheck: { line_count: 293 }
|
6
|
+
ClassNameCheck: { pattern: !ruby/regexp '/\A(?:[A-Z]+|[A-Z][a-z](?:[A-Z]?[a-z])+)\z/' }
|
7
|
+
ClassVariableCheck: { }
|
8
|
+
CyclomaticComplexityBlockCheck: { complexity: 2 }
|
9
|
+
CyclomaticComplexityMethodCheck: { complexity: 4 }
|
10
|
+
EmptyRescueBodyCheck: { }
|
11
|
+
ForLoopCheck: { }
|
12
|
+
# TODO: decrease line_count to 5 to 10
|
13
|
+
MethodLineCountCheck: { line_count: 14 }
|
14
|
+
MethodNameCheck: { pattern: !ruby/regexp '/\A(?:[a-z\d](?:_?[a-z\d])+[?!=]?|\[\]=?|==|<=>|[+*&|-])\z/' }
|
15
|
+
ModuleLineCountCheck: { line_count: 295 }
|
16
|
+
ModuleNameCheck: { pattern: !ruby/regexp '/\A(?:[A-Z]+|[A-Z][a-z](?:[A-Z]?[a-z])+)\z/' }
|
17
|
+
# TODO: decrease parameter_count to 2 or less
|
18
|
+
ParameterNumberCheck: { parameter_count: 3 }
|
data/config/site.reek
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
---
|
2
|
+
UncommunicativeParameterName:
|
3
|
+
accept: []
|
4
|
+
exclude: []
|
5
|
+
enabled: true
|
6
|
+
reject:
|
7
|
+
- !ruby/regexp /^.$/
|
8
|
+
- !ruby/regexp /[0-9]$/
|
9
|
+
- !ruby/regexp /[A-Z]/
|
10
|
+
LargeClass:
|
11
|
+
max_methods: 10
|
12
|
+
exclude: []
|
13
|
+
enabled: true
|
14
|
+
max_instance_variables: 2
|
15
|
+
UncommunicativeMethodName:
|
16
|
+
accept: []
|
17
|
+
exclude: []
|
18
|
+
enabled: true
|
19
|
+
reject:
|
20
|
+
- !ruby/regexp /^[a-z]$/
|
21
|
+
- !ruby/regexp /[0-9]$/
|
22
|
+
- !ruby/regexp /[A-Z]/
|
23
|
+
LongParameterList:
|
24
|
+
max_params: 2 # TODO: decrease max_params to 2
|
25
|
+
exclude: []
|
26
|
+
enabled: true
|
27
|
+
overrides: {}
|
28
|
+
FeatureEnvy:
|
29
|
+
exclude: []
|
30
|
+
enabled: true
|
31
|
+
ClassVariable:
|
32
|
+
exclude: []
|
33
|
+
enabled: true
|
34
|
+
BooleanParameter:
|
35
|
+
exclude: []
|
36
|
+
enabled: true
|
37
|
+
IrresponsibleModule:
|
38
|
+
exclude: []
|
39
|
+
enabled: true
|
40
|
+
UncommunicativeModuleName:
|
41
|
+
accept: []
|
42
|
+
exclude: []
|
43
|
+
enabled: true
|
44
|
+
reject:
|
45
|
+
- !ruby/regexp /^.$/
|
46
|
+
- !ruby/regexp /[0-9]$/
|
47
|
+
NestedIterators:
|
48
|
+
ignore_iterators: []
|
49
|
+
exclude: []
|
50
|
+
enabled: true
|
51
|
+
max_allowed_nesting: 2
|
52
|
+
LongMethod:
|
53
|
+
max_statements: 7 # TODO: decrease max_statements to 5 or less
|
54
|
+
exclude: []
|
55
|
+
enabled: true
|
56
|
+
Duplication:
|
57
|
+
allow_calls: []
|
58
|
+
exclude: []
|
59
|
+
enabled: true
|
60
|
+
max_calls: 1
|
61
|
+
UtilityFunction:
|
62
|
+
max_helper_calls: 1
|
63
|
+
exclude: []
|
64
|
+
enabled: true
|
65
|
+
Attribute:
|
66
|
+
exclude: []
|
67
|
+
enabled: false
|
68
|
+
UncommunicativeVariableName:
|
69
|
+
accept: []
|
70
|
+
exclude: []
|
71
|
+
enabled: true
|
72
|
+
reject:
|
73
|
+
- !ruby/regexp /^.$/
|
74
|
+
- !ruby/regexp /[0-9]$/
|
75
|
+
- !ruby/regexp /[A-Z]/
|
76
|
+
SimulatedPolymorphism:
|
77
|
+
exclude: []
|
78
|
+
enabled: true
|
79
|
+
max_ifs: 1
|
80
|
+
DataClump:
|
81
|
+
exclude: []
|
82
|
+
enabled: true
|
83
|
+
max_copies: 1
|
84
|
+
min_clump_size: 3
|
85
|
+
ControlCouple:
|
86
|
+
exclude: []
|
87
|
+
enabled: true
|
88
|
+
LongYieldList:
|
89
|
+
max_params: 1
|
90
|
+
exclude: []
|
91
|
+
enabled: true
|
data/equalizer.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require File.expand_path('../lib/equalizer/version', __FILE__)
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.name = 'equalizer'
|
7
|
+
gem.version = Equalizer::VERSION.dup
|
8
|
+
gem.authors = ['Dan Kubb', 'Markus Schirp']
|
9
|
+
gem.email = %w[dan.kubb@gmail.com mbj@seonic.net]
|
10
|
+
gem.description = 'Module to define equality, equivalence and inspection methods'
|
11
|
+
gem.summary = gem.description
|
12
|
+
gem.homepage = 'https://github.com/dkubb/equalizer'
|
13
|
+
|
14
|
+
gem.require_paths = %w[lib]
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.test_files = `git ls-files -- {spec}/*`.split($/)
|
17
|
+
gem.extra_rdoc_files = %w[LICENSE README.md TODO]
|
18
|
+
|
19
|
+
gem.add_runtime_dependency('backports', '~> 2.6.4')
|
20
|
+
gem.add_runtime_dependency('adamantium', '~> 0.0.3')
|
21
|
+
|
22
|
+
gem.add_development_dependency('rake', '~> 10.0.2')
|
23
|
+
gem.add_development_dependency('rspec', '~> 1.3.2')
|
24
|
+
end
|
data/lib/equalizer.rb
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'adamantium'
|
4
|
+
|
5
|
+
# Define equality, equivalence and inspection methods
|
6
|
+
class Equalizer < Module
|
7
|
+
include Adamantium
|
8
|
+
|
9
|
+
# Initialize an Equalizer with the given keys
|
10
|
+
#
|
11
|
+
# Will use the keys with which it is initialized to define #cmp?,
|
12
|
+
# #hash, and #inspect
|
13
|
+
#
|
14
|
+
# @param [Array<Symbol>] keys
|
15
|
+
#
|
16
|
+
# @return [undefined]
|
17
|
+
#
|
18
|
+
# @api private
|
19
|
+
def initialize(*keys)
|
20
|
+
@keys = keys
|
21
|
+
define_methods
|
22
|
+
include_comparison_methods
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
# Define the equalizer methods based on #keys
|
28
|
+
#
|
29
|
+
# @return [undefined]
|
30
|
+
#
|
31
|
+
# @api private
|
32
|
+
def define_methods
|
33
|
+
define_cmp_method
|
34
|
+
define_hash_method
|
35
|
+
define_inspect_method
|
36
|
+
end
|
37
|
+
|
38
|
+
# Define an #cmp? method based on the instance's values identified by #keys
|
39
|
+
#
|
40
|
+
# @return [undefined]
|
41
|
+
#
|
42
|
+
# @api private
|
43
|
+
def define_cmp_method
|
44
|
+
keys = @keys
|
45
|
+
define_method(:cmp?) do |comparator, other|
|
46
|
+
keys.all? { |key| send(key).send(comparator, other.send(key)) }
|
47
|
+
end
|
48
|
+
private :cmp?
|
49
|
+
end
|
50
|
+
|
51
|
+
# Define a #hash method based on the instance's values identified by #keys
|
52
|
+
#
|
53
|
+
# @return [undefined]
|
54
|
+
#
|
55
|
+
# @api private
|
56
|
+
def define_hash_method
|
57
|
+
keys = @keys
|
58
|
+
define_method(:hash) do
|
59
|
+
keys.map { |key| send(key).hash }.reduce(self.class.hash, :^)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# Define an inspect method that reports the values of the instance's keys
|
64
|
+
#
|
65
|
+
# @return [undefined]
|
66
|
+
#
|
67
|
+
# @api private
|
68
|
+
def define_inspect_method
|
69
|
+
keys = @keys
|
70
|
+
define_method(:inspect) do
|
71
|
+
klass = self.class
|
72
|
+
name = klass.name || klass.inspect
|
73
|
+
"#<#{name}#{keys.map { |key| " #{key}=#{send(key).inspect}" }.join}>"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# Include the #eql? and #== methods
|
78
|
+
#
|
79
|
+
# @return [undefined]
|
80
|
+
#
|
81
|
+
# @api private
|
82
|
+
def include_comparison_methods
|
83
|
+
module_eval { include Methods }
|
84
|
+
end
|
85
|
+
|
86
|
+
# The comparison methods
|
87
|
+
module Methods
|
88
|
+
|
89
|
+
# Compare the object with other object for equality
|
90
|
+
#
|
91
|
+
# @example
|
92
|
+
# object.eql?(other) # => true or false
|
93
|
+
#
|
94
|
+
# @param [Object] other
|
95
|
+
# the other object to compare with
|
96
|
+
#
|
97
|
+
# @return [Boolean]
|
98
|
+
#
|
99
|
+
# @api public
|
100
|
+
def eql?(other)
|
101
|
+
instance_of?(other.class) && cmp?(__method__, other)
|
102
|
+
end
|
103
|
+
|
104
|
+
# Compare the object with other object for equivalency
|
105
|
+
#
|
106
|
+
# @example
|
107
|
+
# object == other # => true or false
|
108
|
+
#
|
109
|
+
# @param [Object] other
|
110
|
+
# the other object to compare with
|
111
|
+
#
|
112
|
+
# @return [Boolean]
|
113
|
+
#
|
114
|
+
# @api public
|
115
|
+
def ==(other)
|
116
|
+
other = coerce(other) if respond_to?(:coerce, true)
|
117
|
+
!!(self.class <=> other.class) && cmp?(__method__, other)
|
118
|
+
end
|
119
|
+
|
120
|
+
end # module Methods
|
121
|
+
end # class Equalizer
|