Module: Jcsv::Header

Defined in:
lib/reader.rb

Overview

Instance Method Summary collapse

Instance Method Details

#filters=(filters) ⇒ Object





61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/reader.rb', line 61

def filters=(filters)
  
  case filters
  when Hash
    filters = filters.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} unless
      @strings_as_keys
    filters.each do |column_name, processor|
      @filters[column_name] = processor
    end
  when Array
    raise "One filter needed for each column.  Filters size: #{filters.size}" if
      headers.size != filters.size
    filters.each_with_index do |processor, i|
      @filters[i] = processor
    end
  else
    raise ArgumentError.new("Filters parameters should either be a hash or an array of filters")
  end
  
end

#parse_with_block(&block) ⇒ Object


A chunk is either one row of the file, or an array with rows. One row can be either a one dimensional array with all columns or a hash with all columns (excluding the dimensions).




88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/reader.rb', line 88

def parse_with_block(&block)

  # if there is a valid column_mapping, then we need to change the mapped_header
  mapped_header = @headers
  if (@column_mapping.mapping)
    mapped_header = Array.new
    @column_mapping.mapping.each_with_index do |map, index|
      mapped_header[map] = @headers[index] if (map.is_a? Numeric)
    end
  end

  while (!((chunk = read_chunk).nil?))
    if (mapped_header.size == 0)
      block.call(@reader.getLineNumber(), @reader.getRowNumber(), format(chunk))
    else
      block.call(@reader.getLineNumber(), @reader.getRowNumber(), format(chunk),
                 mapped_header)
    end
  end
  
end