Method: LexicalUUID#initialize

Defined in:
lib/lexical_uuid.rb

#initialize(timestamp = nil, worker_id = nil, timestamp_factory = IncreasingMicrosecondClock) ⇒ LexicalUUID

Returns a new instance of LexicalUUID.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/lexical_uuid.rb', line 23

def initialize(timestamp = nil, worker_id = nil, timestamp_factory = IncreasingMicrosecondClock)
  case timestamp
  when Fixnum, Bignum
    @timestamp = timestamp
    @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(Array(elements.join).pack('H32'))
    else
      raise ArgumentError, 
        "#{timestamp} was incorrectly sized. Must be 16 timestamp."
    end
  when Time
    @timestamp = timestamp.stamp
    @worker_id = self.class.worker_id
  when nil
    @worker_id = self.class.worker_id
    @timestamp = timestamp_factory.call
  end
end