lib/openwfe/expressions/condition.rb in openwferu-0.9.8 vs lib/openwfe/expressions/condition.rb in openwferu-0.9.9

- old
+ new

@@ -77,26 +77,27 @@ if rconditional and not conditional return nil \ unless conditional - #ldebug { "eval_condition() 0 for '#{conditional}'" } + ldebug { "eval_condition() 0 for '#{conditional}'" } - conditional = from_xml(conditional) + conditional = from_xml conditional - #ldebug { "eval_condition() 1 for '#{conditional}'" } + ldebug { "eval_condition() 1 for '#{conditional}'" } + begin + return to_boolean(do_eval(conditional)) + rescue Exception => e + # probably needs some quoting... + end + conditional = do_quote(conditional) ldebug { "eval_condition() 2 for '#{conditional}'" } - #result = instance_eval(conditional) - result = do_eval(conditional) - - ldebug { "eval_condition() 3 result is '#{result}'" } - - return (result == "true" or result == true) + to_boolean(do_eval(conditional)) end # # Returns nil if the cited attname (without or without 'r' prefix) # is not present. @@ -119,49 +120,61 @@ nil end protected + private + # - # If the attribute name is a list of attribute names, pick - # the first one present. + # Returns true if result is the "true" String or the true + # boolean value. Returns false else. # - #def pick_attribute (attnames) - # attnames.each do |attname| - # return attname if has_attribute(attname) - # return attname if has_attribute("r" + attname.to_s) - # end - # return attnames[0] - # # - # # some kind of a default (for error messages) - #end + def to_boolean (result) + ldebug { "to_boolean() result is '#{result}'" } + (result == "true" or result == true) + end - private - def from_xml (string) s = string s.gsub!("&gt;", ">") s.gsub!("&lt;", "<") s end + # + # Quotes the given string so that it can easily get evaluated + # as Ruby code (a string comparison actually). + # def do_quote (string) - i = string.index("==") - i = string.index("!=") unless i - i = string.index("<") unless i - i = string.index(">") unless i + op = find_operator string - return '"' + string + '"' unless i + return '"' + string + '"' unless op - '"' + - string[0..i-1].strip + - '" ' + - string[i..i+2] + - ' "' + - string[i+2..-1].strip + - '"' + op, i = op + + s = '"' + s << string[0..i-1].strip + s << '" ' + s << string[i, op.length] + s << ' "' + s << string[i+op.length..-1].strip + s << '"' + s + end + + # + # Returns the operator and its index (position) in the string. + # Returns nil if not operator was found in the string. + # + def find_operator (string) + [ "==", "!=", "<=", ">=", "<", ">" ].each do |op| + i = string.index op + next unless i + return [ op, i ] + end + nil end # # Runs the given given within an instance_eval() at a $SAFE # level of 3.