easy-uuid 0.1.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/uuid.rb +151 -28
  3. metadata +42 -22
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8b199e86c6f8ebe25bbee9619c8ae6bc55c2ee1fb60ed39f7094663c3146e1b2
4
+ data.tar.gz: 779053987990cba37a7680cc1ca551049ccae9429e400e77cb01e17b39f3f43a
5
+ SHA512:
6
+ metadata.gz: 48d208c940f5e1569adfbaa4f1fe9a609bbfe68d661bcc67202c21a0484445cf0b4b3ced0da704bfe90eb65a840a7c347488f3f28d0c910a5665c8ebe60de7b3
7
+ data.tar.gz: 9b6fa9a013391e18878c4f26e3c032be4d7f3f3bea7f4d3dc1c70a69f05091abe2b7d0164a28ced6950cb430eccedcf037d1df92a6ee7ee97e83b83358457150
data/lib/uuid.rb CHANGED
@@ -1,48 +1,103 @@
1
+ require 'securerandom'
2
+ require 'digest/md5'
3
+ require 'digest/sha1'
4
+
1
5
  module Kernel
6
+ # Cast to UUID
7
+ #
8
+ # @raise [TypeError]
2
9
  def UUID(uuid)
3
- if uuid.nil? then nil
10
+ if uuid.nil? then UUID::NIL
4
11
  elsif uuid.is_a?(UUID) then uuid
5
12
  elsif uuid.is_a?(Integer) then UUID.new(uuid)
6
- elsif uuid = String.try_convert(uuid) then UUID.parse(uuid)
7
- else raise ArgumentError,
8
- "bad argument (expected UUID object, string or integer)"
13
+ elsif uuid = String.try_convert(uuid) then
14
+ uuid.size == 16 ? UUID.new(uuid) : UUID.parse(uuid)
15
+ else raise TypeError, "can't convert #{uuid.class} into UUID"
9
16
  end
10
17
  end
11
18
  module_function :UUID
12
19
  end
13
20
 
14
21
  class UUID
15
- VERSION = '0.1.0'
16
-
17
- REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
18
- MAX_INT = 2**128-1
19
- def self.parse(v)
20
- v = v.to_str
21
- s = v.size
22
- v = if s == 16 then v
23
- elsif s == 36 && REGEX === v then [v.delete('-')].pack('H32')
24
- else raise ArgumentError, "unable to parse UUID"
25
- end
26
- self.new(v)
22
+ # Version
23
+ VERSION = '0.3.0'
24
+
25
+ # Basic regex for validating UUID formatted string
26
+ REGEX = /\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/
27
+
28
+
29
+ # Parse a UUID formatted string
30
+ #
31
+ # @param str [String] string to parse
32
+ #
33
+ # @raise [ArgumentError] the string is not parsable as a UUID
34
+ #
35
+ # @return [UUID]
36
+ #
37
+ def self.parse(str)
38
+ unless str =~ REGEX
39
+ raise ArgumentError, "unable to parse UUID value"
40
+ end
41
+
42
+ self.new( [str.delete('-')].pack('H32') )
27
43
  end
28
44
 
45
+ def self.create_v3(namespace, name)
46
+ self.create_v3_v5(Digest::MD5, '3', namespace, name)
47
+ end
48
+
49
+ def self.generate_v4
50
+ UUID(SecureRandom.uuid)
51
+ end
52
+
53
+ def self.create_v5(namespace, name)
54
+ self.create_v3_v5(Digest::SHA1, '5', namespace, name)
55
+ end
56
+
57
+
58
+ # Create UUID
59
+ #
60
+ # @param [String,Integer] UUID value
61
+ #
62
+ # @raise [ArgumentError] given integer is too big or
63
+ # given string is not 16-byte 8bit
64
+ #
29
65
  def initialize(v)
30
66
  case v
31
67
  when Integer
32
- v += MAX_INT + 1 if v < 0
33
- raise "value to big (must fit in 128bit)" if v > MAX_INT
34
- v = [ "%032x" % [ v ] ].pack('H32')
68
+ hex = "%032x" % [ v ]
69
+ if hex.size > 32
70
+ raise ArgumentError,
71
+ "integer to big (must fit in 128-bit)"
72
+ end
73
+ @raw = [ hex ].pack('H32')
35
74
  when String
36
75
  if v.size != 16 || v.encoding.name != 'ASCII-8BIT'
37
76
  raise ArgumentError,
38
- "need to be 16 characters ASCII-8BIT string (#{v})"
77
+ "need to be a 16 byte ASCII-8BIT string (#{v})"
39
78
  end
79
+ @raw = v.dup
40
80
  else
41
- raise ArgumentError, "expected 128-bit integer or 16-byte string"
81
+ raise ArgumentError,
82
+ "expected 128-bit integer or 16-byte string"
42
83
  end
43
- @raw = v.dup.freeze
84
+ @raw.freeze
44
85
  end
45
86
 
87
+
88
+ # Return the 16-byte sequence forming the UUID.
89
+ #
90
+ # @return [Array<Integer>]
91
+ #
92
+ def bytes
93
+ @raw.bytes
94
+ end
95
+
96
+
97
+ # Generates an integer hash value
98
+ #
99
+ # @return [Integer] hash value
100
+ #
46
101
  def hash
47
102
  @raw.hash
48
103
  end
@@ -56,16 +111,84 @@ class UUID
56
111
  alias_method :==, :eql?
57
112
 
58
113
  def to_raw ; @raw ; end
59
- def to_s ; @raw.unpack('H8H4H4H4H12').join('-') ; end
60
- def inspect ; "#<#{self.class}:#{to_s}>" ; end
61
- alias_method :to_binary, :to_raw
114
+
115
+ # Returns a string containing a human-readable representation
116
+ # of this object
117
+ #
118
+ # @return [String]
119
+ #
120
+ def inspect
121
+ "#<#{self.class}:#{to_s}>"
122
+ end
123
+
124
+
125
+ # Get the UUID value as an integer
126
+ #
127
+ # @return [Integer] UUID value
128
+ #
129
+ def to_i
130
+ i, j = @raw.unpack('Q>Q>')
131
+ i * 2**64 + j
132
+ end
133
+ alias :to_int :to_i
134
+
135
+
136
+ # Get the UUID string representation
137
+ #
138
+ # @return [String] UUID string representation
139
+ #
140
+ def to_s
141
+ @raw.unpack('H8H4H4H4H12').join('-')
142
+ end
62
143
  alias_method :to_str, :to_s
63
144
 
145
+
146
+ # Get the URI has a URN in UUID namespace
147
+ #
148
+ # @return [String] Uniform Resource Name
149
+ #
150
+ def to_uri
151
+ "urn:uuid:#{self}"
152
+ end
153
+
154
+
155
+ # Get the JSON represenation
156
+ #
157
+ # @return [String]
158
+ #
159
+ def to_json(*a)
160
+ self.to_s.to_json(*a)
161
+ end
162
+
163
+
64
164
  # Sequel
65
165
  def sql_literal(ds) ; '0x' + @raw.unpack('H32')[0] ; end
66
166
  def to_blob ; Sequel.blob(@raw) ; end
67
167
 
68
- # Constants
69
- NIL = UUID('00000000-0000-0000-0000-000000000000')
70
- FFF = UUID('ffffffff-ffff-ffff-ffff-ffffffffffff')
168
+
169
+ # Nil/Empty UUID
170
+ NIL = UUID('00000000-0000-0000-0000-000000000000')
171
+
172
+ # UUID with all bit set.
173
+ # @note It is not a valid UUID but can be usefull
174
+ FFF = UUID('ffffffff-ffff-ffff-ffff-ffffffffffff')
175
+
176
+ # Known namespace for version 3 and 5
177
+ DNS = UUID('6ba7b810-9dad-11d1-80b4-00c04fd430c8')
178
+ URL = UUID('6ba7b811-9dad-11d1-80b4-00c04fd430c8')
179
+ OID = UUID('6ba7b812-9dad-11d1-80b4-00c04fd430c8')
180
+ X500 = UUID('6ba7b814-9dad-11d1-80b4-00c04fd430c8')
181
+
182
+
183
+ def self.create_v3_v5(h, v, namespace, name)
184
+ namespace_str = namespace.to_raw
185
+ name_str = name.to_s
186
+ digest = h.hexdigest(namespace_str + name_str)
187
+ UUID([digest.tap {|s| s[12] = v }].pack('H32'))
188
+ end
189
+
190
+
191
+ private_class_method :create_v3_v5
192
+
193
+
71
194
  end
metadata CHANGED
@@ -1,16 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy-uuid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
5
- prerelease:
4
+ version: 0.3.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Stephane D'Alu
9
- autorequire:
8
+ autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-02-05 00:00:00.000000000 Z
13
- dependencies: []
11
+ date: 2023-04-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: yard
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13'
14
41
  description: UUID library for Ruby
15
42
  email:
16
43
  - sdalu@sdalu.com
@@ -19,34 +46,27 @@ extensions: []
19
46
  extra_rdoc_files: []
20
47
  files:
21
48
  - lib/uuid.rb
22
- homepage: http://github.com/sdalu/easy-uuid
23
- licenses: []
24
- post_install_message:
49
+ homepage: http://gitlab.com/sdalu/easy-uuid
50
+ licenses:
51
+ - MIT
52
+ metadata: {}
53
+ post_install_message:
25
54
  rdoc_options: []
26
55
  require_paths:
27
56
  - lib
28
57
  required_ruby_version: !ruby/object:Gem::Requirement
29
- none: false
30
58
  requirements:
31
- - - '>='
59
+ - - ">="
32
60
  - !ruby/object:Gem::Version
33
61
  version: '0'
34
- segments:
35
- - 0
36
- hash: 1872846144021895192
37
62
  required_rubygems_version: !ruby/object:Gem::Requirement
38
- none: false
39
63
  requirements:
40
- - - '>='
64
+ - - ">="
41
65
  - !ruby/object:Gem::Version
42
66
  version: '0'
43
- segments:
44
- - 0
45
- hash: 1872846144021895192
46
67
  requirements: []
47
- rubyforge_project:
48
- rubygems_version: 1.8.29
49
- signing_key:
50
- specification_version: 3
68
+ rubygems_version: 3.4.5
69
+ signing_key:
70
+ specification_version: 4
51
71
  summary: UUID library for Ruby
52
72
  test_files: []