easy-uuid 0.1.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 (2) hide show
  1. data/lib/uuid.rb +71 -0
  2. metadata +52 -0
data/lib/uuid.rb ADDED
@@ -0,0 +1,71 @@
1
+ module Kernel
2
+ def UUID(uuid)
3
+ if uuid.nil? then nil
4
+ elsif uuid.is_a?(UUID) then uuid
5
+ 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
+ end
10
+ end
11
+ module_function :UUID
12
+ end
13
+
14
+ 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)
27
+ end
28
+
29
+ def initialize(v)
30
+ case v
31
+ 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')
35
+ when String
36
+ if v.size != 16 || v.encoding.name != 'ASCII-8BIT'
37
+ raise ArgumentError,
38
+ "need to be 16 characters ASCII-8BIT string (#{v})"
39
+ end
40
+ else
41
+ raise ArgumentError, "expected 128-bit integer or 16-byte string"
42
+ end
43
+ @raw = v.dup.freeze
44
+ end
45
+
46
+ def hash
47
+ @raw.hash
48
+ end
49
+ def ===(other)
50
+ other = UUID(other) rescue nil
51
+ !other.nil? && self.to_raw == other.to_raw
52
+ end
53
+ def eql?(other)
54
+ !other.nil? && other.is_a?(UUID) && self.to_raw == other.to_raw
55
+ end
56
+ alias_method :==, :eql?
57
+
58
+ 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
62
+ alias_method :to_str, :to_s
63
+
64
+ # Sequel
65
+ def sql_literal(ds) ; '0x' + @raw.unpack('H32')[0] ; end
66
+ def to_blob ; Sequel.blob(@raw) ; end
67
+
68
+ # Constants
69
+ NIL = UUID('00000000-0000-0000-0000-000000000000')
70
+ FFF = UUID('ffffffff-ffff-ffff-ffff-ffffffffffff')
71
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy-uuid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Stephane D'Alu
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-02-05 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: UUID library for Ruby
15
+ email:
16
+ - sdalu@sdalu.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/uuid.rb
22
+ homepage: http://github.com/sdalu/easy-uuid
23
+ licenses: []
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ segments:
35
+ - 0
36
+ hash: 1872846144021895192
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ segments:
44
+ - 0
45
+ hash: 1872846144021895192
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 1.8.29
49
+ signing_key:
50
+ specification_version: 3
51
+ summary: UUID library for Ruby
52
+ test_files: []