lib/openwfe/expool/wfidgen.rb in openwferu-0.9.16 vs lib/openwfe/expool/wfidgen.rb in openwferu-0.9.17
- old
+ new
@@ -1,8 +1,8 @@
#
#--
-# Copyright (c) 2007, John Mettraux, OpenWFE.org
+# Copyright (c) 2007-2008, John Mettraux, OpenWFE.org
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
@@ -37,13 +37,14 @@
# John Mettraux at openwfe.org
#
require 'thread'
+require 'rufus/mnemo'
+
require 'openwfe/service'
require 'openwfe/rudefinitions'
-require 'openwfe/util/kotoba'
module OpenWFE
#
@@ -52,10 +53,11 @@
# stored in the work directory in the file "wfidgen.last"
#
class DefaultWfidGenerator < Service
def initialize (service_name, application_context)
+
super
@last = -1
@mutex = Mutex.new
@@ -70,53 +72,61 @@
# Returns a new workflow instance id
#
# The launchitem parameter is not used by this generator.
#
def generate (launchitem=nil)
+
wfid = nil
+
@mutex.synchronize do
wfid = now
wfid = @last + 1 if wfid <= @last
@last = wfid
save_last
end
+
to_string(wfid)
end
#
# The actual job of turning the numeric result into a String.
# This method is overriden in extension of this class.
#
def to_string (numeric_id)
+
numeric_id.to_s
end
#
# Is a simple call to OpenWFE::split_wfid()
#
def split_wfid (wfid)
+
OpenWFE.split_wfid(wfid)
end
#
# This method is called by OpenWFE::split_wfid() when it has detected
# a wfid following this 'defaut' scheme.
#
def self.split_wfid (wfid)
+
r = []
0.upto(wfid.length-1) do |i|
r << wfid[i, 1]
end
+
r
end
#
# Stops this service.
# In this particular implementation, makes sure the "wfidgen.last"
# file is closed.
#
def stop
+
#linfo { "stop() stopping '#{@service_name}'" }
@last_f.close if @last_f
end
protected
@@ -173,10 +183,12 @@
#
# This extension of DefaultWfidGenerator produces ids like
# "20070318-jonowoguya" or "20071224-jesoshimoha" that are a bit
# easier to grasp than full integer wfids.
#
+ # Now relying on the 'rufus-mnemo' gem.
+ #
class KotobaWfidGenerator < DefaultWfidGenerator
#
# Overrides the to_string() method of the DefaultWfidGenerator,
#
@@ -194,21 +206,22 @@
i = numeric_id % (10 * 1000 * 60 * 60 * 24)
t = Time.now.gmtime
s = sprintf "%4d%02d%02d", t.year, t.month, t.day
s << "-"
- s << Kotoba::from_integer(i)
+ s << Rufus::Mnemo::from_integer(i)
s
end
#
# This method is called by OpenWFE::split_wfid() when it has detected
# a wfid following the 'kotoba' scheme.
# Returns the 'kotoba' wfid split into its syllables
#
def self.split_wfid (wfid)
- Kotoba::split(wfid[9..-1])
+
+ Rufus::Mnemo::split(wfid[9..-1])
end
#
# Turns a KotobaWfidGenerator produced wfid into a UTC date.
#
@@ -218,11 +231,11 @@
month = wfid[4, 2]
day = wfid[6, 2]
s = wfid[9..-1]
- i = Kotoba::to_integer(s)
+ i = Rufus::Mnemo::to_integer(s)
hour = (i / (10000 * 60 * 60)) % 24
min = (i / (10000 * 60)) % 60
sec = (i / 10000) % 60
usec = (i * 100) % 1000000
@@ -231,10 +244,11 @@
Time.utc(year, month, day, hour, min, sec, usec)
end
def self.from_time (t)
+
to_string(t.to_f * 10 * 1000).to_i
end
end
#
@@ -248,11 +262,11 @@
#
class FieldWfidGenerator < KotobaWfidGenerator
def initialize (service_name, application_context, field_name)
- super(service_name, application_context)
+ super service_name, application_context
@field_name = field_name
end
def generate (launchitem=nil)
@@ -314,17 +328,19 @@
# Generates a brand new UUID
#
# The launchitem parameter is not used by this generator.
#
def generate (launchitem=nil)
- return `#{@command}`.chomp
+
+ `#{@command}`.chomp
end
#
# Is a simple call to OpenWFE::split_wfid()
#
def split_wfid (wfid)
+
OpenWFE.split_wfid(wfid)
end
#
# This method is called by OpenWFE::split_wfid() when it has detected
@@ -332,10 +348,11 @@
#
# Splits the first part of the uuid (will be used for the
# expression storage directory structure).
#
def self.split_wfid (wfid)
+
s = wfid[0, 8]
a = []
4.times do |i|
a << s[i*2, 2]
end
@@ -355,9 +372,10 @@
#
# This method should be able to split any wfid whose scheme is implemented
# here.
#
def OpenWFE.split_wfid (wfid)
+
SPLIT_MAP.each do |regex, clazz|
return clazz.split_wfid(wfid) if wfid.match(regex)
end
#
# else