lib/ruote/id/mnemo_wfid_generator.rb in ruote-2.2.0 vs lib/ruote/id/mnemo_wfid_generator.rb in ruote-2.3.0

- old
+ new

@@ -1,7 +1,7 @@ #-- -# Copyright (c) 2005-2011, John Mettraux, jmettraux@gmail.com +# Copyright (c) 2005-2012, John Mettraux, jmettraux@gmail.com # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell @@ -20,24 +20,47 @@ # THE SOFTWARE. # # Made in Japan. #++ -require 'rufus/mnemo' # gem install rufus-mnemo -require 'ruote/id/wfid_generator' +require 'digest/md5' +require 'rufus/mnemo' module Ruote - class MnemoWfidGenerator < WfidGenerator + # + # The default wfid generator. + # + class MnemoWfidGenerator + def initialize(context) + + @context = context + + @here = "#{Ruote.local_ip}!#{Process.pid}" + @counter = 0 + #@mutex = Mutex.new + end + def generate - raw = get_raw + t = Time.now.utc + time = t.strftime('%Y%m%d-%H%M') + ms = t.to_f % 60.0 - m = ((raw.to_f % 60 * 60 * 24) * 1000).to_i + #c = @mutex.synchronize { @counter = (@counter + 1) % 100_000 } + @counter = (@counter + 1) % 100_000 + # + # no need to worry about skipping a beat, no mutex. - "#{raw.strftime('%Y%m%d')}-#{Rufus::Mnemo.from_integer(m)}" + s = "#{ms}!#{Thread.current.object_id}!#{@here}!#{@counter}" + s = Digest::MD5.hexdigest(s) + + x = Rufus::Mnemo.from_i(s[0, 6].to_i(16)) + y = Rufus::Mnemo.from_i(s[6, 6].to_i(16)) + + "#{time}-#{x}-#{y}" end end end