lib/openwfe/util/csvtable.rb in openwferu-0.9.6 vs lib/openwfe/util/csvtable.rb in openwferu-0.9.7

- old
+ new

@@ -105,10 +105,31 @@ # # puts h["team_member"] # # will yield "Henry" who takes care of all the politics stuff, # # except for Asia and America # + # '>', '>=', '<' and '<=' can be put in front of individual cell values : + # + # table = CsvTable.new(""" + # , + # in:fx, out:fy + # , + # >100,a + # >=10,b + # ,c + # """) + # + # h = { 'fx' => '10' } + # table.transform(h) + # + # require 'pp'; pp h + # # will yield { 'fx' => '10', 'fy' => 'b' } + # + # Such comparisons are done after the elements are transformed to float + # numbers. By default, non-numeric arguments will get compared as Strings. + # + # # Disclaimer : the decision / CSV table system is no replacement for # full rule engines with forward and backward chaining, RETE implementation # and the like... # # @@ -227,24 +248,72 @@ next if cell.length < 1 cell = OpenWFE::dosub(cell, fexp, wi) - modifiers = 0 - modifiers += Regexp::IGNORECASE if @ignore_case - #puts "__does '#{value}' match '#{cell}' ?" - rcell = Regexp.new(cell, modifiers) - return false unless rcell.match(value) + b = if cell[0, 1] == '<' or cell[0, 1] == '>' + numeric_compare(value, cell) + else + regex_compare(value, cell) + end + + return false unless b end #puts "__row matches" return true end + def regex_compare (value, cell) + + modifiers = 0 + modifiers += Regexp::IGNORECASE if @ignore_case + + rcell = Regexp.new(cell, modifiers) + + rcell.match(value) + end + + def numeric_compare (value, cell) + + comparator = cell[0, 1] + comparator += "=" if cell[1, 1] == "=" + cell = cell[comparator.length..-1] + + nvalue = narrow(value) + ncell = narrow(cell) + + if nvalue.is_a? String or ncell.is_a? String + value = '"' + value + '"' + cell = '"' + cell + '"' + else + value = nvalue + cell = ncell + end + + s = "#{value} #{comparator} #{cell}" + + #puts "...>>>#{s}<<<" + + begin + return OpenWFE::eval_safely(s, 4) + rescue Exception => e + return false + end + end + + def narrow (s) + begin + return Float(s) + rescue Exception => e + return s + end + end + def resolve_in_header (in_header) in_header = "f:#{in_header}" \ if points_to_nothing? in_header @@ -277,10 +346,11 @@ if type == "v" fexp.set_variable(target, value) if fexp elsif type == "f" wi.set_attribute(target, value) elsif type == "r" - instance_eval(value) + #instance_eval(value) + OpenWFE::instance_eval_safely(self, value, 3) end end end def parse_header_row (row)