Class: Xid::Generator
- Inherits:
-
Object
- Object
- Xid::Generator
- Defined in:
- lib/ruby_xid.rb
Overview
Xid Generator
Instance Attribute Summary collapse
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #generate ⇒ Object
-
#initialize(rand_val = nil, machine_id = 0) ⇒ Generator
constructor
A new instance of Generator.
Constructor Details
#initialize(rand_val = nil, machine_id = 0) ⇒ Generator
Returns a new instance of Generator.
116 117 118 119 120 121 |
# File 'lib/ruby_xid.rb', line 116 def initialize(rand_val = nil, machine_id = 0) @mutex = Mutex.new @rand_int = rand_val || rand(65_535) @pid = Process.pid @machine_id = machine_id end |
Instance Attribute Details
#value ⇒ Object
Returns the value of attribute value.
114 115 116 |
# File 'lib/ruby_xid.rb', line 114 def value @value end |
Instance Method Details
#generate ⇒ Object
123 124 125 126 127 128 129 |
# File 'lib/ruby_xid.rb', line 123 def generate # () -> str @mutex.synchronize do @rand_int += 1 end [::Time.new.to_i, @machine_id, @pid, @rand_int << 8].pack('N NX n NX') end |