Class Rools::CsvTable
In: lib/rools/csv_table.rb
Parent: Base

Methods

new   quote  

Attributes

rules  [R] 

Public Class methods

[Source]

    # File lib/rools/csv_table.rb, line 21
21:     def initialize( fileName )
22:       csv_data = IO.read( fileName)
23:       arrs = []
24:       CSV::Reader.parse(csv_data, ",", "\r") do |row|
25:         #puts "row:#{row.inspect}"
26:         arrs << row
27:       end
28:       
29:       # get rule parameter 
30:       parameter = arrs[1][1]
31:       #puts "parameter:#{parameter}"
32:       
33:       # get rule elements Conditions/Consequences
34:       rule_elements = arrs[2]
35:       
36:       # get code
37:       rule_code = arrs[3]      
38:       
39:       # get headers
40:       headers = arrs[4]
41:       
42:       #get number of rules
43:       num_rules = arrs.size-5
44:       #puts "num rules: #{num_rules}"
45:       
46:       index   = 0
47:       @rules  = ""
48:       arrs[5..arrs.size].each { |arr|
49:         rule_name = "rule_#{index}"
50:         #puts "arr:#{arr} index: #{index}"
51:         
52:         #if rule_elements[index] != nil
53:           @rules << "rule '#{rule_name}' do \n"
54:           @rules << "  parameter #{parameter}\n"
55:           column = 0
56:           
57:           rule_elements.each do |element|
58:             
59:             field  = headers[column].downcase if headers[column]
60:             str    = arr[column]
61:             
62:             if str != nil && element != nil
63:               #puts ("eval: #{field} = '#{str}'")
64:               #eval( "#{field} = '#{str}'" )
65:   
66:               @rules << "\t" + element.downcase + "{ "
67:               pattern = "\#\{#{field}\}"
68:               
69:               #puts rule_code[column]
70:               #puts "pattern: #{pattern} str:#{str}"
71:               if rule_code[column] == pattern
72:                 statement = str # straight replace
73:               else
74:                 statement = rule_code[column].gsub(pattern,quote(str))
75:               end
76:               
77:               #puts "statement:#{statement}"
78:               
79:               @rules << statement     
80:               @rules << "}\n"
81:             end
82:             
83:             column += 1
84:           end
85:           @rules << "end\n"
86:         #end
87:         index += 1
88:       }
89:     end

Public Instance methods

return quoted String or Number There is probably a more elagant way of doing this but…

[Source]

    # File lib/rools/csv_table.rb, line 15
15:     def quote( str )
16:       return str if (str.to_i.to_s == str)
17:       return str if (str.to_f.to_s == str)
18:       '"' + str + '"'
19:     end

[Validate]