typeid 0.2.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 283674a725da5d9ffa40b5bbc5f0c2a55020eea487f58d6fa6662ac903e51fa7
4
- data.tar.gz: 673a27c6e7cd8c29fb0484564b7ae2eda168d49c386dcfc381fe22d4cdb3e3b5
3
+ metadata.gz: 1c2971eed0f4cb54b1f89e253e78e092e4a8fd9a5895a5f04e9ff9701bf289bc
4
+ data.tar.gz: 469d897f849ac01ecfa60adbaa6aada3f4ce7b29182b1e33fc967f35294b2a96
5
5
  SHA512:
6
- metadata.gz: a447c767d7dd80fe7b2c555941677fe163508a6ee0c090e20f03475d97399b0418e1184ab159999eebcd5f6e2d055f806d70274166cbae7ca3859f7a0242daf4
7
- data.tar.gz: '08fb41867b5af9809cf36635383947972ca7f16c07c7c6df265dff27180aebee3db26dd6ae423775dd45acc49f0e7f374025fec6feed3009780d1944dce32927'
6
+ metadata.gz: bf90dbde8bbcb004ca85c507bcc3d81922971eb9d31bc87d4ce2c2980fdf6be5548c69438b69bce4361eef1ef9ee65caef8b2e87370d0a13c45e201b758303ea
7
+ data.tar.gz: 516981aac6f786f452d28ada1b0e87fa17d31cf8eea1c07db10f15f9ebbd15f499cef4897321a6bd6a8920812b2f835c85f6a6df456239b6f19299f14b62f2b7
data/lib/typeid/uuid.rb CHANGED
@@ -53,11 +53,22 @@ class TypeID < String
53
53
  super(string)
54
54
  end
55
55
 
56
+ # Returns the +UUID+ encoded as a base32 +String+.
57
+ #
56
58
  # @return [String]
57
59
  def base32
58
60
  TypeID::UUID::Base32.encode(bytes)
59
61
  end
60
62
 
63
+ # Returns the timestamp of the +UUID+ as milliseconds since the Unix epoch.
64
+ #
65
+ # @return [Integer]
66
+ def timestamp
67
+ bytes[0..5]
68
+ .map.with_index { |byte, index| byte << (5 - index) * 8 }
69
+ .inject(:|)
70
+ end
71
+
61
72
  # @return [String]
62
73
  def inspect
63
74
  "#<#{self.class.name} #{to_s}>"
@@ -1,3 +1,3 @@
1
1
  class TypeID < String
2
- VERSION = "0.2.0".freeze
2
+ VERSION = "0.2.2".freeze
3
3
  end
data/lib/typeid.rb CHANGED
@@ -32,11 +32,11 @@ class TypeID < String
32
32
  # @param string [String] string representation of a +TypeID+
33
33
  # @return [TypeID]
34
34
  def self.from_string(string)
35
- case string.split("_")
36
- in [suffix]
35
+ case string.rpartition("_")
36
+ in ["", "", suffix]
37
37
  from("", suffix)
38
38
 
39
- in [prefix, suffix]
39
+ in [prefix, "_", suffix]
40
40
  raise Error, "prefix cannot be empty when there's a separator" if prefix.empty?
41
41
 
42
42
  from(prefix, suffix)
@@ -83,7 +83,8 @@ class TypeID < String
83
83
  suffix: TypeID::UUID.generate(timestamp: timestamp).base32
84
84
  )
85
85
  raise Error, "prefix length cannot be greater than #{MAX_PREFIX_LENGTH}" if prefix.length > MAX_PREFIX_LENGTH
86
- raise Error, "prefix must be lowercase ASCII characters" unless prefix.match?(/^[a-z]*$/)
86
+ raise Error, "prefix must be lowercase ASCII characters" unless prefix.match?(/^[a-z_]*$/)
87
+ raise Error, "prefix cannot start or end with an underscore" if prefix.start_with?("_") || prefix.end_with?("_")
87
88
  raise Error, "suffix must be #{TypeID::UUID::Base32::ENCODED_STRING_LENGTH} characters" unless suffix.length == TypeID::UUID::Base32::ENCODED_STRING_LENGTH
88
89
  raise Error, "suffix must only contain the letters in '#{TypeID::UUID::Base32::ALPHABET}'" unless suffix.chars.all? { |char| TypeID::UUID::Base32::ALPHABET.include?(char) }
89
90
  raise Error, "suffix must start with a 0-7 digit to avoid overflows" unless ("0".."7").cover?(suffix.chars.first)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typeid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Booth
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-08 00:00:00.000000000 Z
11
+ date: 2024-04-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: uuid7