Skip to content

Commit ccf9a1d

Browse files
committed
Add a <=> method to compare CSUUIDs.
1 parent 9b56cca commit ccf9a1d

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

shard.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: csuuid
2-
version: 0.2.3
2+
version: 0.2.4
33

44
authors:
55
- Kirk Haines <[email protected]>

spec/csuuid_spec.cr

+8
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,12 @@ describe CSUUID do
9191
uuids = CSUUID.generate(10000)
9292
uuids.uniq.size.should eq uuids.size
9393
end
94+
95+
it "can compare CSUUIDs via <=>" do
96+
uuid1 = CSUUID.new
97+
uuid2 = CSUUID.new
98+
(uuid1 <=> uuid2).should eq -1
99+
(uuid2 <=> uuid1).should eq 1
100+
(uuid1 <=> uuid1).should eq 0
101+
end
94102
end

src/csuuid.cr

+13-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ require "crystal/spin_lock"
4040
# ```
4141
#
4242
struct CSUUID
43-
VERSION = "0.2.3"
43+
VERSION = "0.2.4"
4444

4545
@@mutex = Crystal::SpinLock.new
4646
# @@mutex = Mutex.new(protection: Mutex::Protection::Reentrant)
@@ -168,4 +168,16 @@ struct CSUUID
168168
hs = @bytes.hexstring
169169
io << "#{hs[0..7]}-#{hs[8..11]}-#{hs[12..15]}-#{hs[16..19]}-#{hs[20..31]}"
170170
end
171+
172+
def <=>(val)
173+
s, ns = seconds_and_nanoseconds
174+
s_val, ns_val = val.seconds_and_nanoseconds
175+
r = s <=> s_val
176+
return r unless r == 0
177+
178+
r = ns <=> ns_val
179+
return r unless r == 0
180+
181+
to_s <=> val.to_s
182+
end
171183
end

0 commit comments

Comments
 (0)