Class: CSVPlusPlus::Options

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/csv_plus_plus/options.rb

Overview

The options a user can supply (via CLI flags)

Defined Under Namespace

Classes: OutputFormat

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOptions

Initialize a default Options object



54
55
56
57
58
59
60
61
# File 'lib/csv_plus_plus/options.rb', line 54

def initialize
  @offset = ::T.let([0, 0], ::T::Array[::Integer])
  @create_if_not_exists = ::T.let(false, ::T::Boolean)
  @key_values = ::T.let({}, ::T::Hash[::Symbol, ::String])
  @verbose = ::T.let(false, ::T::Boolean)
  @backup = ::T.let(false, ::T::Boolean)
  @google = ::T.let(nil, ::T.nilable(::CSVPlusPlus::GoogleOptions))
end

Instance Attribute Details

#backupboolean

Create a backup of the spreadsheet before writing

Returns:

  • (boolean)

    the current value of backup



15
16
17
# File 'lib/csv_plus_plus/options.rb', line 15

def backup
  @backup
end

#create_if_not_existsboolean

Create the spreadsheet if it does not exist?

Returns:

  • (boolean)

    the current value of create_if_not_exists



15
16
17
# File 'lib/csv_plus_plus/options.rb', line 15

def create_if_not_exists
  @create_if_not_exists
end

#googleGoogleOptions (readonly)

Options that are specific to the Google Sheets writer

Returns:



15
16
17
# File 'lib/csv_plus_plus/options.rb', line 15

def google
  @google
end

#key_valuesHash

Additional variables that can be supplied to the template

Returns:

  • (Hash)

    the current value of key_values



15
16
17
# File 'lib/csv_plus_plus/options.rb', line 15

def key_values
  @key_values
end

#offsetArray<Integer>

An [x, y] offset (array with two integers)

Returns:

  • (Array<Integer>)

    the current value of offset



15
16
17
# File 'lib/csv_plus_plus/options.rb', line 15

def offset
  @offset
end

#output_filenameString

The file to write our compiled results to

Returns:

  • (String)

    the current value of output_filename



15
16
17
# File 'lib/csv_plus_plus/options.rb', line 15

def output_filename
  @output_filename
end

#sheet_nameString

The name of the spreadsheet to write to

Returns:

  • (String)

    the current value of sheet_name



15
16
17
# File 'lib/csv_plus_plus/options.rb', line 15

def sheet_name
  @sheet_name
end

#verboseboolean

Include extra verbose output?

Returns:

  • (boolean)

    the current value of verbose



15
16
17
# File 'lib/csv_plus_plus/options.rb', line 15

def verbose
  @verbose
end

Instance Method Details

#google_sheet_id=(sheet_id) ⇒ ::String

Set the Google Sheet ID

Parameters:

  • sheet_id (::String)

    The identifier used by Google’s API to reference the sheet. You can find it in the URL for the sheet

Returns:

  • (::String)


70
71
72
# File 'lib/csv_plus_plus/options.rb', line 70

def google_sheet_id=(sheet_id)
  @google = ::CSVPlusPlus::GoogleOptions.new(sheet_id)
end

#output_formatOptions::OutputFormat

Given the options, figure out which type of OutputFormat we’ll be writing to



78
79
80
81
82
83
84
85
86
87
# File 'lib/csv_plus_plus/options.rb', line 78

def output_format
  return ::CSVPlusPlus::Options::OutputFormat::GoogleSheets if @google

  case @output_filename
  when /\.csv$/ then ::CSVPlusPlus::Options::OutputFormat::CSV
  when /\.ods$/ then ::CSVPlusPlus::Options::OutputFormat::OpenDocument
  when /\.xl(sx|sm|tx|tm)$/ then ::CSVPlusPlus::Options::OutputFormat::Excel
  else raise(::CSVPlusPlus::Error::Error, "Unsupported file extension: #{@output_filename}")
  end
end

#validateString?

Returns an error string or nil if there are no validation problems

Returns:

  • (String, nil)


93
94
95
96
97
# File 'lib/csv_plus_plus/options.rb', line 93

def validate
  return if @google || @output_filename

  'You must supply either a Google Sheet ID or an output file'
end

#verbose_summaryString

Return a string with a verbose description of what we’re doing with the options

Returns:

  • (String)


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/csv_plus_plus/options.rb', line 103

def verbose_summary
  "    \#{summary_divider}\n\n    # csv++ Command Options\n\n    > Sheet name                          | \#{@sheet_name}\n    > Create sheet if it does not exist?  | \#{@create_if_not_exists}\n    > Spreadsheet row-offset              | \#{@offset[0]}\n    > Spreadsheet cell-offset             | \#{@offset[1]}\n    > User-supplied key-values            | \#{@key_values}\n    > Verbose                             | \#{@verbose}\n\n    ## Output Options\n\n    > Backup                              | \#{@backup}\n    > Output filename                     | \#{@output_filename}\n\n    \#{@google&.verbose_summary || ''}\n    \#{summary_divider}\n  SUMMARY\nend\n"