Module: CsvRowModel::Export::Attributes

Extended by:
ActiveSupport::Concern
Includes:
Model::Comparison
Included in:
CsvRowModel::Export
Defined in:
lib/csv_row_model/export/attributes.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Model::Comparison

#eql?, #hash

Class Method Details

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

See Model#column



36
37
38
39
# File 'lib/csv_row_model/export/attributes.rb', line 36

def column(column_name, options={})
  super
  define_attribute_method(column_name)
end

.define_attribute_method(column_name) ⇒ Object

Define default attribute method for a column

Parameters:

  • column_name (Symbol)

    the cell's column_name



43
44
45
46
47
# File 'lib/csv_row_model/export/attributes.rb', line 43

def define_attribute_method(column_name)
  define_method(column_name) do
    source_model.public_send(column_name)
  end
end

.format_cell(cell, column_name, column_index, context = {}) ⇒ Object

Safe to override. Method applied to each cell by default

Parameters:

  • cell (Object)

    the cell's value



52
53
54
# File 'lib/csv_row_model/export/attributes.rb', line 52

def format_cell(cell, column_name, column_index, context={})
  cell
end

Instance Method Details

#formatted_attribute(column_name) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/csv_row_model/export/attributes.rb', line 18

def formatted_attribute(column_name)
  return public_send(column_name) if self.class.is_dynamic_column?(column_name)

  self.class.format_cell(
    public_send(column_name),
    column_name,
    self.class.index(column_name),
    context
  )
end

#formatted_attributesHash

Returns a map of column_name => self.class.format_cell(public_send(column_name)).

Returns:

  • (Hash)

    a map of column_name => self.class.format_cell(public_send(column_name))



14
15
16
# File 'lib/csv_row_model/export/attributes.rb', line 14

def formatted_attributes
  formatted_attributes_from_column_names self.class.column_names
end

#formatted_attributes_from_column_names(column_names) ⇒ Object (protected)



30
31
32
# File 'lib/csv_row_model/export/attributes.rb', line 30

def formatted_attributes_from_column_names(column_names)
  array_to_block_hash(column_names) { |column_name| formatted_attribute(column_name) }
end