Module: CsvOmg::ClassMethods

Defined in:
lib/csv-omg.rb

Instance Method Summary collapse

Instance Method Details

#col(attr, *args, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/csv-omg.rb', line 50

def col(attr, *args, &block)
  attr = attr.to_s
  type = args.delete(([String, Float, Integer, Date, DateTime] & args).first) || String
  source = args.first || attr
  
  @_columns[attr] = Column.new(attr, source, type, block)
  
  create_accessor(attr)
end

#parse(contents, parser_opts = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/csv-omg.rb', line 29

def parse(contents, parser_opts={})
  @_parser_options.merge!(parser_opts)
  
  instances = []
  parser = FasterCSV.new(contents, @_parser_options)
  
  parser.each do |line|
    instance = new
    
    @_columns.values.each do |column|
      raw_value = line[column[:source]]
      
      value = column_value(raw_value, column[:type], column[:conversion])
      
      instance.send("#{column.attr.to_s}=", value)
    end
    instances << instance
  end
  instances
end