Class: Sheetah::Backends::Csv
- Inherits:
-
Object
- Object
- Sheetah::Backends::Csv
show all
- Includes:
- Sheet
- Defined in:
- lib/sheetah/backends/csv.rb
Overview
Expect: - UTF-8 without BOM, or the correct encoding given explicitly - line endings as n or rn - comma-separated - quoted with “
Defined Under Namespace
Classes: ArgumentError, EncodingError
Constant Summary
Constants included
from Sheet
Sheet::COL_CONVERTER
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Sheet
col2int, int2col
Constructor Details
#initialize(io: nil, path: nil, encoding: nil) ⇒ Csv
Returns a new instance of Csv.
48
49
50
51
52
53
54
|
# File 'lib/sheetah/backends/csv.rb', line 48
def initialize(io: nil, path: nil, encoding: nil)
io = setup_io(io, path, encoding)
@csv = CSV.new(io, **CSV_OPTS)
@headers = (@csv)
@cols_count = @headers.size
end
|
Class Method Details
.register(registry = Backends.registry) ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/sheetah/backends/csv.rb', line 31
def self.register(registry = Backends.registry)
registry.set(self) do |args, opts|
next false unless args.empty?
case opts
in { io: _, **nil } | \
{ io: _, encoding: String | Encoding, **nil } | \
{ path: /\.csv$/i, **nil } | \
{ path: /\.csv$/i, encoding: String | Encoding, **nil }
then
true
else
false
end
end
end
|
Instance Method Details
#close ⇒ Object
84
85
86
87
88
|
# File 'lib/sheetah/backends/csv.rb', line 84
def close
@csv.close
nil
end
|
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/sheetah/backends/csv.rb', line 56
def
return to_enum(:each_header) { @cols_count } unless block_given?
@headers.each_with_index do |, col_idx|
col = Sheet.int2col(col_idx + 1)
yield Header.new(col: col, value: )
end
self
end
|
#each_row ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/sheetah/backends/csv.rb', line 68
def each_row
return to_enum(:each_row) unless block_given?
@csv.each.with_index(1) do |raw, row|
value = Array.new(@cols_count) do |col_idx|
col = Sheet.int2col(col_idx + 1)
Cell.new(row: row, col: col, value: raw[col_idx])
end
yield Row.new(row: row, value: value)
end
self
end
|