Class: Kiba::Extend::Destinations::CSV
- Inherits:
-
Object
- Object
- Kiba::Extend::Destinations::CSV
show all
- Extended by:
- 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
as_source_class, is_destination?, special_options
#default_args, #default_file_options, #labeled_options, #options_key, #path_key, #requires_path?
Constructor Details
#initialize(filename:, csv_options: nil, headers: nil, initial_headers: []) ⇒ CSV
Returns a new instance of CSV.
50
51
52
53
54
55
56
|
# File 'lib/kiba/extend/destinations/csv.rb', line 50
def initialize(filename:, csv_options: nil, headers: nil,
initial_headers: [])
@filename = filename
@csv_options = csv_options || {}
@headers =
@initial_headers =
end
|
Instance Attribute Details
#csv ⇒ Object
Returns the value of attribute csv.
39
40
41
|
# File 'lib/kiba/extend/destinations/csv.rb', line 39
def csv
@csv
end
|
#csv_options ⇒ Object
Returns the value of attribute csv_options.
39
40
41
|
# File 'lib/kiba/extend/destinations/csv.rb', line 39
def csv_options
@csv_options
end
|
#filename ⇒ Object
Returns the value of attribute filename.
39
40
41
|
# File 'lib/kiba/extend/destinations/csv.rb', line 39
def filename
@filename
end
|
Returns the value of attribute headers.
39
40
41
|
# File 'lib/kiba/extend/destinations/csv.rb', line 39
def
@headers
end
|
Class Method Details
.as_source_class ⇒ Object
14
15
16
|
# File 'lib/kiba/extend/destinations/csv.rb', line 14
def as_source_class
Kiba::Extend::Sources::CSV
end
|
.default_file_options ⇒ Object
18
19
20
|
# File 'lib/kiba/extend/destinations/csv.rb', line 18
def default_file_options
Kiba::Extend.csvopts
end
|
.options_key ⇒ Object
22
23
24
|
# File 'lib/kiba/extend/destinations/csv.rb', line 22
def options_key
:csv_options
end
|
.path_key ⇒ Object
26
27
28
|
# File 'lib/kiba/extend/destinations/csv.rb', line 26
def path_key
:filename
end
|
.requires_path? ⇒ Boolean
30
31
32
|
# File 'lib/kiba/extend/destinations/csv.rb', line 30
def requires_path?
true
end
|
.special_options ⇒ Object
34
35
36
|
# File 'lib/kiba/extend/destinations/csv.rb', line 34
def special_options
[:initial_headers]
end
|
Instance Method Details
#close ⇒ Object
82
83
84
|
# File 'lib/kiba/extend/destinations/csv.rb', line 82
def close
csv&.close
end
|
#fields ⇒ Array<Symbol>
59
60
61
62
63
64
65
66
|
# File 'lib/kiba/extend/destinations/csv.rb', line 59
def fields
return [] unless File.exist?(filename)
csv ||= ::CSV.open(filename, "r", **csv_options)
hdrs = csv.shift.
close
hdrs
end
|
#write(row) ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/kiba/extend/destinations/csv.rb', line 69
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
|