Class: Kiba::Extend::Destinations::CSV
- Inherits:
-
Object
- Object
- Kiba::Extend::Destinations::CSV
show all
- Includes:
- Destinationable
- Defined in:
- lib/kiba/extend/destinations/csv.rb
Overview
An extension of Kiba::Common’s CSV destination, adding the
initial_headers
option
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#ensure_dir, included, is_destination?
Constructor Details
#initialize(filename:, csv_options: nil, headers: nil, initial_headers: []) ⇒ CSV
Returns a new instance of CSV.
38
39
40
41
42
43
44
45
|
# File 'lib/kiba/extend/destinations/csv.rb', line 38
def initialize(filename:, csv_options: nil, headers: nil,
initial_headers: [])
@filename = filename
@csv_options = csv_options || {}
@headers =
@initial_headers =
ensure_dir
end
|
Instance Attribute Details
#csv ⇒ Object
Returns the value of attribute csv.
27
28
29
|
# File 'lib/kiba/extend/destinations/csv.rb', line 27
def csv
@csv
end
|
#csv_options ⇒ Object
Returns the value of attribute csv_options.
27
28
29
|
# File 'lib/kiba/extend/destinations/csv.rb', line 27
def csv_options
@csv_options
end
|
#filename ⇒ Object
Returns the value of attribute filename.
27
28
29
|
# File 'lib/kiba/extend/destinations/csv.rb', line 27
def filename
@filename
end
|
Returns the value of attribute headers.
27
28
29
|
# File 'lib/kiba/extend/destinations/csv.rb', line 27
def
@headers
end
|
Class Method Details
.as_source_class ⇒ Object
14
|
# File 'lib/kiba/extend/destinations/csv.rb', line 14
def as_source_class = Kiba::Extend::Sources::CSV
|
.default_file_options ⇒ Object
16
|
# File 'lib/kiba/extend/destinations/csv.rb', line 16
def default_file_options = Kiba::Extend.csvopts
|
.options_key ⇒ Object
18
|
# File 'lib/kiba/extend/destinations/csv.rb', line 18
def options_key = :csv_options
|
.path_key ⇒ Object
20
|
# File 'lib/kiba/extend/destinations/csv.rb', line 20
def path_key = :filename
|
.requires_path? ⇒ Boolean
22
|
# File 'lib/kiba/extend/destinations/csv.rb', line 22
def requires_path? = true
|
.special_options ⇒ Object
24
|
# File 'lib/kiba/extend/destinations/csv.rb', line 24
def special_options = [:initial_headers]
|
Instance Method Details
#close ⇒ Object
71
72
73
|
# File 'lib/kiba/extend/destinations/csv.rb', line 71
def close
csv&.close
end
|
#fields ⇒ Array<Symbol>
48
49
50
51
52
53
54
55
|
# File 'lib/kiba/extend/destinations/csv.rb', line 48
def fields
return [] unless File.exist?(filename)
csv ||= ::CSV.open(filename, "r", **csv_options)
hdrs = csv.shift.
close
hdrs
end
|
#write(row) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/kiba/extend/destinations/csv.rb', line 58
def write(row)
@csv ||= ::CSV.open(filename, "wb", **csv_options)
@headers ||= row.keys
@headers_written ||= begin
csv <<
true
end
csv << row.fetch_values(*@headers)
end
|