Class: CSV::FieldsConverter
- Inherits:
-
Object
- Object
- CSV::FieldsConverter
- Includes:
- Enumerable
- Defined in:
- lib/csv/fields_converter.rb
Instance Method Summary collapse
- #add_converter(name = nil, &converter) ⇒ Object
- #convert(fields, headers, lineno) ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(options = {}) ⇒ FieldsConverter
constructor
A new instance of FieldsConverter.
Constructor Details
#initialize(options = {}) ⇒ FieldsConverter
Returns a new instance of FieldsConverter.
7 8 9 10 11 12 13 14 15 |
# File 'lib/csv/fields_converter.rb', line 7 def initialize(={}) @converters = [] @nil_value = [:nil_value] @empty_value = [:empty_value] @empty_value_is_empty_string = (@empty_value == "") @accept_nil = [:accept_nil] @builtin_converters = [:builtin_converters] @need_static_convert = need_static_convert? end |
Instance Method Details
#add_converter(name = nil, &converter) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/csv/fields_converter.rb', line 17 def add_converter(name=nil, &converter) if name.nil? # custom converter @converters << converter else # named converter combo = @builtin_converters[name] case combo when Array # combo converter combo.each do |sub_name| add_converter(sub_name) end else # individual named converter @converters << combo end end end |
#convert(fields, headers, lineno) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/csv/fields_converter.rb', line 41 def convert(fields, headers, lineno) return fields unless need_convert? fields.collect.with_index do |field, index| if field.nil? field = @nil_value elsif field.empty? field = @empty_value unless @empty_value_is_empty_string end @converters.each do |converter| break if field.nil? and @accept_nil if converter.arity == 1 # straight field converter field = converter[field] else # FieldInfo converter if headers header = headers[index] else header = nil end field = converter[field, FieldInfo.new(index, lineno, header)] end break unless field.is_a?(String) # short-circuit pipeline for speed end field # final state of each field, converted or original end end |
#each(&block) ⇒ Object
33 34 35 |
# File 'lib/csv/fields_converter.rb', line 33 def each(&block) @converters.each(&block) end |
#empty? ⇒ Boolean
37 38 39 |
# File 'lib/csv/fields_converter.rb', line 37 def empty? @converters.empty? end |