Module: Jcsv::Processors

Included in:
CLR, CMR
Defined in:
lib/supercsv_interface.rb

Overview

Module Processors interfaces the Ruby code with the SuperCsv cell processors.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dimensionsObject (readonly)

Returns the value of attribute dimensions.



64
65
66
# File 'lib/supercsv_interface.rb', line 64

def dimensions
  @dimensions
end

#key_arrayObject (readonly)

Returns the value of attribute key_array.



65
66
67
# File 'lib/supercsv_interface.rb', line 65

def key_array
  @key_array
end

Instance Method Details

#executeProcessors(processors) ⇒ Object


This method uses variable @processed_columns that should be initialized in the class that includes this module. In the case of a list_reader for instance, processed_columns is initalized as an Array. For map_reader, processed_columns is initalized as a Hash. So, processed_columns is a data structure for storing the data processed. The mapping defines where the data should be stored in this data structure. In the case of list_reader, mapping = i, for map_reader, mapping = <mapping name for hash>




84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/supercsv_interface.rb', line 84

def executeProcessors(processors)

  source = getColumns()
  
  context = CsvContext.new(getLineNumber(), getRowNumber(), 1);
  context.setRowSource(source);

  # raise "The number of columns to be processed #{source.size} must match the number of
  # CellProcessors #{processors.length}" if (source.size != processors.length)

  @key_array = Array.new
  
  source.each_with_index do |s, i|
    begin
      # is @column_mapping[i] ever nil? I don't think so... CHECK!!!
      next if ((@column_mapping[i] == false) || (@column_mapping[i].nil?))
      # if column mapping is 'true', then this column is a dimension and the data in this
      # column is part of the key
      if (@column_mapping[i] == true)
        begin
          @dimensions[@headers[i]] = s
        rescue RuntimeError => e
          puts "Warning reading row: #{source.toString()} in field '#{@headers[i]}'. " +
               e.message if !@suppress_warnings
          # raise "Error reading row: #{source.toString()} in field '#{@headers[i]}'. " +
          # e.message
        end
        @key_array[@dimensions.dimensions_names.index(@headers[i])] = s
        next
      end
      
      context.setColumnNumber(i + 1)
      if (i >= processors.size)
        @processed_columns[@column_mapping[i]] = s
      else
        if (processors[i] == nil)
          @processed_columns[@column_mapping[i]] = s
        else
          cell = processors[i].execute(s, context)
          # cell = (cell.is_a? Jcsv::Pack)? cell.ruby_obj : cell
          @processed_columns[@column_mapping[i]] = cell
        end
      end
    rescue SuperCsvConstraintViolationException => e
      raise Jcsv::ContraintViolation.new("Constraint violation: #{context.toString}")
    end
    
  end

  @processed_columns
  
end

#headersObject





71
72
73
# File 'lib/supercsv_interface.rb', line 71

def headers
  @headers ||= getHeader(true).to_a
end