lib/openwfe/extras/util/csvtable.rb in openwferu-extras-0.9.13 vs lib/openwfe/extras/util/csvtable.rb in openwferu-extras-0.9.14
- old
+ new
@@ -36,13 +36,15 @@
#
# John Mettraux at openwfe.org
#
require 'csv'
+require 'open-uri'
require 'openwfe/utils'
require 'openwfe/util/dollar'
+require 'openwfe/workitem'
include OpenWFE
module OpenWFE
@@ -202,28 +204,53 @@
apply(row, flow_expression, workitem)
break if @first_match
end
end
- return workitem
+ workitem
end
#
# Passes a simple Hash instance though the csv table
#
def transform (hash)
+
wi = InFlowWorkItem.new()
wi.attributes = hash
- return transform_wi(nil, wi).attributes
+
+ transform_wi(nil, wi).attributes
end
+ #
+ # Outputs back this table as a CSV String
+ #
+ def to_csv
+
+ s = ""
+ s << @header.to_csv
+ s << "\n"
+ @rows.each do |row|
+ s << row.join(",")
+ s << "\n"
+ end
+ s
+ end
+
protected
def to_csv_array (csv_data)
- return csv_data if csv_data.kind_of? Array
- return CSV::Reader.parse(csv_data)
+ return csv_data if csv_data.kind_of?(Array)
+
+ if csv_data.is_a?(URI)
+ csv_data = csv_data.to_s
+ end
+ if OpenWFE::parse_uri(csv_data)
+ csv_data = open(csv_data)
+ end
+
+ CSV::Reader.parse(csv_data)
end
def matches? (row, fexp, wi)
return false if empty_row? row
@@ -260,11 +287,11 @@
return false unless b
end
#puts "__row matches"
- return true
+ true
end
def regex_compare (value, cell)
modifiers = 0
@@ -312,13 +339,13 @@
end
def resolve_in_header (in_header)
in_header = "f:#{in_header}" \
- if points_to_nothing? in_header
+ if points_to_nothing?(in_header)
- return "${#{in_header}}"
+ "${#{in_header}}"
end
def apply (row, fexp, wi)
#puts "__ apply() wi.class : #{wi.class.name}"
@@ -345,11 +372,10 @@
if type == "v"
fexp.set_variable(target, value) if fexp
elsif type == "f"
wi.set_attribute(target, value)
elsif type == "r"
- #instance_eval(value)
OpenWFE::instance_eval_safely(self, value, 3)
end
end
end
@@ -378,16 +404,17 @@
end
end
end
def empty_row? (row)
+
return true unless row
return true if (row.length == 1 and not row[0])
row.each do |cell|
return false if cell
end
- return true
+ true
end
def points_to_nothing? (label)
(not points_to_variable? label) and \
(not points_to_field? label) and \
@@ -416,11 +443,11 @@
return nil unless i
s = label[0..i-1]
names.each do |name|
return label[i+1..-1] if s == name
end
- return nil
+ nil
end
class Header
attr_accessor :ins, :outs
@@ -437,9 +464,21 @@
elsif OpenWFE::starts_with(cell, "out:")
@outs[icol] = cell[4..-1]
#puts "o added #{@outs[icol]}"
end
# else don't add
+ end
+
+ def to_csv
+
+ s = ""
+ @ins.each do |_in|
+ s << "in:#{_in}," if _in
+ end
+ @outs.each do |out|
+ s << "out:#{out}," if out
+ end
+ s[0..-2]
end
end
end
end