Permalink
Browse files

ability to create ObjectId at a given time

  • Loading branch information...
1 parent 52dc88b commit b3efd5575774573cd5db05be5bd00cf597f0c4c2 @mislav committed Mar 7, 2011
Showing with 19 additions and 0 deletions.
  1. +19 −0 lib/mingo.rb
View
@@ -11,6 +11,25 @@ def self.[](id)
def id
self
end
+
+ def initialize(time_or_data = nil)
+ if Time === time_or_data or Date === time_or_data
+ @data = generate_from_time(time_or_data)
+ else
+ @data = time_or_data || generate
+ end
+ end
+
+ private
+
+ def generate_from_time(time)
+ oid = ''
+ oid += [time.to_i].pack("N")
+ oid += Digest::MD5.digest(Socket.gethostname)[0, 3]
+ oid += [Process.pid % 0xFFFF].pack("n")
+ oid += [get_inc].pack("N")[1, 3]
+ oid.unpack("C12")
+ end
end
class Mingo < Hashie::Dash

0 comments on commit b3efd55

Please sign in to comment.