lib/openwfe/expressions/condition.rb in openwferu-0.9.16 vs lib/openwfe/expressions/condition.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:
#
@@ -35,11 +35,11 @@
# "made in Japan"
#
# John Mettraux at openwfe.org
#
-require 'openwfe/util/safe'
+require 'rufus/eval' # gem 'rufus-eval'
module OpenWFE
#
@@ -114,38 +114,72 @@
if rconditional and not conditional
return nil \
unless conditional
- ldebug { "do_eval_condition() 0 for '#{conditional}'" }
+ ldebug { "do_eval_condition() 0 for >#{conditional}<" }
conditional = unescape conditional
- ldebug { "do_eval_condition() 1 for '#{conditional}'" }
+ ldebug { "do_eval_condition() 1 for >#{conditional}<" }
+ r = eval_set conditional
+ return r if r != nil
+
begin
return to_boolean(do_eval(conditional, workitem))
rescue Exception => e
# probably needs some quoting...
+ ldebug { "do_eval_condition() e : #{e}" }
end
conditional = do_quote(conditional)
- ldebug { "do_eval_condition() 2 for '#{conditional}'" }
+ ldebug { "do_eval_condition() 2 for >#{conditional}<" }
to_boolean(do_eval(conditional, workitem))
end
+ SET_REGEX = /(\S*?)( is)?( not)? set$/
+
+ #
+ # Evals the 'x [ is][ not] set' notation...
+ #
+ def eval_set (cond)
+
+ m = SET_REGEX.match cond
+
+ return nil unless m
+
+ val = m[1]
+ n = m[3]
+
+ ldebug do
+ "eval_set() for >#{cond}< "+
+ "m[1] is '#{val}', m[3] is '#{n}'"
+ end
+
+ val = val.strip if val
+ val = (val != nil and val != '')
+ n = (n and n.strip == 'not')
+
+ n ? (not val) : val
+ end
+
private
#
# Returns true if result is the "true" String or the true
# boolean value. Returns false else.
#
- def to_boolean (result)
- ldebug { "to_boolean() result is '#{result}'" }
- (result == "true" or result == true)
+ def to_boolean (o)
+
+ ldebug { "to_boolean() o is _#{o}_" }
+
+ #(o == "true" or o == true)
+ o = o.strip if o.is_a?(String)
+ not (o == nil || o == false || o == 'false' || o == '')
end
#
# unescapes '>' and '<'
#
@@ -179,28 +213,31 @@
#
# 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.
+ # level of 4.
#
def do_eval (s, workitem)
- #OpenWFE::instance_eval_safely(self, s, 3)
-
wi = workitem
fe = self
- OpenWFE::eval_safely(s, 3, binding())
+ #
+ # wi and fe are thus available as well
+ # (as self and workitem)
+
+ Rufus::eval_safely(s, 4, binding())
end
end
end