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)


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

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



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

def column_names
  columns.keys
end

.columnsHash

Returns column names mapped to their options.

Returns:

  • (Hash)

    column names mapped to their options



33
34
35
# File 'lib/csv_row_model/model/columns.rb', line 33

def columns
  inherited_class_var(:@_columns, {}, :merge)
end

.format_header(column_name) ⇒ String

Safe to override

Returns:

  • (String)

    formatted header



64
65
66
# File 'lib/csv_row_model/model/columns.rb', line 64

def format_header(column_name)
  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



57
58
59
# File 'lib/csv_row_model/model/columns.rb', line 57

def headers(context={})
  @headers ||= columns.map { |name, options| options[:header] || format_header(name) }
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



45
46
47
# File 'lib/csv_row_model/model/columns.rb', line 45

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



51
52
53
# File 'lib/csv_row_model/model/columns.rb', line 51

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

.merge_columns(column_hash) ⇒ Object (protected)



70
71
72
73
74
# File 'lib/csv_row_model/model/columns.rb', line 70

def merge_columns(column_hash)
  @_columns ||= {}
  deep_clear_class_cache(:@_columns)
  @_columns.merge!(column_hash)
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



39
40
41
# File 'lib/csv_row_model/model/columns.rb', line 39

def options(column_name)
  columns[column_name]
end

Instance Method Details

#attributesHash

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

Returns:

  • (Hash)

    a map of column_name => public_send(column_name)



7
8
9
# File 'lib/csv_row_model/model/columns.rb', line 7

def attributes
  attributes_from_column_names self.class.column_names
end

#attributes_from_column_names(column_names) ⇒ Object (protected)



20
21
22
23
24
# File 'lib/csv_row_model/model/columns.rb', line 20

def attributes_from_column_names(column_names)
  column_names
    .zip(column_names.map { |column_name| public_send(column_name) })
    .to_h
end

#headersObject



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

def headers
  self.class.headers(context)
end

#to_jsonObject



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

def to_json
  attributes.to_json
end