Skip to content

Commit cabc95a

Browse files
committed
Update the shard.yml to be unopinionated about crystal version, and adjusted the default UUID creation slightly to provide a random identifier if none is given.
1 parent ab99980 commit cabc95a

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ uuid = CSUUID.new
4848
4949
uuid = CSUUID.new(seconds: 9223372036, nanoseconds: 729262400)
5050
51+
uuid = CSUUID.new(identifier: Random.new.random_bytes(6))
52+
5153
dt = ParseDate.parse("2020/07/29 09:15:37")
5254
uuid = CSUUID.new(dt)
5355
```

shard.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name: csuuid
2-
version: 0.1.1
2+
version: 0.1.2
33

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

7-
crystal: 1.0.0
7+
crystal: "*"
88

99
license: MIT
1010

spec/csuuid_spec.cr

+19
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ describe CSUUID do
4545
uuid.to_s.should match /-79f659ccb685/
4646
end
4747

48+
it "generates a UUID with a random identifier" do
49+
random_bytes = Random.new.random_bytes(6)
50+
uuid = CSUUID.new(identifier: random_bytes)
51+
uuid.to_s.should match /-#{random_bytes.hexstring}/
52+
end
53+
54+
it "generates a UUID with the current time, but a random identifier, if initialized with no input" do
55+
uuid1 = CSUUID.new
56+
sleep 1.1
57+
uuid2 = CSUUID.new
58+
59+
match_1 = CHECKUUID.match(uuid1.to_s)
60+
match_1.should_not be_nil
61+
match_2 = CHECKUUID.match(uuid2.to_s)
62+
match_2.should_not be_nil
63+
match_1.to_s.should_not eq match_2.to_s
64+
(!match_1.nil?) && (!match_2.nil?) && match_1[5].should_not eq match_2[5]
65+
end
66+
4867
it "accurately returns the seconds and nanoseconds encoded within the UUID" do
4968
uuid = CSUUID.new(seconds: 9223372036, nanoseconds: 729262400)
5069
uuid.seconds_and_nanoseconds.should eq({9223372036, 729262400})

src/csuuid.cr

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ struct CSUUID
7474
end
7575

7676
def initialize(identifier : Slice(UInt8) | String | Nil = nil)
77+
identifier ||= @@prng.random_bytes(6)
7778
t = Time.local
7879
initialize_impl(t.internal_seconds, t.internal_nanoseconds, identifier)
7980
end

0 commit comments

Comments
 (0)