Class: Cql::Uuid
- Inherits:
-
Object
- Object
- Cql::Uuid
- Defined in:
- lib/cql/uuid.rb
Overview
Represents a UUID value.
This is a very basic implementation of UUIDs and exists more or less just to encode and decode UUIDs from and to Cassandra.
Instance Method Summary collapse
- #eql?(other) ⇒ Boolean (also: #==)
- #hash ⇒ Object
-
#initialize(n) ⇒ Uuid
constructor
Creates a new UUID either from a string (expected to be on the standard 8-4-4-4-12 form, or just 32 characters without hyphens), or from a 128 bit number.
-
#to_s ⇒ Object
Returns a string representation of this UUID in the standard 8-4-4-4-12 form.
-
#value ⇒ Bignum
Returns the numerical representation of this UUID.
Constructor Details
#initialize(n) ⇒ Uuid
Creates a new UUID either from a string (expected to be on the standard 8-4-4-4-12 form, or just 32 characters without hyphens), or from a 128 bit number.
16 17 18 19 20 21 22 23 |
# File 'lib/cql/uuid.rb', line 16 def initialize(n) case n when String @n = from_s(n) else @n = n end end |
Instance Method Details
#eql?(other) ⇒ Boolean Also known as: ==
52 53 54 |
# File 'lib/cql/uuid.rb', line 52 def eql?(other) self.value == other.value end |
#hash ⇒ Object
39 40 41 |
# File 'lib/cql/uuid.rb', line 39 def hash @h = (@n & 0xffffffffffffffff) ^ ((@n >> 64) & 0xffffffffffffffff) end |
#to_s ⇒ Object
Returns a string representation of this UUID in the standard 8-4-4-4-12 form.
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/cql/uuid.rb', line 27 def to_s @s ||= begin parts = [] parts << (@n >> (24 * 4)).to_s(16).rjust(8, '0') parts << ((@n >> (20 * 4)) & 0xffff).to_s(16).rjust(4, '0') parts << ((@n >> (16 * 4)) & 0xffff).to_s(16).rjust(4, '0') parts << ((@n >> (12 * 4)) & 0xffff).to_s(16).rjust(4, '0') parts << (@n & 0xffffffffffff).to_s(16).rjust(12, '0') parts.join('-').force_encoding(::Encoding::ASCII) end end |
#value ⇒ Bignum
Returns the numerical representation of this UUID
47 48 49 |
# File 'lib/cql/uuid.rb', line 47 def value @n end |