Class: Kiba::Extend::Destinations::CSV

Inherits:
Object
  • Object
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

Methods included from Destinationable

as_source_class, is_destination?, special_options

Methods included from Registry::Fileable

#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.

Parameters:

  • filename (String)

    path for writing CSV

  • csv_options (Hash) (defaults to: nil)

    options passable to CSV objects. Refer to https://rubyapi.org/2.7/o/csv#method-c-new for details

  • headers (defaults to: nil)

    Don’t use this

  • initial_headers (Array<Symbol>) (defaults to: [])

    names of fields in the order you want them output in the CSV. Any you do not explicitly include here will be appended in whatever order they got created/processed in, to the right of the ones named here. Set in registry entry’s dest_special_opts



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 = headers
  @initial_headers = initial_headers
end

Instance Attribute Details

#csvObject (readonly)

Returns the value of attribute csv.



39
40
41
# File 'lib/kiba/extend/destinations/csv.rb', line 39

def csv
  @csv
end

#csv_optionsObject (readonly)

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

#filenameObject (readonly)

Returns the value of attribute filename.



39
40
41
# File 'lib/kiba/extend/destinations/csv.rb', line 39

def filename
  @filename
end

#headersObject (readonly)

Returns the value of attribute headers.



39
40
41
# File 'lib/kiba/extend/destinations/csv.rb', line 39

def headers
  @headers
end

Class Method Details

.as_source_classObject



14
15
16
# File 'lib/kiba/extend/destinations/csv.rb', line 14

def as_source_class
  Kiba::Extend::Sources::CSV
end

.default_file_optionsObject



18
19
20
# File 'lib/kiba/extend/destinations/csv.rb', line 18

def default_file_options
  Kiba::Extend.csvopts
end

.options_keyObject



22
23
24
# File 'lib/kiba/extend/destinations/csv.rb', line 22

def options_key
  :csv_options
end

.path_keyObject



26
27
28
# File 'lib/kiba/extend/destinations/csv.rb', line 26

def path_key
  :filename
end

.requires_path?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/kiba/extend/destinations/csv.rb', line 30

def requires_path?
  true
end

.special_optionsObject



34
35
36
# File 'lib/kiba/extend/destinations/csv.rb', line 34

def special_options
  [:initial_headers]
end

Instance Method Details

#closeObject



82
83
84
# File 'lib/kiba/extend/destinations/csv.rb', line 82

def close
  csv&.close
end

#fieldsArray<Symbol>

Returns:

  • (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.headers
  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
  verify_initial_headers
  order_headers
  @headers_written ||= begin
    csv << headers
    true
  end
  csv << row.fetch_values(*@headers)
end