lib/openwfe/utils.rb in openwferu-0.9.9 vs lib/openwfe/utils.rb in openwferu-0.9.10

- old
+ new

@@ -110,31 +110,38 @@ # 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 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 + return object.dup if object.kind_of?(String) #return deep_clone(object) if object.kind_of? REXML::Document - return REXML::Document.new(object.to_s) \ - if object.kind_of? REXML::Document + + #return REXML::Document.new(object.to_s) \ + # if object.kind_of? REXML::Document + if object.kind_of?(REXML::Element) + d = REXML::Document.new object.to_s + return d if object.kind_of?(REXML::Document) + return d.root + end + o = object.class.new # # some kind of collection ? - if object.kind_of? Array + if object.kind_of?(Array) object.each do |i| o << fulldup(i) end - elsif object.kind_of? Hash + elsif object.kind_of?(Hash) object.each do |k, v| o[copy(k)] = fulldup(v) end end @@ -156,11 +163,11 @@ rescue # ignore, must be readonly end end - return o + o end def OpenWFE.to_underscore (string) string.gsub("-", "_") end @@ -193,11 +200,11 @@ def OpenWFE.parse_uri (string) begin return URI::parse(string) rescue Exception => e end - return nil + nil end # # Returns true if the given string starts with the 'start' string. # @@ -207,20 +214,20 @@ # method to the String class, but that could be intrusive # (as OpenWFE is meant at first as an embeddable workflow engine). # return false unless string return false if string.length < start.length - return string[0, start.length] == start + string[0, start.length] == start end # # Returns true if the given string ends with the '_end' string. # def OpenWFE.ends_with (string, _end) return false unless string return false if string.length < _end.length - return string[-_end.length..-1] == _end + string[-_end.length..-1] == _end end # # Attempts at displaying a nice stack trace # @@ -252,22 +259,26 @@ # end # else # raw_expression.apply(wi) # end # + # Returns the new thread instance. + # def OpenWFE.call_in_thread (caller_name, caller_object=nil, &block) return unless block Thread.new do begin #$SAFE = safe_level # + # (note) # doesn't work : the block inherits the safety level # of its surroundings, it's a closure, ne ? 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 @@ -366,9 +377,37 @@ return false end return has_attribute?(rest, key) + end + + # + # Returns a list of lines matching the pattern in the given file. + # + # This is also possible : + # + # OpenWFE::grep "^..) ", "path/to/file.txt" do |line| + # puts " - '#{line.downcase}'" + # end + # + def OpenWFE.grep (pattern, filepath, &block) + + result = [] + r = Regexp.new pattern + + File.open filepath do |file| + file.readlines.each do |l| + if r.match l + if block + block.call l + else + result << l + end + end + end + end + result unless block end # # This method is used within the InFlowWorkItem and the CsvTable classes. #