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_data ⇒ 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.
115 116 117 118 119 120 |
# File 'lib/ruby_xid.rb', line 115 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.
113 114 115 |
# File 'lib/ruby_xid.rb', line 113 def value @value end |
Instance Method Details
#generate_data ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/ruby_xid.rb', line 122 def generate_data # () -> str @mutex.synchronize do @rand_int += 1 end [::Time.new.to_i, @machine_id, @pid, @rand_int << 8].pack('N NX n NX') end |