Class: LexicalUUID

Inherits:
Object
  • Object
show all
Defined in:
lib/lexical_uuid.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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(timestamp = nil, jitter = nil, worker_id = nil)
  case timestamp
  when Fixnum, Bignum
    @timestamp = timestamp
    @jitter    = jitter || create_jitter
    @worker_id = worker_id || self.class.worker_id
  when String
    case timestamp.size
    when 16
      from_bytes(timestamp)
    when 36
      elements = timestamp.split("-")
      from_bytes([elements.join].pack('H32'))
    else
      raise ArgumentError, 
        "#{timestamp} was incorrectly sized. Must be 16 timestamp."
    end
  when Time
    @timestamp = 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

#jitterObject (readonly)

Returns the value of attribute jitter.



48
49
50
# File 'lib/lexical_uuid.rb', line 48

def jitter
  @jitter
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



48
49
50
# File 'lib/lexical_uuid.rb', line 48

def timestamp
  @timestamp
end

#worker_idObject (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_idObject



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 timestamp == other.timestamp && jitter == other.jitter
    worker_id <=> other.worker_id
  elsif timestamp == other.timestamp
    jitter <=> other.jitter
  else
    timestamp <=> other.timestamp
  end
end

#==(other) ⇒ Object



103
104
105
106
107
108
# File 'lib/lexical_uuid.rb', line 103

def ==(other)
  other.is_a?(LexicalUUID) &&
    timestamp == other.timestamp &&
      jitter == other.jitter &&
        worker_id == other.worker_id
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/lexical_uuid.rb', line 110

def eql?(other)
  self == other
end

#hashObject



114
115
116
# File 'lib/lexical_uuid.rb', line 114

def hash
  to_bytes.hash
end

#to_bytesObject



78
79
80
81
82
83
# File 'lib/lexical_uuid.rb', line 78

def to_bytes
  [timestamp >> 32,
   timestamp & 0xffffffff,
   jitter,
   worker_id].pack("iIii")
end

#to_guidObject

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