druuid 1.0.1 → 1.0.2
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.
- checksums.yaml +4 -4
- data/druuid.rb +18 -4
- data/spec/druuid_spec.rb +29 -10
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6b3eb19e63eb6ef54ba0ba47e1196a6f2c15c830
|
4
|
+
data.tar.gz: 6de75af50919eac4bd7f818cd41ec37dbaa541f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4000d869db3357fb2c52ed77306479db6e41ce02bc3ef0a6c68358dae4822b246517f547c7dcbc651e32292d590594a8f59937d249692cd1a80a2382cf8f1807
|
7
|
+
data.tar.gz: 00e0b4894bcd655bc74fbc497a42e98eb6424417a1b594ca605ee041dfc4935f669f2e8abda529955117f7a24bef96697be19c6489b804c218235bb9ac10abd6
|
data/druuid.rb
CHANGED
@@ -4,6 +4,7 @@ require 'securerandom'
|
|
4
4
|
#
|
5
5
|
# Date-relative UUID generation.
|
6
6
|
module Druuid
|
7
|
+
NUM_RANDOM_BITS = 64 - 41
|
7
8
|
|
8
9
|
class << self
|
9
10
|
|
@@ -21,8 +22,8 @@ module Druuid
|
|
21
22
|
def gen time = Time.now, epoch_offset = epoch
|
22
23
|
ms = ((time.to_f - epoch_offset.to_i) * 1e3).round
|
23
24
|
rand = (SecureRandom.random_number * 1e16).round
|
24
|
-
id = ms <<
|
25
|
-
id | rand % (2 **
|
25
|
+
id = ms << NUM_RANDOM_BITS
|
26
|
+
id | rand % (2 ** NUM_RANDOM_BITS)
|
26
27
|
end
|
27
28
|
|
28
29
|
# Determines when a given UUID was generated.
|
@@ -34,8 +35,21 @@ module Druuid
|
|
34
35
|
# Druuid.time 11142943683383068069
|
35
36
|
# # => 2012-02-04 00:00:00 -0800
|
36
37
|
def time uuid, epoch_offset = epoch
|
37
|
-
ms = uuid >>
|
38
|
-
Time.at
|
38
|
+
ms = uuid >> NUM_RANDOM_BITS
|
39
|
+
Time.at(ms / 1e3) + epoch_offset.to_i
|
40
|
+
end
|
41
|
+
|
42
|
+
# Determines the minimum UUID that could be generated for the given time.
|
43
|
+
#
|
44
|
+
# @param [Time] time of UUID
|
45
|
+
# @param [Numeric] epoch offset
|
46
|
+
# @return [Bignum] UUID
|
47
|
+
# @example
|
48
|
+
# Druuid.min_for_time
|
49
|
+
# # => 11142943683379200000
|
50
|
+
def min_for_time time = Time.now, epoch_offset = epoch
|
51
|
+
ms = ((time.to_f - epoch_offset.to_i) * 1e3).round
|
52
|
+
ms << NUM_RANDOM_BITS
|
39
53
|
end
|
40
54
|
|
41
55
|
end
|
data/spec/druuid_spec.rb
CHANGED
@@ -1,37 +1,46 @@
|
|
1
1
|
require_relative '../druuid'
|
2
2
|
|
3
3
|
describe Druuid do
|
4
|
-
|
5
|
-
it 'generates a UUID' do
|
6
|
-
uuid = Druuid.gen
|
7
|
-
uuid.should be_instance_of Bignum
|
8
|
-
uuid.should_not eq Druuid.gen
|
9
|
-
end
|
10
|
-
|
4
|
+
shared_examples "druuid generator" do |method|
|
11
5
|
let(:datetime) { Time.utc 2012, 2, 4, 8 }
|
12
6
|
let(:prefix) { '111429436833' }
|
7
|
+
|
8
|
+
it "is a Bugnum" do
|
9
|
+
Druuid.send(method).should be_instance_of Bignum
|
10
|
+
end
|
11
|
+
|
13
12
|
context 'with a given time' do
|
14
13
|
it 'generates the UUID against the time' do
|
15
|
-
Druuid.
|
14
|
+
Druuid.send(method, datetime).to_s[0, 12].should eq prefix
|
16
15
|
end
|
17
16
|
end
|
18
17
|
|
19
18
|
let(:offset) { 60 * 60 * 24 }
|
20
19
|
context 'with a given epoch' do
|
21
20
|
it 'generates the UUID against the offset' do
|
22
|
-
Druuid.
|
21
|
+
Druuid.send(method, datetime + offset, offset).to_s[0, 12].should eq prefix
|
23
22
|
end
|
24
23
|
end
|
25
24
|
|
26
25
|
context 'with a default epoch' do
|
27
26
|
before { @old_epoch, Druuid.epoch = Druuid.epoch, offset }
|
28
27
|
it 'generates the UUID against the offset' do
|
29
|
-
Druuid.
|
28
|
+
Druuid.send(method, datetime + offset).to_s[0, 12].should eq prefix
|
30
29
|
end
|
31
30
|
after { Druuid.epoch = @old_epoch }
|
32
31
|
end
|
33
32
|
end
|
34
33
|
|
34
|
+
|
35
|
+
describe '.gen' do
|
36
|
+
it_behaves_like 'druuid generator', :gen
|
37
|
+
|
38
|
+
it 'generates a UUID' do
|
39
|
+
uuid = Druuid.gen
|
40
|
+
uuid.should_not eq Druuid.gen
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
35
44
|
describe '.time' do
|
36
45
|
let(:datetime) { Time.utc 2012, 2, 4, 8 }
|
37
46
|
let(:uuid) { 11142943683383068069 }
|
@@ -54,4 +63,14 @@ describe Druuid do
|
|
54
63
|
after { Druuid.epoch = @old_epoch }
|
55
64
|
end
|
56
65
|
end
|
66
|
+
|
67
|
+
describe '.min_for_time' do
|
68
|
+
it_behaves_like 'druuid generator', :min_for_time
|
69
|
+
|
70
|
+
let(:datetime) { Time.utc 2012, 2, 4, 8 }
|
71
|
+
it 'generates druuid without random bits' do
|
72
|
+
random_bits = Druuid.min_for_time(Time.now) & Integer("0b" + ("1" * Druuid::NUM_RANDOM_BITS))
|
73
|
+
random_bits.should be_zero
|
74
|
+
end
|
75
|
+
end
|
57
76
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: druuid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stephen Celis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-01-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -66,10 +66,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
66
66
|
version: '0'
|
67
67
|
requirements: []
|
68
68
|
rubyforge_project:
|
69
|
-
rubygems_version: 2.
|
69
|
+
rubygems_version: 2.5.1
|
70
70
|
signing_key:
|
71
71
|
specification_version: 4
|
72
72
|
summary: Date-relative UUID generation
|
73
73
|
test_files:
|
74
74
|
- spec/druuid_spec.rb
|
75
|
-
has_rdoc:
|