Class: LexicalUUID
- Inherits:
-
Object
- Object
- LexicalUUID
- Defined in:
- lib/lexical_uuid.rb
Instance Attribute Summary collapse
-
#jitter ⇒ Object
readonly
Returns the value of attribute jitter.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
-
#worker_id ⇒ Object
readonly
Returns the value of attribute worker_id.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(timestamp = nil, jitter = nil, worker_id = nil) ⇒ LexicalUUID
constructor
A new instance of LexicalUUID.
- #to_bytes ⇒ Object
-
#to_guid ⇒ Object
Also borrowed from simple_uuid.
Constructor Details
#initialize(timestamp = nil, jitter = nil, worker_id = nil) ⇒ LexicalUUID
Returns a new instance of LexicalUUID.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/lexical_uuid.rb', line 50 def initialize( = nil, jitter = nil, worker_id = nil) case when Fixnum, Bignum @timestamp = @jitter = jitter || create_jitter @worker_id = worker_id || self.class.worker_id when String case .size when 16 from_bytes() when 36 elements = .split("-") from_bytes([elements.join].pack('H32')) else raise ArgumentError, "#{} was incorrectly sized. Must be 16 timestamp." end when Time @timestamp = .stamp @jitter = create_jitter @worker_id = self.class.worker_id when nil @worker_id = self.class.worker_id @jitter = create_jitter @timestamp = Time.stamp end end |
Instance Attribute Details
#jitter ⇒ Object (readonly)
Returns the value of attribute jitter.
48 49 50 |
# File 'lib/lexical_uuid.rb', line 48 def jitter @jitter end |
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
48 49 50 |
# File 'lib/lexical_uuid.rb', line 48 def @timestamp end |
#worker_id ⇒ Object (readonly)
Returns the value of attribute worker_id.
48 49 50 |
# File 'lib/lexical_uuid.rb', line 48 def worker_id @worker_id end |
Class Method Details
.worker_id ⇒ Object
36 37 38 |
# File 'lib/lexical_uuid.rb', line 36 def worker_id @worker_id ||= create_worker_id end |
Instance Method Details
#<=>(other) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/lexical_uuid.rb', line 93 def <=>(other) if == other. && jitter == other.jitter worker_id <=> other.worker_id elsif == other. jitter <=> other.jitter else <=> other. end end |
#==(other) ⇒ Object
103 104 105 106 107 108 |
# File 'lib/lexical_uuid.rb', line 103 def ==(other) other.is_a?(LexicalUUID) && == other. && jitter == other.jitter && worker_id == other.worker_id end |
#eql?(other) ⇒ Boolean
110 111 112 |
# File 'lib/lexical_uuid.rb', line 110 def eql?(other) self == other end |
#hash ⇒ Object
114 115 116 |
# File 'lib/lexical_uuid.rb', line 114 def hash to_bytes.hash end |
#to_bytes ⇒ Object
78 79 80 81 82 83 |
# File 'lib/lexical_uuid.rb', line 78 def to_bytes [ >> 32, & 0xffffffff, jitter, worker_id].pack("iIii") end |
#to_guid ⇒ Object
Also borrowed from simple_uuid
86 87 88 89 90 91 |
# File 'lib/lexical_uuid.rb', line 86 def to_guid elements = to_bytes.unpack("NnnCCa6") node = elements[-1].unpack('C*') elements[-1] = '%02x%02x%02x%02x%02x%02x' % node "%08x-%04x-%04x-%02x%02x-%s" % elements end |