Class: UUID
- Inherits:
-
Object
- Object
- UUID
- Defined in:
- lib/uuid.rb
Constant Summary collapse
- VERSION =
'0.3.2'
- RESERVED_NCS =
Reserved for NCS compatibility
0b000
- RFC_4122 =
Specified in RFC 4122
0b100
- RESERVED_MICROSOFT =
Reserved for Microsoft compatibility
0b110
- RESERVED_FUTURE =
Reserved for future definition
0b111
- Nil =
UUID.new(0)
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #bytes ⇒ Object
- #clock_seq ⇒ Object
- #clock_seq_and_reserved ⇒ Object
- #clock_seq_low ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
- #hex ⇒ Object
-
#initialize(value) ⇒ UUID
constructor
A new instance of UUID.
- #inspect ⇒ Object
- #nil_uuid? ⇒ Boolean
- #node ⇒ Object
- #time ⇒ Object
- #time_hi_and_version ⇒ Object
- #time_low ⇒ Object
- #time_mid ⇒ Object
- #to_i ⇒ Object
- #to_s ⇒ Object
- #urn ⇒ Object
- #variant ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(value) ⇒ UUID
Returns a new instance of UUID.
10 11 12 13 14 15 |
# File 'lib/uuid.rb', line 10 def initialize(value) @value = Integer(value) unless (@value >= 0) && (@value < (1 << 128)) raise RangeError, "#{value} (Integer value #{@value}) is out of range (need unsigned 128-bit value)" end end |
Class Method Details
.new_from_bytes(bytes) ⇒ Object
112 113 114 115 116 117 |
# File 'lib/uuid.rb', line 112 def self.new_from_bytes(bytes) unless bytes.length == 16 raise ArgumentError, "#{bytes} must have length of 16" end new bytes.unpack('C*').inject { |value, i| value * 256 | i } end |
.parse(uuid) ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/uuid.rb', line 119 def self.parse(uuid) str = uuid.to_s unless str =~ /^(urn:uuid:)?[0-9a-fA-F]{8}((-)?[0-9a-fA-F]{4}){3}(-)?[0-9a-fA-F]{12}$/ raise ArgumentError, "#{str} is not a recognized UUID representation" end new_from_bytes (str.gsub(/(^urn:uuid:|-)/, '').downcase.unpack('a2' * 16).collect { |x| x.to_i(16) }.pack('C*')) end |
.uuid4 ⇒ Object
127 128 129 130 131 132 |
# File 'lib/uuid.rb', line 127 def self.uuid4 bytes = SecureRandom.random_bytes(16) bytes[6] = (bytes[6] & 0x0f) | 0x40 bytes[8] = (bytes[8] & 0x3f) | 0x80 new_from_bytes bytes end |
Instance Method Details
#==(other) ⇒ Object
17 18 19 |
# File 'lib/uuid.rb', line 17 def ==(other) eql?(other) end |
#bytes ⇒ Object
21 22 23 24 25 |
# File 'lib/uuid.rb', line 21 def bytes bs = '' (0..120).step(8) { |shift| bs << ((@value >> (120 - shift)) & 0xff) } bs end |
#clock_seq ⇒ Object
27 28 29 |
# File 'lib/uuid.rb', line 27 def clock_seq ((clock_seq_and_reserved & 0x3f) << 8) | clock_seq_low end |
#clock_seq_and_reserved ⇒ Object
31 32 33 |
# File 'lib/uuid.rb', line 31 def clock_seq_and_reserved (to_i >> 56) & 0xff end |
#clock_seq_low ⇒ Object
35 36 37 |
# File 'lib/uuid.rb', line 35 def clock_seq_low (to_i >> 48) & 0xff end |
#eql?(other) ⇒ Boolean
39 40 41 |
# File 'lib/uuid.rb', line 39 def eql?(other) other.is_a?(self.class) && (other.to_i == to_i) end |
#hash ⇒ Object
43 44 45 |
# File 'lib/uuid.rb', line 43 def hash to_i.hash end |
#hex ⇒ Object
47 48 49 |
# File 'lib/uuid.rb', line 47 def hex '%032x' % to_i end |
#inspect ⇒ Object
51 52 53 |
# File 'lib/uuid.rb', line 51 def inspect to_s end |
#nil_uuid? ⇒ Boolean
55 56 57 |
# File 'lib/uuid.rb', line 55 def nil_uuid? to_i == 0 end |
#node ⇒ Object
59 60 61 |
# File 'lib/uuid.rb', line 59 def node to_i & 0xffffffffffff end |
#time ⇒ Object
63 64 65 |
# File 'lib/uuid.rb', line 63 def time ((time_hi_and_version & 0x0fff) << 48) | (time_mid << 32) | time_low end |
#time_hi_and_version ⇒ Object
67 68 69 |
# File 'lib/uuid.rb', line 67 def time_hi_and_version (to_i >> 64) & 0xffff end |
#time_low ⇒ Object
71 72 73 |
# File 'lib/uuid.rb', line 71 def time_low (to_i >> 96) end |
#time_mid ⇒ Object
75 76 77 |
# File 'lib/uuid.rb', line 75 def time_mid (to_i >> 80) & 0xffff end |
#to_i ⇒ Object
79 80 81 |
# File 'lib/uuid.rb', line 79 def to_i @value end |
#to_s ⇒ Object
83 84 85 86 |
# File 'lib/uuid.rb', line 83 def to_s h = hex '%s-%s-%s-%s-%s' % [h[0, 8], h[8, 4], h[12, 4], h[16, 4], h[20, 12]] end |
#urn ⇒ Object
88 89 90 |
# File 'lib/uuid.rb', line 88 def urn "urn:uuid:#{self}" end |
#variant ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/uuid.rb', line 97 def variant raw_variant = (clock_seq_and_reserved >> 5) if (raw_variant >> 2) == 0 0x000 elsif (raw_variant >> 1) == 2 0x100 else raw_variant end >> 6 end |
#version ⇒ Object
108 109 110 |
# File 'lib/uuid.rb', line 108 def version (variant == RFC_4122) ? ((to_i >> 76) & 0xf) : nil end |