class Rubyflake
Constants
- EPOCH
01/01/2011
- FLAKE_RANDOM_MAX
- FLAKE_TIMESTAMP_LENGTH
- FLAKE_TIMESTAMP_SHIFT
Public Class Methods
generate(epoch = Rubyflake::EPOCH)
click to toggle source
# File lib/rubyflake.rb, line 34 def generate(epoch = Rubyflake::EPOCH) Rubyflake.new(epoch).generate() end
new(epoch = EPOCH)
click to toggle source
# File lib/rubyflake.rb, line 10 def initialize(epoch = EPOCH) @epoch = epoch end
time(id, epoch = Rubyflake::EPOCH)
click to toggle source
# File lib/rubyflake.rb, line 38 def time(id, epoch = Rubyflake::EPOCH) Rubyflake.new(epoch).time(id) end
Public Instance Methods
generate()
click to toggle source
# File lib/rubyflake.rb, line 14 def generate # Figure out how many milliseconds have occurred since epoch. milliseconds = ((Time.now().to_f - @epoch) * 1000).to_i # Generate 23 random bits. random_bits = Random.new.rand(0..Rubyflake::FLAKE_RANDOM_MAX) # Shift our timestamp over 23 bits to make room for the random bits, # and then add the two together. (milliseconds << Rubyflake::FLAKE_TIMESTAMP_SHIFT) + random_bits.to_i end
time(id)
click to toggle source
Extract the time from an ID.
# File lib/rubyflake.rb, line 29 def time(id) Time.at(((id >> Rubyflake::FLAKE_TIMESTAMP_SHIFT) + @epoch) / 1000) end