Class: FileProcessor::CSV
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- FileProcessor::CSV
- Includes:
- Enumerable
- Defined in:
- lib/file_processor/csv.rb
Instance Attribute Summary collapse
-
#detected_encoding ⇒ Object
Returns the value of attribute detected_encoding.
Class Method Summary collapse
-
.open(*args) ⇒ Object
Opens a file and yields it, ensuring that it is properly closed.
Instance Method Summary collapse
-
#each ⇒ Object
Yields each row of the data source in turn, skipping blanks and rows with no data.
-
#gzipped? ⇒ Boolean
Returns true when the file is gzipped, false otherwise.
-
#initialize(filename, options = {}) ⇒ CSV
constructor
A new instance of CSV.
-
#process_range(options = {}) ⇒ Enumerable
Process a range of lines in the CSV file.
-
#total_count(&block) ⇒ Integer
Counts the number of rows in the file, even if it has already been read.
Constructor Details
#initialize(filename, options = {}) ⇒ CSV
Returns a new instance of CSV.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/file_processor/csv.rb', line 22 def initialize(filename, ={}) @gzipped = .delete(:gzipped) load(filename, .delete(:open_options)) = .merge() [:encoding] ||= detect_encoding @detected_encoding ||= Encoding.find([:encoding]) tempfile.reopen(detected_mode) if tempfile.closed? [:col_sep] ||= detect_column_separator super(::CSV.new(tempfile, )) end |
Instance Attribute Details
#detected_encoding ⇒ Object
Returns the value of attribute detected_encoding.
20 21 22 |
# File 'lib/file_processor/csv.rb', line 20 def detected_encoding @detected_encoding end |
Class Method Details
.open(*args) ⇒ Object
Opens a file and yields it, ensuring that it is properly closed.
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/file_processor/csv.rb', line 6 def self.open(*args) instance = new(*args) if block_given? begin yield instance ensure instance.close if instance end else instance end end |
Instance Method Details
#each ⇒ Object
Yields each row of the data source in turn, skipping blanks and rows with no data.
Support for Enumerable.
The data source must be open for reading.
56 57 58 59 60 61 62 63 64 |
# File 'lib/file_processor/csv.rb', line 56 def each if block_given? while row = shift yield row unless skip_blanks? && row_with_no_data?(row) end else to_enum end end |
#gzipped? ⇒ Boolean
Returns true when the file is gzipped, false otherwise
97 98 99 |
# File 'lib/file_processor/csv.rb', line 97 def gzipped? @gzipped end |
#process_range(options = {}) ⇒ Enumerable
Process a range of lines in the CSV file.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/file_processor/csv.rb', line 79 def process_range(={}) ||= {} offset = [:offset] || 0 limit = [:limit] || -1 rewind each_with_index do |row, index| next if index < offset break if limit >= 0 && index >= offset + limit yield row, index end ensure rewind end |
#total_count(&block) ⇒ Integer
Counts the number of rows in the file, even if it has already been read
42 43 44 45 46 47 |
# File 'lib/file_processor/csv.rb', line 42 def total_count(&block) rewind count(&block) ensure rewind end |