Class: UUID
- Inherits:
-
Object
- Object
- UUID
- Defined in:
- lib/uuid.rb
Constant Summary collapse
- VERSION =
Version
'0.2.0'
- REGEX =
Basic regex for validating UUID formatted string
/\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/
- NIL =
Nil/Empty UUID
UUID('00000000-0000-0000-0000-000000000000')
- FFF =
Note:
It is not a valid UUID but can be usefull
UUID with all bit set.
UUID('ffffffff-ffff-ffff-ffff-ffffffffffff')
Class Method Summary collapse
-
.parse(str) ⇒ UUID
Parse a UUID formatted string.
Instance Method Summary collapse
- #===(other) ⇒ Object
-
#bytes ⇒ Array<Integer>
Return the 16-byte sequence forming the UUID.
- #eql?(other) ⇒ Boolean (also: #==)
-
#hash ⇒ Integer
Generates an integer hash value.
-
#initialize(v) ⇒ UUID
constructor
Create UUID.
-
#inspect ⇒ String
Returns a string containing a human-readable representation of this object.
-
#sql_literal(ds) ⇒ Object
Sequel.
- #to_blob ⇒ Object
-
#to_i ⇒ Integer
(also: #to_int)
Get the UUID value as an integer.
-
#to_json(*a) ⇒ String
Get the JSON represenation.
- #to_raw ⇒ Object
-
#to_s ⇒ String
(also: #to_str)
Get the UUID string representation.
-
#to_uri ⇒ String
Get the URI has a URN in UUID namespace.
Constructor Details
#initialize(v) ⇒ UUID
Create UUID
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/uuid.rb', line 49 def initialize(v) case v when Integer hex = "%032x" % [ v ] if hex.size > 32 raise ArgumentError, "integer to big (must fit in 128-bit)" end @raw = [ hex ].pack('H32') when String if v.size != 16 || v.encoding.name != 'ASCII-8BIT' raise ArgumentError, "need to be a 16 byte ASCII-8BIT string (#{v})" end @raw = v.dup.freeze else raise ArgumentError, "expected 128-bit integer or 16-byte string" end end |
Class Method Details
Instance Method Details
#===(other) ⇒ Object
87 88 89 90 |
# File 'lib/uuid.rb', line 87 def ===(other) other = UUID(other) rescue nil !other.nil? && self.to_raw == other.to_raw end |
#bytes ⇒ Array<Integer>
Return the 16-byte sequence forming the UUID.
75 76 77 |
# File 'lib/uuid.rb', line 75 def bytes @raw.bytes end |
#eql?(other) ⇒ Boolean Also known as: ==
91 92 93 |
# File 'lib/uuid.rb', line 91 def eql?(other) !other.nil? && other.is_a?(UUID) && self.to_raw == other.to_raw end |
#hash ⇒ Integer
Generates an integer hash value
84 85 86 |
# File 'lib/uuid.rb', line 84 def hash @raw.hash end |
#inspect ⇒ String
Returns a string containing a human-readable representation of this object
103 104 105 |
# File 'lib/uuid.rb', line 103 def inspect "#<#{self.class}:#{to_s}>" end |
#sql_literal(ds) ⇒ Object
Sequel
148 |
# File 'lib/uuid.rb', line 148 def sql_literal(ds) ; '0x' + @raw.unpack('H32')[0] ; end |
#to_blob ⇒ Object
149 |
# File 'lib/uuid.rb', line 149 def to_blob ; Sequel.blob(@raw) ; end |
#to_i ⇒ Integer Also known as: to_int
Get the UUID value as an integer
112 113 114 115 |
# File 'lib/uuid.rb', line 112 def to_i i, j = @raw.unpack('Q>Q>') i * 2**64 + j end |
#to_json(*a) ⇒ String
Get the JSON represenation
142 143 144 |
# File 'lib/uuid.rb', line 142 def to_json(*a) self.to_s.to_json(*a) end |
#to_raw ⇒ Object
96 |
# File 'lib/uuid.rb', line 96 def to_raw ; @raw ; end |
#to_s ⇒ String Also known as: to_str
Get the UUID string representation
123 124 125 |
# File 'lib/uuid.rb', line 123 def to_s @raw.unpack('H8H4H4H4H12').join('-') end |
#to_uri ⇒ String
Get the URI has a URN in UUID namespace
133 134 135 |
# File 'lib/uuid.rb', line 133 def to_uri "urn:uuid:#{self}" end |