Skip to content

Commit

Permalink
Showing 1 changed file with 39 additions and 27 deletions.
66 changes: 39 additions & 27 deletions lib/ruby/1.9/csv.rb
Original file line number Diff line number Diff line change
@@ -906,29 +906,35 @@ class MalformedCSVError < RuntimeError; end
# To add a combo field, the value should be an Array of names. Combo fields
# can be nested with other combo fields.
#
Converters = { integer: lambda { |f|
Integer(f.encode(ConverterEncoding)) rescue f
},
float: lambda { |f|
Float(f.encode(ConverterEncoding)) rescue f
},
converter_methods = Module.new do
def self.integer(f)
Integer(f.encode(ConverterEncoding)) rescue f
end
def self.float(f)
Float(f.encode(ConverterEncoding)) rescue f
end
def self.date(f)
begin
e = f.encode(ConverterEncoding)
e =~ DateMatcher ? Date.parse(e) : f
rescue # encoding conversion or date parse errors
f
end
end
def self.date_time(f)
begin
e = f.encode(ConverterEncoding)
e =~ DateTimeMatcher ? DateTime.parse(e) : f
rescue # encoding conversion or date parse errors
f
end
end
end
Converters = { integer: converter_methods.method(:integer),
float: converter_methods.method(:float),
numeric: [:integer, :float],
date: lambda { |f|
begin
e = f.encode(ConverterEncoding)
e =~ DateMatcher ? Date.parse(e) : f
rescue # encoding conversion or date parse errors
f
end
},
date_time: lambda { |f|
begin
e = f.encode(ConverterEncoding)
e =~ DateTimeMatcher ? DateTime.parse(e) : f
rescue # encoding conversion or date parse errors
f
end
},
date: converter_methods.method(:date),
date_time: converter_methods.method(:date_time),
all: [:date_time, :numeric] }

#
@@ -951,12 +957,18 @@ class MalformedCSVError < RuntimeError; end
# To add a combo field, the value should be an Array of names. Combo fields
# can be nested with other combo fields.
#
header_converters = Module.new do
def self.downcase(h)
h.encode(ConverterEncoding).downcase
end
def self.symbol(h)
h.encode(ConverterEncoding).downcase.strip.gsub(/\s+/, "_").
gsub(/\W+/, "").to_sym
end
end
HeaderConverters = {
downcase: lambda { |h| h.encode(ConverterEncoding).downcase },
symbol: lambda { |h|
h.encode(ConverterEncoding).downcase.gsub(/\s+/, "_").
gsub(/\W+/, "").to_sym
}
downcase: header_converters.method(:downcase),
symbol: header_converters.method(:symbol)
}

#

0 comments on commit 65c5183

Please sign in to comment.