Class: UUID
- Inherits:
-
Object
- Object
- UUID
- Defined in:
- lib/uuid.rb
Constant Summary collapse
- VERSION =
'0.1.0'
- REGEX =
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
- MAX_INT =
2**128-1
- NIL =
Constants
UUID('00000000-0000-0000-0000-000000000000')
- FFF =
UUID('ffffffff-ffff-ffff-ffff-ffffffffffff')
Class Method Summary collapse
Instance Method Summary collapse
- #===(other) ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
- #hash ⇒ Object
-
#initialize(v) ⇒ UUID
constructor
A new instance of UUID.
- #inspect ⇒ Object
-
#sql_literal(ds) ⇒ Object
Sequel.
- #to_blob ⇒ Object
- #to_raw ⇒ Object (also: #to_binary)
- #to_s ⇒ Object (also: #to_str)
Constructor Details
#initialize(v) ⇒ UUID
Returns a new instance of UUID.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/uuid.rb', line 29 def initialize(v) case v when Integer v += MAX_INT + 1 if v < 0 raise "value to big (must fit in 128bit)" if v > MAX_INT v = [ "%032x" % [ v ] ].pack('H32') when String if v.size != 16 || v.encoding.name != 'ASCII-8BIT' raise ArgumentError, "need to be 16 characters ASCII-8BIT string (#{v})" end else raise ArgumentError, "expected 128-bit integer or 16-byte string" end @raw = v.dup.freeze end |
Class Method Details
.parse(v) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/uuid.rb', line 19 def self.parse(v) v = v.to_str s = v.size v = if s == 16 then v elsif s == 36 && REGEX === v then [v.delete('-')].pack('H32') else raise ArgumentError, "unable to parse UUID" end self.new(v) end |
Instance Method Details
#===(other) ⇒ Object
49 50 51 52 |
# File 'lib/uuid.rb', line 49 def ===(other) other = UUID(other) rescue nil !other.nil? && self.to_raw == other.to_raw end |
#eql?(other) ⇒ Boolean Also known as: ==
53 54 55 |
# File 'lib/uuid.rb', line 53 def eql?(other) !other.nil? && other.is_a?(UUID) && self.to_raw == other.to_raw end |
#hash ⇒ Object
46 47 48 |
# File 'lib/uuid.rb', line 46 def hash @raw.hash end |
#inspect ⇒ Object
60 |
# File 'lib/uuid.rb', line 60 def inspect ; "#<#{self.class}:#{to_s}>" ; end |
#sql_literal(ds) ⇒ Object
Sequel
65 |
# File 'lib/uuid.rb', line 65 def sql_literal(ds) ; '0x' + @raw.unpack('H32')[0] ; end |
#to_blob ⇒ Object
66 |
# File 'lib/uuid.rb', line 66 def to_blob ; Sequel.blob(@raw) ; end |
#to_raw ⇒ Object Also known as: to_binary
58 |
# File 'lib/uuid.rb', line 58 def to_raw ; @raw ; end |
#to_s ⇒ Object Also known as: to_str
59 |
# File 'lib/uuid.rb', line 59 def to_s ; @raw.unpack('H8H4H4H4H12').join('-') ; end |