Skip to content

Commit 288ac24

Browse files
authored
Merge pull request #108 from fattymiller/uniq-equality
Array#uniq to correctly identify == GlobalIDs
2 parents a9c4e1e + 05fac3e commit 288ac24

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/global_id/global_id.rb

+5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ def model_class
6363
def ==(other)
6464
other.is_a?(GlobalID) && @uri == other.uri
6565
end
66+
alias_method :eql?, :==
67+
68+
def hash
69+
self.class.hash | @uri.hash
70+
end
6671

6772
def to_param
6873
# remove the = padding character for a prettier param -- it'll be added back in parse_encoded_gid

test/cases/global_id_test.rb

+24
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,30 @@ class GlobalIDCreationTest < ActiveSupport::TestCase
190190
person_gid = GlobalID.create(Person.new(5), app: nil)
191191
end
192192
end
193+
194+
test 'equality' do
195+
p1 = Person.new(5)
196+
p2 = Person.new(5)
197+
p3 = Person.new(10)
198+
assert_equal p1, p2
199+
assert_not_equal p2, p3
200+
201+
gid1 = GlobalID.create(p1)
202+
gid2 = GlobalID.create(p2)
203+
gid3 = GlobalID.create(p3)
204+
assert_equal gid1, gid2
205+
assert_not_equal gid2, gid3
206+
207+
# hash and eql? to match for two GlobalID's pointing to the same object
208+
assert_equal [gid1], [gid1, gid2].uniq
209+
assert_equal [gid1, gid3], [gid1, gid2, gid3].uniq
210+
211+
# verify that the GlobalID's hash is different to the underlaying URI
212+
assert_not_equal gid1.hash, gid1.uri.hash
213+
214+
# verify that URI and GlobalID do not pass the uniq test
215+
assert_equal [gid1, gid1.uri], [gid1, gid1.uri].uniq
216+
end
193217
end
194218

195219
class GlobalIDCustomParamsTest < ActiveSupport::TestCase

0 commit comments

Comments
 (0)