Module: CSVUtils::CSVRow::ClassMethods

Defined in:
lib/csv_utils/csv_row.rb

Overview

Class level DSL for declaring columns.

Instance Method Summary collapse

Instance Method Details

#csv_column(header, options = {}) { ... } ⇒ void

This method returns an undefined value.

Declares a column.

Parameters:

  • header (Symbol, String) —

    column name; also the default header and the method that provides the value

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :header (String) —

    header text

  • :method (Symbol) —

    method that provides the value

  • :proc (Proc) —

    evaluated on the object to provide the value

Yields:

  • evaluated on the object to provide the value, like :proc



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/csv_utils/csv_row.rb', line 42

def csv_column(header, options = {}, &block)
  options = options.dup
  options[:header] ||= header.to_s

  if block
    options[:proc] = block
  elsif options[:proc].nil?
    options[:method] ||= header
  end

  add_value_to_class_method(:csv_columns, header => options)
end

#csv_columns ⇒ Hash{Symbol => Hash}

Declared columns, keyed by name, in declaration order.

Returns:

  • (Hash{Symbol => Hash})


30
31
32
# File 'lib/csv_utils/csv_row.rb', line 30

def csv_columns
  {}
end

#csv_headers ⇒ Array<String>

Returns the header of each column.

Returns:

  • (Array<String>) —

    the header of each column



56
57
58
# File 'lib/csv_utils/csv_row.rb', line 56

def csv_headers
  csv_columns.values.map { |column_options| csv_column_header(column_options) }
end