simple-identity 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +2 -0
- data/lib/simple-identity/version.rb +1 -1
- data/lib/simple-identity.rb +6 -2
- data/test/spec_simple-identity.rb +6 -1
- metadata +3 -3
data/README.md
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
simple-identity is a Ruby library for providing simple identity methods of #eql?, #==, and #hash.
|
4
4
|
|
5
|
+
[](http://badge.fury.io/rb/simple-identity) [](https://travis-ci.org/keita/simple-identity) [](https://coveralls.io/r/keita/simple-identity) [](https://codeclimate.com/github/keita/simple-identity)
|
6
|
+
|
5
7
|
## Installation
|
6
8
|
|
7
9
|
$ gem install simple-identity
|
data/lib/simple-identity.rb
CHANGED
@@ -3,13 +3,17 @@ require "simple-identity/version"
|
|
3
3
|
module SimpleIdentity
|
4
4
|
def eql?(other)
|
5
5
|
return false unless other.kind_of?(self.class)
|
6
|
-
|
6
|
+
__identities__.all? do |symbol|
|
7
7
|
instance_variable_get(symbol) == other.__send__(symbol.to_s[1..-1])
|
8
8
|
end
|
9
9
|
end
|
10
10
|
alias :"==" :eql?
|
11
11
|
|
12
12
|
def hash
|
13
|
-
|
13
|
+
__identities__.map{|symbol| instance_variable_get(symbol)}.hash
|
14
|
+
end
|
15
|
+
|
16
|
+
def __identities__
|
17
|
+
instance_variables.select{|id| not(id.to_s.start_with?("@__"))}
|
14
18
|
end
|
15
19
|
end
|
@@ -4,16 +4,19 @@ require 'simple-identity'
|
|
4
4
|
class A
|
5
5
|
include SimpleIdentity
|
6
6
|
attr_accessor :a, :b, :c
|
7
|
-
def initialize(a, b, c)
|
7
|
+
def initialize(a, b, c, d=nil)
|
8
8
|
@a = a
|
9
9
|
@b = b
|
10
10
|
@c = c
|
11
|
+
@__d__ = d
|
11
12
|
end
|
12
13
|
end
|
13
14
|
|
14
15
|
describe "SimpleIdentity" do
|
15
16
|
it 'should equal' do
|
16
17
|
A.new(1,2,3).should == A.new(1,2,3)
|
18
|
+
A.new(1,2,3,4).should == A.new(1,2,3)
|
19
|
+
A.new(1,2,3,4).should == A.new(1,2,3,5)
|
17
20
|
end
|
18
21
|
|
19
22
|
it 'should not equal' do
|
@@ -23,6 +26,8 @@ describe "SimpleIdentity" do
|
|
23
26
|
|
24
27
|
it 'should have same hash values' do
|
25
28
|
A.new(1,2,3).hash.should == A.new(1,2,3).hash
|
29
|
+
A.new(1,2,3,4).hash.should == A.new(1,2,3).hash
|
30
|
+
A.new(1,2,3,4).hash.should == A.new(1,2,3,5).hash
|
26
31
|
end
|
27
32
|
|
28
33
|
it 'should have different hash values' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple-identity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -93,7 +93,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
93
93
|
version: '0'
|
94
94
|
segments:
|
95
95
|
- 0
|
96
|
-
hash:
|
96
|
+
hash: 4579653586567278030
|
97
97
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
98
|
none: false
|
99
99
|
requirements:
|
@@ -102,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
102
102
|
version: '0'
|
103
103
|
segments:
|
104
104
|
- 0
|
105
|
-
hash:
|
105
|
+
hash: 4579653586567278030
|
106
106
|
requirements: []
|
107
107
|
rubyforge_project:
|
108
108
|
rubygems_version: 1.8.24
|