class Mongoid::Bigbang::Generator

Attributes

used[R]

Public Class Methods

new() click to toggle source
# File lib/mongoid/bigbang/generator.rb, line 14
def initialize()
  clean
end

Public Instance Methods

clean() click to toggle source
# File lib/mongoid/bigbang/generator.rb, line 18
def clean
  @used = {}
end
id_from_time(time) click to toggle source
# File lib/mongoid/bigbang/generator.rb, line 22
def id_from_time(time)
  time = unix_time(time)
  string = time.to_s(16) + random_for(time)
  BSON::ObjectId.from_string(string)
end

Private Instance Methods

already_used?(time, random) click to toggle source
# File lib/mongoid/bigbang/generator.rb, line 55
def already_used?(time, random)
  used[time] && used[time].include?(random)
end
random_for(time) click to toggle source
# File lib/mongoid/bigbang/generator.rb, line 42
def random_for(time)
  random = secure_random
  while already_used?(time, random)
    random = secure_random
  end
  used[time] ? used[time].push(random) : used[time] = [random]
  random
end
secure_random() click to toggle source
# File lib/mongoid/bigbang/generator.rb, line 51
def secure_random
  SecureRandom.hex(8)
end
unix_time(time) click to toggle source
# File lib/mongoid/bigbang/generator.rb, line 29
def unix_time(time)
  case time
  when Time
    time.to_i
  when Integer
    time
  when String
    Time.parse(time).to_i
  else
    raise ArgumentError
  end
end