Module: CsvRowModel::Model::Columns

Extended by:
ActiveSupport::Concern
Included in:
CsvRowModel::Model
Defined in:
lib/csv_row_model/model/columns.rb

Constant Summary collapse

VALID_OPTIONS_KEYS =
%i[type parse validate_type default header header_matchs].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.column(column_name, options = {}) ⇒ Object (protected)

Adds column to the row model

if true, it will add the default validation for the given :type (if applicable)

Parameters:

  • column_name (Symbol)

    name of column to add

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

Options Hash (options):

  • :type (class)

    class you want to automatically parse to (by default does nothing, equivalent to String)

  • :parse (Lambda, Proc)

    for parsing the cell

  • :validate_type (Boolean)

    adds a validations within a csv_string_model call.

  • :default (Object)

    default value of the column if it is blank?, can pass Proc

  • :header (String)

    human friendly string of the column name, by default format_header(column_name)

  • :header_matchs (Hash)

    array with string to match cell to find in the row, by default column name

Raises:

  • (ArgumentError)


89
90
91
92
93
94
# File 'lib/csv_row_model/model/columns.rb', line 89

def column(column_name, options={})
  extra_keys = options.keys - VALID_OPTIONS_KEYS
  raise ArgumentError.new("invalid options #{extra_keys}") unless extra_keys.empty?

  merge_columns(column_name.to_sym => options)
end

.column_namesArray<Symbol>

Returns column names for the row model.

Returns:

  • (Array<Symbol>)

    column names for the row model



37
38
39
# File 'lib/csv_row_model/model/columns.rb', line 37

def column_names
  columns.keys
end

.format_header(column_name, context = {}) ⇒ String

Safe to override

Returns:

  • (String)

    formatted header



68
69
70
# File 'lib/csv_row_model/model/columns.rb', line 68

def format_header(column_name, context={})
  column_name
end

.headers(context = {}) ⇒ Array

Returns column headers for the row model.

Parameters:

  • context (Hash, OpenStruct) (defaults to: {})

    name of column to check

Returns:

  • (Array)

    column headers for the row model



61
62
63
# File 'lib/csv_row_model/model/columns.rb', line 61

def headers(context={})
  columns.map { |name, options| options[:header] || format_header(name, context) }
end

.index(column_name) ⇒ Integer

Returns index of the column_name.

Parameters:

  • column_name (Symbol)

    name of column to find index

Returns:

  • (Integer)

    index of the column_name



49
50
51
# File 'lib/csv_row_model/model/columns.rb', line 49

def index(column_name)
  column_names.index column_name
end

.is_column_name?(column_name) ⇒ Boolean

Returns true if it's a column name.

Parameters:

  • column_name (Symbol)

    name of column to check

Returns:

  • (Boolean)

    true if it's a column name



55
56
57
# File 'lib/csv_row_model/model/columns.rb', line 55

def is_column_name? column_name
  column_name.is_a?(Symbol) && index(column_name)
end

.options(column_name) ⇒ Hash

Returns options for the column_name.

Parameters:

  • column_name (Symbol)

    name of column to find option

Returns:

  • (Hash)

    options for the column_name



43
44
45
# File 'lib/csv_row_model/model/columns.rb', line 43

def options(column_name)
  columns[column_name]
end

Instance Method Details

#array_to_block_hash(array, &block) ⇒ Object (protected)



28
29
30
31
32
33
# File 'lib/csv_row_model/model/columns.rb', line 28

def array_to_block_hash(array, &block)
  array
    .zip(
      array.map { |column_name| block.call(column_name) }
    ).to_h
end

#attributesHash

Returns a map of column_name => public_send(column_name).

Returns:

  • (Hash)

    a map of column_name => public_send(column_name)



10
11
12
# File 'lib/csv_row_model/model/columns.rb', line 10

def attributes
  attributes_from_column_names self.class.column_names
end

#attributes_from_column_names(column_names) ⇒ Object (protected)



24
25
26
# File 'lib/csv_row_model/model/columns.rb', line 24

def attributes_from_column_names(column_names)
  array_to_block_hash(column_names) { |column_name| public_send(column_name) }
end

#headersObject



18
19
20
# File 'lib/csv_row_model/model/columns.rb', line 18

def headers
  self.class.headers(context)
end

#to_jsonObject



14
15
16
# File 'lib/csv_row_model/model/columns.rb', line 14

def to_json
  attributes.to_json
end