equatable 0.3.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +35 -0
- data/LICENSE.txt +2 -2
- data/README.md +67 -23
- data/lib/equatable.rb +21 -22
- data/lib/equatable/version.rb +2 -2
- metadata +41 -57
- data/.gitignore +0 -18
- data/.rspec +0 -2
- data/.rvmrc +0 -34
- data/.travis.yml +0 -20
- data/Gemfile +0 -3
- data/Rakefile +0 -12
- data/equatable.gemspec +0 -23
- data/spec/equatable/eql_spec.rb +0 -54
- data/spec/equatable/equal_spec.rb +0 -76
- data/spec/equatable/include_spec.rb +0 -170
- data/spec/equatable/subclass_spec.rb +0 -66
- data/spec/spec_helper.rb +0 -11
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 260460daef33f12a7c0995243cf628bbd2eb81f9e42f0bf00a7b0d226e91eadf
|
4
|
+
data.tar.gz: f38cd2a3d53cabb2f9fdbaa8c4e446be59aecc08b6121385acf4de2d5c72d116
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4b7074a4d0d50f379e5831530ea473745b9a22257d0583d67d974830998f9e85d5418cedb37990ff6e5a1607556626343746025e2e3c937a93e7217b0a662daa
|
7
|
+
data.tar.gz: 9e980c6ee681d0e359200803502f0a17c6be5a655a863cf6fd9de5fea17b2419f71a7eb5b6ccea6c4bcd729327f1d3653bd8f98c60b2a0c0aa39ba3776f03381
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Change log
|
2
|
+
|
3
|
+
## [v0.7.0] - 2021-04-05
|
4
|
+
|
5
|
+
### Changed
|
6
|
+
* Change gemspec to remove test and rake files to reduce gem size
|
7
|
+
* Change to remove bundler as a dev dependency and relax rspec upper constraint
|
8
|
+
* Change to require Ruby 2.0 or higher
|
9
|
+
|
10
|
+
## [v0.6.1] - 2019-06-26
|
11
|
+
|
12
|
+
### Added
|
13
|
+
* Add license key to gemspec
|
14
|
+
|
15
|
+
## [v0.6.0] - 2019-06-16
|
16
|
+
|
17
|
+
### Added
|
18
|
+
* Add dev dependencies to gemspec
|
19
|
+
|
20
|
+
### Changed
|
21
|
+
* Change to limit Ruby >= 1.8.7
|
22
|
+
* Change gemspec to load files directly instead of git
|
23
|
+
|
24
|
+
## [v0.1.0] - 2012-12-09
|
25
|
+
|
26
|
+
* Initial implementation and release
|
27
|
+
|
28
|
+
[v0.7.0]: https://github.com/piotrmurach/equatable/compare/v0.6.1...v0.7.0
|
29
|
+
[v0.6.1]: https://github.com/piotrmurach/equatable/compare/v0.6.0...v0.6.1
|
30
|
+
[v0.6.0]: https://github.com/piotrmurach/equatable/compare/v0.5.0...v0.6.0
|
31
|
+
[v0.5.0]: https://github.com/piotrmurach/equatable/compare/v0.4.0...v0.5.0
|
32
|
+
[v0.4.0]: https://github.com/piotrmurach/equatable/compare/v0.3.0...v0.4.0
|
33
|
+
[v0.3.0]: https://github.com/piotrmurach/equatable/compare/v0.2.0...v0.3.0
|
34
|
+
[v0.2.0]: https://github.com/piotrmurach/equatable/compare/v0.1.0...v0.2.0
|
35
|
+
[v0.1.0]: https://github.com/piotrmurach/equatable/compare/v0.1.0
|
data/LICENSE.txt
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c) 2012 Piotr Murach
|
1
|
+
Copyright (c) 2012 Piotr Murach (piotrmurach.com)
|
2
2
|
|
3
3
|
MIT License
|
4
4
|
|
@@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
19
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
20
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
21
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,18 +1,28 @@
|
|
1
1
|
# Equatable
|
2
|
-
[![Gem Version](https://badge.fury.io/rb/equatable.png)](http://badge.fury.io/rb/equatable) [![Build Status](https://secure.travis-ci.org/peter-murach/equatable.png?branch=master)][travis] [![Code Climate](https://codeclimate.com/badge.png)][codeclimate]
|
3
2
|
|
4
|
-
[
|
5
|
-
[
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/equatable.svg)][gem]
|
4
|
+
[![Actions CI](https://github.com/piotrmurach/equatable/workflows/CI/badge.svg?branch=master)][gh_actions_ci]
|
5
|
+
[![Build status](https://ci.appveyor.com/api/projects/status/lsb02nm0g4c6guiu?svg=true)][appveyor]
|
6
|
+
[![Code Climate](https://codeclimate.com/github/piotrmurach/equatable/badges/gpa.svg)][codeclimate]
|
7
|
+
[![Coverage Status](https://coveralls.io/repos/github/piotrmurach/equatable/badge.svg)][coverage]
|
8
|
+
[![Inline docs](http://inch-ci.org/github/piotrmurach/equatable.svg?branch=master)][inchpages]
|
6
9
|
|
7
|
-
|
10
|
+
[gem]: http://badge.fury.io/rb/equatable
|
11
|
+
[gh_actions_ci]: https://github.com/piotrmurach/equatable/actions?query=workflow%3ACI
|
12
|
+
[appveyor]: https://ci.appveyor.com/project/piotrmurach/equatable
|
13
|
+
[codeclimate]: https://codeclimate.com/github/piotrmurach/equatable
|
14
|
+
[coverage]: https://coveralls.io/github/piotrmurach/equatable
|
15
|
+
[inchpages]: http://inch-ci.org/github/piotrmurach/equatable
|
8
16
|
|
9
|
-
|
17
|
+
> Provide equality comparison methods for objects based on their attributes.
|
18
|
+
|
19
|
+
By including this module, a class indicates that its instances have explicit general contracts for `==`, `eql?` and `hash` methods. Specifically the `eql?` contract requires that it implements an equivalence relation. By default, each instance of a class is equal only to itself. This is the right behaviour when you have distinct objects. However, it is the responsibility of any class to clearly define its equality. Failure to do so may prevent instances from behaving as expected when tested for uniqueness in Array#uniq or when used as Hash keys.
|
10
20
|
|
11
21
|
## Installation
|
12
22
|
|
13
23
|
Add this line to your application's Gemfile:
|
14
24
|
|
15
|
-
gem
|
25
|
+
gem "equatable"
|
16
26
|
|
17
27
|
And then execute:
|
18
28
|
|
@@ -27,33 +37,63 @@ Or install it yourself as:
|
|
27
37
|
It is assumed that your objects are value objects and the only values that affect equality comparison are the ones specified by your attribute readers. Each attribute reader should be a significant field in determining objects values.
|
28
38
|
|
29
39
|
```ruby
|
30
|
-
|
31
|
-
|
40
|
+
class Point
|
41
|
+
include Equatable
|
32
42
|
|
33
|
-
|
43
|
+
attr_reader :x, :y
|
34
44
|
|
35
|
-
|
36
|
-
|
37
|
-
end
|
45
|
+
def initialize(x, y)
|
46
|
+
@x, @y = x, y
|
38
47
|
end
|
48
|
+
end
|
39
49
|
|
40
|
-
|
41
|
-
|
42
|
-
|
50
|
+
point_1 = Point.new(1, 1)
|
51
|
+
point_2 = Point.new(1, 1)
|
52
|
+
point_3 = Point.new(1, 2)
|
43
53
|
|
44
|
-
|
45
|
-
|
46
|
-
|
54
|
+
point_1 == point_2 # => true
|
55
|
+
point_1.hash == point_2.hash # => true
|
56
|
+
point_1.eql?(point_2) # => true
|
57
|
+
point_1.equal?(point_2) # => false
|
47
58
|
|
48
|
-
|
49
|
-
|
50
|
-
|
59
|
+
point_1 == point_3 # => false
|
60
|
+
point_1.hash == point_3.hash # => false
|
61
|
+
point_1.eql?(point_3) # => false
|
62
|
+
point_1.equal?(point_3) # => false
|
51
63
|
|
64
|
+
point_1.inspect # => "#<Point x=1 y=1>"
|
52
65
|
```
|
53
66
|
|
67
|
+
## Attributes
|
68
|
+
|
54
69
|
It is important that the attribute readers should allow for performing deterministic computations on class instances. Therefore you should avoid specifying attributes that depend on unreliable resources like IP address that require network access.
|
55
70
|
|
56
|
-
|
71
|
+
## Subtypes
|
72
|
+
|
73
|
+
**Equatable** ensures that any important property of a type holds for its subtypes. However, please note that adding an extra attribute reader to a subclass will violate the equivalence contract, namely, the superclass will be equal to the subclass but reverse won't be true. For example:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
class ColorPoint < Point
|
77
|
+
attr_reader :color
|
78
|
+
|
79
|
+
def initialize(x, y, color)
|
80
|
+
super(x, y)
|
81
|
+
@color = color
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
point = Point.new(1, 1)
|
86
|
+
color_point = ColorPoint.new(1, 1, :red)
|
87
|
+
|
88
|
+
point == color_point # => true
|
89
|
+
color_point == point # => false
|
90
|
+
|
91
|
+
point.hash == color_point.hash # => false
|
92
|
+
point.eql?(color_point) # => false
|
93
|
+
point.equal?(color_point) # => false
|
94
|
+
```
|
95
|
+
|
96
|
+
The `ColorPoint` class demonstrates that extending a class with extra value property does not preserve the `equals` contract.
|
57
97
|
|
58
98
|
## Contributing
|
59
99
|
|
@@ -63,6 +103,10 @@ Note that adding a extra attr reader to a subclass will violate the equivalence
|
|
63
103
|
4. Push to the branch (`git push origin my-new-feature`)
|
64
104
|
5. Create new Pull Request
|
65
105
|
|
106
|
+
## Code of Conduct
|
107
|
+
|
108
|
+
Everyone interacting in the Equatable project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/piotrmurach/equatable/blob/master/CODE_OF_CONDUCT.md).
|
109
|
+
|
66
110
|
## Copyright
|
67
111
|
|
68
|
-
Copyright (c) 2012
|
112
|
+
Copyright (c) 2012 Piotr Murach. See LICENSE for further details.
|
data/lib/equatable.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "equatable/version"
|
4
4
|
|
5
5
|
# Make it easy to define equality and hash methods.
|
6
6
|
module Equatable
|
7
|
-
|
8
7
|
# Hook into module inclusion.
|
9
8
|
#
|
10
9
|
# @param [Module] base
|
@@ -17,11 +16,9 @@ module Equatable
|
|
17
16
|
super
|
18
17
|
base.extend(self)
|
19
18
|
base.class_eval do
|
20
|
-
define_comparison_attrs
|
21
19
|
include Methods
|
22
20
|
define_methods
|
23
21
|
end
|
24
|
-
self
|
25
22
|
end
|
26
23
|
|
27
24
|
# Holds all attributes used for comparison.
|
@@ -42,7 +39,7 @@ module Equatable
|
|
42
39
|
# @api public
|
43
40
|
def attr_reader(*args)
|
44
41
|
super
|
45
|
-
|
42
|
+
comparison_attrs.concat(args)
|
46
43
|
end
|
47
44
|
|
48
45
|
# Copy the comparison_attrs into the subclass.
|
@@ -57,24 +54,25 @@ module Equatable
|
|
57
54
|
|
58
55
|
private
|
59
56
|
|
60
|
-
# Define
|
57
|
+
# Define all methods needed for ensuring object's equality.
|
61
58
|
#
|
62
59
|
# @return [undefined]
|
63
60
|
#
|
64
61
|
# @api private
|
65
|
-
def
|
66
|
-
|
62
|
+
def define_methods
|
63
|
+
define_comparison_attrs
|
64
|
+
define_compare
|
65
|
+
define_hash
|
66
|
+
define_inspect
|
67
67
|
end
|
68
68
|
|
69
|
-
# Define
|
69
|
+
# Define class instance #comparison_attrs as an empty array.
|
70
70
|
#
|
71
71
|
# @return [undefined]
|
72
72
|
#
|
73
73
|
# @api private
|
74
|
-
def
|
75
|
-
|
76
|
-
define_hash
|
77
|
-
define_inspect
|
74
|
+
def define_comparison_attrs
|
75
|
+
instance_variable_set("@comparison_attrs", [])
|
78
76
|
end
|
79
77
|
|
80
78
|
# Define a #compare? method to check if the receiver is the same
|
@@ -86,8 +84,10 @@ module Equatable
|
|
86
84
|
def define_compare
|
87
85
|
define_method(:compare?) do |comparator, other|
|
88
86
|
klass = self.class
|
89
|
-
attrs = klass.comparison_attrs
|
90
|
-
attrs.all?
|
87
|
+
attrs = klass.comparison_attrs
|
88
|
+
attrs.all? do |attr|
|
89
|
+
other.respond_to?(attr) && send(attr).send(comparator, other.send(attr))
|
90
|
+
end
|
91
91
|
end
|
92
92
|
end
|
93
93
|
|
@@ -98,8 +98,8 @@ module Equatable
|
|
98
98
|
def define_hash
|
99
99
|
define_method(:hash) do
|
100
100
|
klass = self.class
|
101
|
-
attrs = klass.comparison_attrs
|
102
|
-
([klass] + attrs.map { |attr| send(attr)}).hash
|
101
|
+
attrs = klass.comparison_attrs
|
102
|
+
([klass] + attrs.map { |attr| send(attr) }).hash
|
103
103
|
end
|
104
104
|
end
|
105
105
|
|
@@ -112,14 +112,14 @@ module Equatable
|
|
112
112
|
def define_inspect
|
113
113
|
define_method(:inspect) do
|
114
114
|
klass = self.class
|
115
|
-
name
|
116
|
-
attrs = klass.comparison_attrs
|
115
|
+
name = klass.name || klass.inspect
|
116
|
+
attrs = klass.comparison_attrs
|
117
117
|
"#<#{name}#{attrs.map { |attr| " #{attr}=#{send(attr).inspect}" }.join}>"
|
118
118
|
end
|
119
119
|
end
|
120
120
|
|
121
|
+
# The equality methods
|
121
122
|
module Methods
|
122
|
-
|
123
123
|
# Compare two objects for equality based on their value
|
124
124
|
# and being an instance of the given class.
|
125
125
|
#
|
@@ -143,8 +143,7 @@ module Equatable
|
|
143
143
|
#
|
144
144
|
# @api public
|
145
145
|
def ==(other)
|
146
|
-
|
146
|
+
other.is_a?(self.class) && compare?(__method__, other)
|
147
147
|
end
|
148
|
-
|
149
148
|
end # Methods
|
150
149
|
end # Equatable
|
data/lib/equatable/version.rb
CHANGED
metadata
CHANGED
@@ -1,102 +1,86 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: equatable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.7.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Piotr Murach
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2021-04-05 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
16
|
-
requirement:
|
17
|
-
none: false
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
|
-
version_requirements:
|
25
|
-
- !ruby/object:Gem::Dependency
|
26
|
-
name: rake
|
27
|
-
requirement: &2160138980 !ruby/object:Gem::Requirement
|
28
|
-
none: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
29
23
|
requirements:
|
30
|
-
- -
|
24
|
+
- - ">="
|
31
25
|
- !ruby/object:Gem::Version
|
32
26
|
version: '0'
|
33
|
-
type: :development
|
34
|
-
prerelease: false
|
35
|
-
version_requirements: *2160138980
|
36
27
|
- !ruby/object:Gem::Dependency
|
37
|
-
name:
|
38
|
-
requirement:
|
39
|
-
none: false
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
40
30
|
requirements:
|
41
|
-
- -
|
31
|
+
- - ">="
|
42
32
|
- !ruby/object:Gem::Version
|
43
|
-
version: '0'
|
33
|
+
version: '3.0'
|
44
34
|
type: :development
|
45
35
|
prerelease: false
|
46
|
-
version_requirements:
|
47
|
-
|
48
|
-
|
49
|
-
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
41
|
+
description: Provide equality comparison methods for objects based on their attributes
|
42
|
+
by generating implementations for the ==, eql?, hash and inspect methods.
|
50
43
|
email:
|
51
|
-
-
|
44
|
+
- piotr@piotrmurach.com
|
52
45
|
executables: []
|
53
46
|
extensions: []
|
54
|
-
extra_rdoc_files:
|
47
|
+
extra_rdoc_files:
|
48
|
+
- README.md
|
49
|
+
- CHANGELOG.md
|
50
|
+
- LICENSE.txt
|
55
51
|
files:
|
56
|
-
- .
|
57
|
-
- .rspec
|
58
|
-
- .rvmrc
|
59
|
-
- .travis.yml
|
60
|
-
- Gemfile
|
52
|
+
- CHANGELOG.md
|
61
53
|
- LICENSE.txt
|
62
54
|
- README.md
|
63
|
-
- Rakefile
|
64
|
-
- equatable.gemspec
|
65
55
|
- lib/equatable.rb
|
66
56
|
- lib/equatable/version.rb
|
67
|
-
|
68
|
-
|
69
|
-
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
57
|
+
homepage: https://github.com/piotrmurach/equatable
|
58
|
+
licenses:
|
59
|
+
- MIT
|
60
|
+
metadata:
|
61
|
+
allowed_push_host: https://rubygems.org
|
62
|
+
bug_tracker_uri: https://github.com/piotrmurach/equatable/issues
|
63
|
+
changelog_uri: https://github.com/piotrmurach/equatable/blob/master/CHANGELOG.md
|
64
|
+
documentation_uri: https://www.rubydoc.info/github/piotrmurach/equatable
|
65
|
+
homepage_uri: https://github.com/piotrmurach/equatable
|
66
|
+
source_code_uri: https://github.com/piotrmurach/equatable
|
74
67
|
post_install_message:
|
75
68
|
rdoc_options: []
|
76
69
|
require_paths:
|
77
70
|
- lib
|
78
71
|
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
-
none: false
|
80
72
|
requirements:
|
81
|
-
- -
|
73
|
+
- - ">="
|
82
74
|
- !ruby/object:Gem::Version
|
83
|
-
version:
|
75
|
+
version: 2.0.0
|
84
76
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
-
none: false
|
86
77
|
requirements:
|
87
|
-
- -
|
78
|
+
- - ">="
|
88
79
|
- !ruby/object:Gem::Version
|
89
80
|
version: '0'
|
90
81
|
requirements: []
|
91
|
-
|
92
|
-
rubygems_version: 1.8.10
|
82
|
+
rubygems_version: 3.1.2
|
93
83
|
signing_key:
|
94
|
-
specification_version:
|
95
|
-
summary:
|
96
|
-
test_files:
|
97
|
-
- spec/equatable/eql_spec.rb
|
98
|
-
- spec/equatable/equal_spec.rb
|
99
|
-
- spec/equatable/include_spec.rb
|
100
|
-
- spec/equatable/subclass_spec.rb
|
101
|
-
- spec/spec_helper.rb
|
102
|
-
has_rdoc:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Provide equality comparison methods for objects based on their attributes.
|
86
|
+
test_files: []
|
data/.gitignore
DELETED
data/.rspec
DELETED
data/.rvmrc
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
#!/usr/bin/env bash
|
2
|
-
|
3
|
-
environment_id="ruby-1.9.3-p0@equatable"
|
4
|
-
|
5
|
-
#
|
6
|
-
# First we attempt to load the desired environment directly from the environment
|
7
|
-
# file. This is very fast and efficient compared to running through the entire
|
8
|
-
# CLI and selector. If you want feedback on which environment was used then
|
9
|
-
# insert the word 'use' after --create as this triggers verbose mode.
|
10
|
-
#
|
11
|
-
if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
|
12
|
-
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
|
13
|
-
then
|
14
|
-
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
|
15
|
-
|
16
|
-
if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
|
17
|
-
then
|
18
|
-
. "${rvm_path:-$HOME/.rvm}/hooks/after_use"
|
19
|
-
fi
|
20
|
-
else
|
21
|
-
# If the environment file has not yet been created, use the RVM CLI to select.
|
22
|
-
if ! rvm --create "$environment_id"
|
23
|
-
then
|
24
|
-
echo "Failed to create RVM environment '${environment_id}'."
|
25
|
-
return 1
|
26
|
-
fi
|
27
|
-
fi
|
28
|
-
|
29
|
-
if [[ $- == *i* ]] # check for interactive shells
|
30
|
-
then
|
31
|
-
echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
|
32
|
-
else
|
33
|
-
echo "Using: $GEM_HOME" # don't use colors in interactive shells
|
34
|
-
fi
|
data/.travis.yml
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
language: ruby
|
2
|
-
before_install:
|
3
|
-
- gem install bundler
|
4
|
-
rvm:
|
5
|
-
- 1.8.7
|
6
|
-
- 1.9.2
|
7
|
-
- 1.9.3
|
8
|
-
- ruby-head
|
9
|
-
- jruby-18mode
|
10
|
-
- jruby-19mode
|
11
|
-
- rbx-18mode
|
12
|
-
- rbx-19mode
|
13
|
-
- ree
|
14
|
-
- jruby-head
|
15
|
-
matrix:
|
16
|
-
allow_failures:
|
17
|
-
- rvm: ruby-head
|
18
|
-
- rvm: jruby-head
|
19
|
-
- rvm: jruby-19mode
|
20
|
-
- rvm: rbx-19mode
|
data/Gemfile
DELETED
data/Rakefile
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
$:.unshift File.expand_path('../lib', __FILE__)
|
3
|
-
require "bundler/gem_tasks"
|
4
|
-
require "rspec/core/rake_task"
|
5
|
-
|
6
|
-
RSpec::Core::RakeTask.new(:spec) do |spec|
|
7
|
-
spec.pattern = FileList['spec/**/*_spec.rb']
|
8
|
-
end
|
9
|
-
|
10
|
-
FileList['tasks/**/*.rake'].each { |task| import task }
|
11
|
-
|
12
|
-
task :default => [:spec]
|
data/equatable.gemspec
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'equatable/version'
|
5
|
-
|
6
|
-
Gem::Specification.new do |gem|
|
7
|
-
gem.name = "equatable"
|
8
|
-
gem.version = Equatable::VERSION
|
9
|
-
gem.authors = ["Piotr Murach"]
|
10
|
-
gem.email = [""]
|
11
|
-
gem.description = %q{Allows ruby objects to implement equality comparison and inspection methods. By including this module, a class indicates that its instances have explicit general contracts for `hash`, `==` and `eql?` methods.}
|
12
|
-
gem.summary = %q{Allows ruby objects to implement equality comparison and inspection methods.}
|
13
|
-
gem.homepage = "http://github.com/peter-murach/equatable"
|
14
|
-
|
15
|
-
gem.files = `git ls-files`.split($/)
|
16
|
-
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
-
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
-
gem.require_paths = ["lib"]
|
19
|
-
|
20
|
-
gem.add_development_dependency 'rspec'
|
21
|
-
gem.add_development_dependency 'rake'
|
22
|
-
gem.add_development_dependency 'yard'
|
23
|
-
end
|
data/spec/equatable/eql_spec.rb
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
describe Equatable, '#eql?' do
|
6
|
-
let(:name) { 'Value' }
|
7
|
-
let(:value) { 11 }
|
8
|
-
|
9
|
-
let(:klass) {
|
10
|
-
::Class.new do
|
11
|
-
include Equatable
|
12
|
-
|
13
|
-
attr_reader :value
|
14
|
-
|
15
|
-
def initialize(value)
|
16
|
-
@value = value
|
17
|
-
end
|
18
|
-
end
|
19
|
-
}
|
20
|
-
|
21
|
-
let(:object) { klass.new(value) }
|
22
|
-
|
23
|
-
subject { object.eql?(other) }
|
24
|
-
|
25
|
-
context 'with the same object' do
|
26
|
-
let(:other) { object }
|
27
|
-
|
28
|
-
it { should be_true }
|
29
|
-
|
30
|
-
it 'is symmetric' do
|
31
|
-
should eql(other.eql?(object))
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
context 'with an equivalent object' do
|
36
|
-
let(:other) { object.dup }
|
37
|
-
|
38
|
-
it { should be_true }
|
39
|
-
|
40
|
-
it 'is symmetric' do
|
41
|
-
should eql(other.eql?(object))
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
context 'with an equivalent object of a subclass' do
|
46
|
-
let(:other) { ::Class.new(klass).new(value) }
|
47
|
-
|
48
|
-
it { should be_false }
|
49
|
-
|
50
|
-
it 'is symmetric' do
|
51
|
-
should eql(other.eql?(object))
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
@@ -1,76 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
describe Equatable, '#==' do
|
6
|
-
let(:name) { 'Value' }
|
7
|
-
let(:value) { 11 }
|
8
|
-
|
9
|
-
let(:super_klass) {
|
10
|
-
::Class.new do
|
11
|
-
include Equatable
|
12
|
-
|
13
|
-
attr_reader :value
|
14
|
-
|
15
|
-
def initialize(value)
|
16
|
-
@value = value
|
17
|
-
end
|
18
|
-
end
|
19
|
-
}
|
20
|
-
|
21
|
-
let(:klass) { Class.new(super_klass) }
|
22
|
-
|
23
|
-
let(:object) { klass.new(value) }
|
24
|
-
|
25
|
-
subject { object == other }
|
26
|
-
|
27
|
-
context 'with the same object' do
|
28
|
-
let(:other) { object }
|
29
|
-
|
30
|
-
it { should be_true }
|
31
|
-
|
32
|
-
it 'is symmetric' do
|
33
|
-
should eql(other == object)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
context 'with an equivalent object' do
|
38
|
-
let(:other) { object.dup }
|
39
|
-
|
40
|
-
it { should be_true }
|
41
|
-
|
42
|
-
it 'is symmetric' do
|
43
|
-
should eql(other == object)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
context 'with an equivalent object of a subclass' do
|
48
|
-
let(:other) { ::Class.new(klass).new(value) }
|
49
|
-
|
50
|
-
it { should be_false }
|
51
|
-
|
52
|
-
it 'is not symmetric' do
|
53
|
-
should_not eql(other == object)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
context 'with an equivalent object of a superclass' do
|
58
|
-
let(:other) { super_klass.new(value) }
|
59
|
-
|
60
|
-
it { should be_true }
|
61
|
-
|
62
|
-
it 'is not symmetric' do
|
63
|
-
should_not eql(other == object)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
context 'with an object of another class' do
|
68
|
-
let(:other) { Class.new.new }
|
69
|
-
|
70
|
-
it { should be_false }
|
71
|
-
|
72
|
-
it 'is symmetric' do
|
73
|
-
should eql(other == object)
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
@@ -1,170 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
describe Equatable, '#include' do
|
6
|
-
let(:name) { 'Value' }
|
7
|
-
let(:object) { described_class }
|
8
|
-
|
9
|
-
context 'without attributes' do
|
10
|
-
let(:klass) { ::Class.new }
|
11
|
-
|
12
|
-
subject { klass.new }
|
13
|
-
|
14
|
-
before {
|
15
|
-
klass.stub(:name).and_return name
|
16
|
-
klass.send(:include, object)
|
17
|
-
}
|
18
|
-
|
19
|
-
it { should respond_to :compare? }
|
20
|
-
|
21
|
-
it { should be_instance_of klass }
|
22
|
-
|
23
|
-
describe '#inspect' do
|
24
|
-
it { subject.inspect.should eql('#<Value>') }
|
25
|
-
end
|
26
|
-
|
27
|
-
describe '#hash' do
|
28
|
-
it { subject.hash.should eql([klass].hash) }
|
29
|
-
end
|
30
|
-
|
31
|
-
describe '#eql?' do
|
32
|
-
context 'when objects are similar' do
|
33
|
-
let(:other) { subject.dup }
|
34
|
-
|
35
|
-
it { subject.eql?(other).should be_true }
|
36
|
-
end
|
37
|
-
|
38
|
-
context 'when objects are different' do
|
39
|
-
let(:other) { stub('other') }
|
40
|
-
|
41
|
-
it { subject.eql?(other).should be_false }
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
describe '#==' do
|
46
|
-
context 'when objects are similar' do
|
47
|
-
let(:other) { subject.dup }
|
48
|
-
|
49
|
-
it { (subject == other).should be_true }
|
50
|
-
end
|
51
|
-
|
52
|
-
context 'when objects are different' do
|
53
|
-
let(:other) { stub('other') }
|
54
|
-
|
55
|
-
it { (subject == other)}
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
context 'equivalence relation' do
|
60
|
-
let(:other) { subject.dup }
|
61
|
-
let(:another) { other.dup }
|
62
|
-
|
63
|
-
it 'is not equal to nil reference' do
|
64
|
-
expect(subject.eql?(nil)).to be_false
|
65
|
-
end
|
66
|
-
|
67
|
-
it 'is reflexive' do
|
68
|
-
expect(subject.eql?(subject)).to be_true
|
69
|
-
end
|
70
|
-
|
71
|
-
it 'is symmetric' do
|
72
|
-
expect(subject.eql?(other)).to eql( other.eql?(subject) )
|
73
|
-
end
|
74
|
-
|
75
|
-
it 'is transitive' do
|
76
|
-
expect(subject.eql?(other) && other.eql?(another)).to eql(subject.eql?(another))
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
context 'with attributes' do
|
82
|
-
let(:value) { 11 }
|
83
|
-
let(:klass) {
|
84
|
-
::Class.new do
|
85
|
-
include Equatable
|
86
|
-
|
87
|
-
attr_reader :value
|
88
|
-
|
89
|
-
def initialize(value)
|
90
|
-
@value = value
|
91
|
-
end
|
92
|
-
end
|
93
|
-
}
|
94
|
-
|
95
|
-
before {
|
96
|
-
klass.stub(:name).and_return name
|
97
|
-
}
|
98
|
-
|
99
|
-
subject { klass.new(value) }
|
100
|
-
|
101
|
-
it 'dynamically defines #hash method' do
|
102
|
-
klass.method_defined?(:hash).should be_true
|
103
|
-
end
|
104
|
-
|
105
|
-
it 'dynamically defines #inspect method' do
|
106
|
-
klass.method_defined?(:inspect).should be_true
|
107
|
-
end
|
108
|
-
|
109
|
-
it { should respond_to :compare? }
|
110
|
-
|
111
|
-
it { should respond_to :eql? }
|
112
|
-
|
113
|
-
describe '#eql?' do
|
114
|
-
context 'when objects are similar' do
|
115
|
-
let(:other) { subject.dup }
|
116
|
-
|
117
|
-
it { subject.eql?(other).should be_true }
|
118
|
-
end
|
119
|
-
|
120
|
-
context 'when objects are different' do
|
121
|
-
let(:other) { stub('other') }
|
122
|
-
|
123
|
-
it { subject.eql?(other).should be_false }
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
describe '#==' do
|
128
|
-
context 'when objects are similar' do
|
129
|
-
let(:other) { subject.dup }
|
130
|
-
|
131
|
-
it { (subject == other).should be_true }
|
132
|
-
end
|
133
|
-
|
134
|
-
context 'when objects are different' do
|
135
|
-
let(:other) { stub('other') }
|
136
|
-
|
137
|
-
it { (subject == other).should be_false }
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
describe '#inspect' do
|
142
|
-
it { subject.inspect.should eql('#<Value value=11>') }
|
143
|
-
end
|
144
|
-
|
145
|
-
describe '#hash' do
|
146
|
-
it { subject.hash.should eql( ([klass] + [value]).hash) }
|
147
|
-
end
|
148
|
-
|
149
|
-
context 'equivalence relation' do
|
150
|
-
let(:other) { subject.dup }
|
151
|
-
let(:another) { other.dup }
|
152
|
-
|
153
|
-
it 'is not equal to nil reference' do
|
154
|
-
expect(subject.eql?(nil)).to be_false
|
155
|
-
end
|
156
|
-
|
157
|
-
it 'is reflexive' do
|
158
|
-
expect(subject.eql?(subject)).to be_true
|
159
|
-
end
|
160
|
-
|
161
|
-
it 'is symmetric' do
|
162
|
-
expect(subject.eql?(other)).to eql( other.eql?(subject) )
|
163
|
-
end
|
164
|
-
|
165
|
-
it 'is transitive' do
|
166
|
-
expect(subject.eql?(other) && other.eql?(another)).to eql(subject.eql?(another))
|
167
|
-
end
|
168
|
-
end
|
169
|
-
end
|
170
|
-
end
|
@@ -1,66 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
describe Equatable, 'subclass' do
|
6
|
-
let(:name) { 'Value' }
|
7
|
-
|
8
|
-
context 'when subclass' do
|
9
|
-
let(:value) { 11 }
|
10
|
-
let(:klass) {
|
11
|
-
::Class.new do
|
12
|
-
include Equatable
|
13
|
-
|
14
|
-
attr_reader :value
|
15
|
-
|
16
|
-
def initialize(value)
|
17
|
-
@value = value
|
18
|
-
end
|
19
|
-
end
|
20
|
-
}
|
21
|
-
let(:subclass) { ::Class.new(klass) }
|
22
|
-
|
23
|
-
before {
|
24
|
-
klass.stub(:name).and_return name
|
25
|
-
}
|
26
|
-
|
27
|
-
subject { subclass.new(value) }
|
28
|
-
|
29
|
-
it { subclass.superclass.should == klass }
|
30
|
-
|
31
|
-
it { should respond_to :value }
|
32
|
-
|
33
|
-
describe '#inspect' do
|
34
|
-
it { subject.inspect.should eql('#<Value value=11>') }
|
35
|
-
end
|
36
|
-
|
37
|
-
describe '#eql?' do
|
38
|
-
context 'when objects are similar' do
|
39
|
-
let(:other) { subject.dup }
|
40
|
-
|
41
|
-
it { subject.eql?(other).should be_true }
|
42
|
-
end
|
43
|
-
|
44
|
-
context 'when objects are different' do
|
45
|
-
let(:other) { stub('other') }
|
46
|
-
|
47
|
-
it { subject.eql?(other).should be_false }
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
describe '#==' do
|
52
|
-
context 'when objects are similar' do
|
53
|
-
let(:other) { subject.dup }
|
54
|
-
|
55
|
-
it { (subject == other).should be_true }
|
56
|
-
end
|
57
|
-
|
58
|
-
context 'when objects are different' do
|
59
|
-
let(:other) { stub('other') }
|
60
|
-
|
61
|
-
it { (subject == other)}
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
end
|
data/spec/spec_helper.rb
DELETED