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

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

Methods included from Destinationable

#ensure_dir, included, is_destination?

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



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

Instance Attribute Details

#csvObject (readonly)

Returns the value of attribute csv.



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

def csv
  @csv
end

#csv_optionsObject (readonly)

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

#filenameObject (readonly)

Returns the value of attribute filename.



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

def filename
  @filename
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

Class Method Details

.as_source_classObject



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

def as_source_class = Kiba::Extend::Sources::CSV

.default_file_optionsObject



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

def default_file_options = Kiba::Extend.csvopts

.options_keyObject



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

def options_key = :csv_options

.path_keyObject



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

def path_key = :filename

.requires_path?Boolean

Returns:

  • (Boolean)


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

def requires_path? = true

.special_optionsObject



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

def special_options = [:initial_headers]

Instance Method Details

#closeObject



71
72
73
# File 'lib/kiba/extend/destinations/csv.rb', line 71

def close
  csv&.close
end

#fieldsArray<Symbol>

Returns:

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