lib/openwfe/utils.rb in openwferu-0.9.15 vs lib/openwfe/utils.rb in openwferu-0.9.16
- old
+ new
@@ -106,10 +106,13 @@
d = REXML::Document.new object.to_s
return d if object.kind_of?(REXML::Document)
return d.root
end
+ return Rational(object.denominator, object.numerator) \
+ if object.kind_of?(Rational)
+
o = nil
begin
o = object.class.new
rescue ArgumentError
@@ -151,31 +154,36 @@
o
end
def OpenWFE.to_underscore (string)
+
string.gsub("-", "_")
end
def OpenWFE.to_dash (string)
+
string.gsub("_", "-")
end
def OpenWFE.symbol_to_name (symbol)
+
to_dash(symbol.to_s)
end
def OpenWFE.name_to_symbol (name)
+
return name if name.is_a?(Symbol)
- return to_underscore(name).intern
+ to_underscore(name).intern
end
#
# Turns all the spaces in string into underscores.
# Returns the new String.
#
def OpenWFE.stu (s)
+
s.gsub("\s", "_")
end
#
# Returns an URI if the string is one, else returns nil.
@@ -334,11 +342,11 @@
#
# Returns the number of milliseconds since this Timer was
# instantiated.
#
def duration
- return (Time.now.to_f - @start) * 1000
+ (Time.now.to_f - @start) * 1000
end
end
#
@@ -353,19 +361,21 @@
#
# 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
+
+ s
end
#
# "my//path" -> "my/path"
#
@@ -377,12 +387,19 @@
# This method is used within the InFlowWorkItem and the CsvTable classes.
#
def OpenWFE.lookup_attribute (container, key)
key, rest = pop_key(key)
- value = container[key]
+ #value = nil
+ #begin
+ # value = container[key]
+ #rescue Exception => e
+ #end
+
+ value = flex_lookup(container, key)
+
return value unless rest
return nil unless value
lookup_attribute(value, rest)
@@ -391,21 +408,26 @@
#
# This method is used within the InFlowWorkItem and the CsvTable classes.
#
def OpenWFE.has_attribute? (container, key)
- key, rest = pop_key(key)
+ key, last_key = pop_last_key(key)
- unless rest
+ container = lookup_attribute(container, key) if key
- return container.has_key?(key) \
- if container.respond_to?(:has_key?)
+ if container.respond_to?(:has_key?)
- return false
- end
+ (container.has_key?(last_key) or container.has_key?(last_key.to_s))
- has_attribute?(rest, key)
+ elsif container.is_a?(Array) and last_key.is_a?(Fixnum)
+
+ (last_key < container.length)
+
+ else
+
+ false
+ end
end
#
# Returns a list of lines matching the pattern in the given file.
#
@@ -413,10 +435,12 @@
#
# OpenWFE::grep "^..) ", "path/to/file.txt" do |line|
# puts " - '#{line.downcase}'"
# end
#
+ # TODO : find a ruby library and use it instead of that code
+ #
def OpenWFE.grep (pattern, filepath, &block)
result = []
r = Regexp.new pattern
@@ -466,20 +490,50 @@
end
end
protected
- def pop_key (key)
+ def OpenWFE.pop_key (key)
i = key.index(".")
return narrow(key), nil unless i
[ narrow(key[0..i-1]), key[i+1..-1] ]
end
- def narrow (key)
+ def OpenWFE.pop_last_key (key)
+ i = key.rindex(".")
+ return nil, narrow(key) unless i
+ [ key[0..i-1], narrow(key[i+1..-1]) ]
+ end
+
+ def OpenWFE.narrow (key)
return 0 if key == "0"
i = key.to_i
return i if i != 0
key
+ end
+
+ #
+ # looks up in a container (something that has a [] method) with a key,
+ # if nothing has been found and the key is a number, turns the
+ # the number into a String a does a last lookup.
+ #
+ def OpenWFE.flex_lookup (container, key)
+
+ value = nil
+
+ begin
+ value = container[key]
+ rescue Exception => e
+ end
+
+ if value == nil and key.kind_of?(Fixnum)
+ begin
+ value = container[key.to_s]
+ rescue Exception => e
+ end
+ end
+
+ value
end
end