easy-uuid 0.1.0 → 0.2.0

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/uuid.rb +113 -26
  3. metadata +42 -22
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ad07d1208eb16cd745b048adb55ca02f86430e88f513f7e188d77a4a30d3fc10
4
+ data.tar.gz: cad79f0eea9bdf41194365c4935cf49c946395d754632335bee9fd6a32e7561b
5
+ SHA512:
6
+ metadata.gz: bbbe31f97f6e193c3ee8c35b73dbf6ff433d77c1a801320dc9a6e8fa9fe11a71eaf998f4854fe69274955c9142456f336ce3439a4b9ddfd69200f82bd9c05bd5
7
+ data.tar.gz: 41681b7c64d41c1f4e0b18ef0b26467be123df2ff528e57bc229c8b4bdf04525b1c1742f05c561d6009ae00bf93f0acc9a101ab7bf33e4cadb5f3fbc0f29826f
data/lib/uuid.rb CHANGED
@@ -1,48 +1,86 @@
1
1
  module Kernel
2
+ # Cast to UUID
3
+ #
4
+ # @raise [TypeError]
2
5
  def UUID(uuid)
3
- if uuid.nil? then nil
6
+ if uuid.nil? then UUID::NIL
4
7
  elsif uuid.is_a?(UUID) then uuid
5
8
  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)"
9
+ elsif uuid = String.try_convert(uuid) then
10
+ uuid.size == 16 ? UUID.new(uuid) : UUID.parse(uuid)
11
+ else raise TypeError, "can't convert #{uuid.class} into UUID"
9
12
  end
10
13
  end
11
14
  module_function :UUID
12
15
  end
13
16
 
14
17
  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)
18
+ # Version
19
+ VERSION = '0.2.0'
20
+
21
+ # Basic regex for validating UUID formatted string
22
+ REGEX = /\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/
23
+
24
+
25
+ # Parse a UUID formatted string
26
+ #
27
+ # @param str [String] string to parse
28
+ #
29
+ # @raise [ArgumentError] the string is not parsable as a UUID
30
+ #
31
+ # @return [UUID]
32
+ #
33
+ def self.parse(str)
34
+ unless str =~ REGEX
35
+ raise ArgumentError, "unable to parse UUID value"
36
+ end
37
+
38
+ self.new( [str.delete('-')].pack('H32') )
27
39
  end
28
40
 
41
+
42
+ # Create UUID
43
+ #
44
+ # @param [String,Integer] UUID value
45
+ #
46
+ # @raise [ArgumentError] given integer is too big or
47
+ # given string is not 16-byte 8bit
48
+ #
29
49
  def initialize(v)
30
50
  case v
31
51
  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')
52
+ hex = "%032x" % [ v ]
53
+ if hex.size > 32
54
+ raise ArgumentError,
55
+ "integer to big (must fit in 128-bit)"
56
+ end
57
+ @raw = [ hex ].pack('H32')
35
58
  when String
36
59
  if v.size != 16 || v.encoding.name != 'ASCII-8BIT'
37
60
  raise ArgumentError,
38
- "need to be 16 characters ASCII-8BIT string (#{v})"
61
+ "need to be a 16 byte ASCII-8BIT string (#{v})"
39
62
  end
63
+ @raw = v.dup.freeze
40
64
  else
41
- raise ArgumentError, "expected 128-bit integer or 16-byte string"
65
+ raise ArgumentError,
66
+ "expected 128-bit integer or 16-byte string"
42
67
  end
43
- @raw = v.dup.freeze
44
68
  end
45
69
 
70
+
71
+ # Return the 16-byte sequence forming the UUID.
72
+ #
73
+ # @return [Array<Integer>]
74
+ #
75
+ def bytes
76
+ @raw.bytes
77
+ end
78
+
79
+
80
+ # Generates an integer hash value
81
+ #
82
+ # @return [Integer] hash value
83
+ #
46
84
  def hash
47
85
  @raw.hash
48
86
  end
@@ -56,16 +94,65 @@ class UUID
56
94
  alias_method :==, :eql?
57
95
 
58
96
  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
97
+
98
+ # Returns a string containing a human-readable representation
99
+ # of this object
100
+ #
101
+ # @return [String]
102
+ #
103
+ def inspect
104
+ "#<#{self.class}:#{to_s}>"
105
+ end
106
+
107
+
108
+ # Get the UUID value as an integer
109
+ #
110
+ # @return [Integer] UUID value
111
+ #
112
+ def to_i
113
+ i, j = @raw.unpack('Q>Q>')
114
+ i * 2**64 + j
115
+ end
116
+ alias :to_int :to_i
117
+
118
+
119
+ # Get the UUID string representation
120
+ #
121
+ # @return [String] UUID string representation
122
+ #
123
+ def to_s
124
+ @raw.unpack('H8H4H4H4H12').join('-')
125
+ end
62
126
  alias_method :to_str, :to_s
63
127
 
128
+
129
+ # Get the URI has a URN in UUID namespace
130
+ #
131
+ # @return [String] Uniform Resource Name
132
+ #
133
+ def to_uri
134
+ "urn:uuid:#{self}"
135
+ end
136
+
137
+
138
+ # Get the JSON represenation
139
+ #
140
+ # @return [String]
141
+ #
142
+ def to_json(*a)
143
+ self.to_s.to_json(*a)
144
+ end
145
+
146
+
64
147
  # Sequel
65
148
  def sql_literal(ds) ; '0x' + @raw.unpack('H32')[0] ; end
66
149
  def to_blob ; Sequel.blob(@raw) ; end
67
150
 
68
- # Constants
151
+
152
+ # Nil/Empty UUID
69
153
  NIL = UUID('00000000-0000-0000-0000-000000000000')
154
+
155
+ # UUID with all bit set.
156
+ # @note It is not a valid UUID but can be usefull
70
157
  FFF = UUID('ffffffff-ffff-ffff-ffff-ffffffffffff')
71
158
  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.2.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: 2021-05-21 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.0.8
69
+ signing_key:
70
+ specification_version: 4
51
71
  summary: UUID library for Ruby
52
72
  test_files: []