Class: ACSV::CSV
- Inherits:
-
CSV
- Object
- CSV
- ACSV::CSV
- Defined in:
- lib/acsv/csv.rb
Overview
This class provides a complete interface to CSV files and data while trying to detect the separator and character set. It is Ruby’s standard CSV class with auto-detection facilities.
Please note that non-rewindable IO objects, like STDIN, are not supported.
Class Method Summary collapse
-
.open(*args) ⇒ Object
This method opens an IO object, and wraps that with CSV.
Instance Method Summary collapse
-
#initialize(data, options = Hash.new) ⇒ CSV
constructor
This constructor will wrap either a String or IO object passed in data for reading and/or writing.
Constructor Details
#initialize(data, options = Hash.new) ⇒ CSV
This constructor will wrap either a String or IO object passed in data for reading and/or writing. In case of reading, the character separator is auto-detected (unless given as an option).
16 17 18 19 |
# File 'lib/acsv/csv.rb', line 16 def initialize(data, = Hash.new) [:col_sep] ||= ACSV::Detect.separator(data) super(data, ) end |
Class Method Details
.open(*args) ⇒ Object
This method opens an IO object, and wraps that with CSV. For reading, separator and character encoding (when an encoding-detection gem is loaded) are auto-detected.
If the encoding
or external_encoding
option is set (and not nil
), or if the external encoding is specified as part of the mode parameter or option, no auto-detection takes place (since the given encoding is used).
When auto-detection fails, the default encoding as used by CSV and IO is taken.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/acsv/csv.rb', line 34 def self.open(*args) # find the +options+ Hash = if args.last.is_a? Hash then args.pop else Hash.new end # auto-detect encoding unless external encoding is specified full_mode = args[1] || 'rb' mode, ext_enc, int_enc = full_mode.split(':') if (ext_enc.nil? || ext_enc=='') && [:encoding].nil? && [:external_encoding].nil? # try to detect encoding if ext_enc = ACSV::Detect.encoding(File.open(args[0], mode, ), ) # workaround for http://stackoverflow.com/a/20723346 ext_enc = "BOM|#{ext_enc}" if ext_enc =~ /UTF/ # create new mode specification if there was one, else store in option # only one may be supplied to IO#new so we need to check this # also, BOM may only be specified as part of a mode parameter if full_mode.include?(':') || ext_enc.include?('BOM|') mode = "#{mode}:#{ext_enc}" mode += ":#{int_enc}" if int_enc args[1] = mode else [:external_encoding] = ext_enc end end end # remove options CSV doesn't understand .delete :confidence .delete :method # to superclass args << super(*args) end |