lib/openwfe/util/dollar.rb in openwferu-0.9.16 vs lib/openwfe/util/dollar.rb in openwferu-0.9.17
- old
+ new
@@ -1,8 +1,8 @@
#
#--
-# Copyright (c) 2006-2007, John Mettraux, OpenWFE.org
+# Copyright (c) 2006-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,133 +35,87 @@
# "made in Japan"
#
# John Mettraux at openwfe.org
#
+#require 'rubygems'
+require 'rufus/dollar' # gem 'rufus-dollar'
+require 'rufus/eval' # gem 'rufus-eval'
+
require 'openwfe/utils'
-require 'openwfe/util/safe'
#
# 'dollar notation' implementation in Ruby
#
module OpenWFE
- DSUB_SAFETY_LEVEL = 3
+ DSUB_SAFETY_LEVEL = 4
#
# Ruby code ${ruby:...} will be evaluated with this
# safety level.
# (see http://www.rubycentral.com/book/taint.html )
#
- # Performs 'dollar substitution' on a piece of text with a given
- # dictionary.
- #
- def OpenWFE.dsub (text, dict)
-
- text = text.to_s
-
- #puts "### text is >#{text}<"
- #puts "### dict is of class #{dict.class.name}"
-
- #return nil unless text
-
- j = text.index("}")
-
- return text if not j
-
- t = text[0, j]
-
- i = t.rindex("${")
- ii = t.rindex("\\${")
-
- #puts "i is #{i}"
- #puts "ii is #{ii}"
-
- return text if not i
-
- return unescape(text) if (i) and (i != 0) and (ii == i-1)
- #
- # found "\${"
-
- key = text[i+2..j-1]
-
- #puts "### key is '#{key}'"
-
- value = dict[key]
-
- #puts "### value 0 is '#{value}'"
-
- value = if value
- value.to_s
- else
- if dict.has_key?(key)
- "false"
- else
- ""
- end
- end
-
- #puts "### value 1 is '#{value}'"
-
- #puts "pre is >#{text[0..i-1]}<"
- #puts "post is >#{text[j+1..-1]}<"
-
- pre = if i > 0
- text[0..i-1]
- else
- ""
- end
-
- dsub("#{pre}#{value}#{text[j+1..-1]}", dict)
- end
-
- def OpenWFE.unescape (text)
- text.gsub("\\\\\\$\\{", "\\${")
- end
-
- #
# Performs 'dollar substitution' on a piece of text with as input
# a flow expression and a workitem (fields and variables).
#
def OpenWFE.dosub (text, flow_expression, workitem)
- dsub(text, FlowDict.new(flow_expression, workitem))
+
+ Rufus::dsub(text, FlowDict.new(flow_expression, workitem))
end
#
# Wrapping a process expression and the current workitem as a
# Hash object ready for lookup at substitution time.
#
- class FlowDict < Hash
+ class FlowDict
- def initialize (flow_expression, workitem)
+ def initialize (flow_expression, workitem, default_prefix='v')
+
@flow_expression = flow_expression
@workitem = workitem
+ @default_prefix = default_prefix
end
def [] (key)
+
p, k = extract_prefix(key)
#puts "### p, k is '#{p}', '#{k}'"
return '' if k == ''
return @workitem.lookup_attribute(k) if p == 'f'
if p == 'v'
return '' unless @flow_expression
- return @flow_expression.lookup_variable(k)
+ return @flow_expression.lookup_variable(k)
end
- return call_function(k) if p == 'c'
+ #return call_function(k) if p == 'c'
return call_ruby(k) if p == 'r'
- # TODO : implement constant lookup
- @workitem.lookup_attribute(key)
+ @workitem.lookup_attribute key
end
+ def []= (key, value)
+
+ pr, k = extract_prefix(key)
+
+ if pr == 'f'
+
+ @workitem.set_attribute k, value
+
+ elsif @flow_expression
+
+ @flow_expression.set_variable k, value
+ end
+ end
+
def has_key? (key)
+
p, k = extract_prefix(key)
return false if k == ''
return @workitem.has_attribute?(k) if p == 'f'
@@ -169,29 +123,32 @@
if p == 'v'
return false unless @flow_expression
return (@flow_expression.lookup_variable(k) != nil)
end
- return true if p == 'c'
+ #return true if p == 'c'
return true if p == 'r'
- # TODO : implement constant lookup
@workitem.has_attribute?(key)
end
protected
def extract_prefix (key)
i = key.index(':')
- return 'v', key if not i
+ return @default_prefix, key if not i
[ key[0..0], key[i+1..-1] ]
end
- def call_function (function_name)
- #"function '#{function_name}' is not implemented"
- "functions are not yet implemented"
- end
+ #--
+ #def call_function (function_name)
+ # #"function '#{function_name}' is not implemented"
+ # "functions are not yet implemented"
+ # #
+ # # no need for them... we have Ruby :)
+ #end
+ #++
def call_ruby (ruby_code)
if @flow_expression
return "" \
@@ -219,10 +176,10 @@
# notations
#eval(ruby_code, binding).to_s
#eval(ruby_code).to_s
- OpenWFE::eval_safely(
+ Rufus::eval_safely(
ruby_code, DSUB_SAFETY_LEVEL, binding()).to_s
end
end
end