lib/openwfe/utils.rb in openwferu-0.9.4 vs lib/openwfe/utils.rb in openwferu-0.9.5
- old
+ new
@@ -97,10 +97,11 @@
#
def OpenWFE.fulldup (object)
return nil if object == nil
+ return object if object.kind_of? Float
return object if object.kind_of? Fixnum
return object if object.kind_of? TrueClass
return object if object.kind_of? FalseClass
return object.dup if object.kind_of? String
@@ -195,22 +196,83 @@
end
return s
end
#
+ # Some code for writing thinks like :
+ #
+ # if async
+ # OpenWFE::call_in_thread "launch()", self do
+ # raw_expression.apply(wi)
+ # end
+ # else
+ # raw_expression.apply(wi)
+ # end
+ #
+ def OpenWFE.call_in_thread (caller_name, caller_object=nil, &block)
+ return unless block
+ Thread.new do
+ begin
+ block.call
+ rescue Exception => e
+ msg = "#{caller_name} caught an exception\n" + exception_to_s(e)
+ if caller_object and caller_object.respond_to? :lwarn
+ caller_object.lwarn { msg }
+ else
+ puts msg
+ end
+ end
+ end
+ # implicitely return the thread
+ end
+
+ #
# A small Timer class for debug purposes.
#
+ # t = Time.new
+ #
+ # # ... do something
+ #
+ # puts "that something took #{t.duration} ms"
+ #
class Timer
attr_reader :start
def initialize
@start = Time.now.to_f
end
+ #
+ # Returns the number of milliseconds since this Timer was
+ # instantiated.
+ #
def duration
return (Time.now.to_f - @start) * 1000
end
+ end
+
+ #
+ # Returns a version of s that is usable as or within a filename
+ # (removes for examples things like '/' or '\'...)
+ #
+ def OpenWFE.ensure_for_filename (s)
+ s = s.gsub(" ", "_")
+ s = s.gsub("/", "_")
+ s = s.gsub(":", "_")
+ s = s.gsub(";", "_")
+ s = s.gsub("\*", "_")
+ s = s.gsub("\\", "_")
+ s = s.gsub("\+", "_")
+ s = s.gsub("\?", "_")
+ return s
+ end
+
+ #
+ # "my//path" -> "my/path"
+ #
+ def OpenWFE.clean_path (s)
+ s.gsub(/\/+/, "/")
end
#
# This method is used within the InFlowWorkItem and the CsvTable classes.
#