Module: CsvRowModel::Export

Extended by:
ActiveSupport::Concern
Includes:
Attributes, DynamicColumns
Defined in:
lib/csv_row_model/export.rb,
lib/csv_row_model/export/file.rb,
lib/csv_row_model/export/attributes.rb,
lib/csv_row_model/export/file_model.rb,
lib/csv_row_model/export/dynamic_columns.rb

Overview

Include this to with Model to have a RowModel for exporting to CSVs.

Defined Under Namespace

Modules: Attributes, DynamicColumns, FileModel Classes: File

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DynamicColumns

define_dynamic_attribute_method, dynamic_column, #formatted_attributes

Methods included from Attributes

column, define_attribute_method, format_cell, #formatted_attribute, #formatted_attributes, #formatted_attributes_from_column_names

Methods included from Model::Comparison

#eql?, #hash

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

#source_modelObject (readonly)

Returns the value of attribute source_model.



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

def source_model
  @source_model
end

Class Method Details

.setup(csv, context = {}, with_headers: true) ⇒ Object



34
35
36
# File 'lib/csv_row_model/export.rb', line 34

def setup(csv, context={}, with_headers: true)
  csv << headers(context) if with_headers
end

Instance Method Details

#initialize(source_model, context = {}) ⇒ Object

Parameters:

  • source_model (Model)

    object to export to CSV

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


19
20
21
22
# File 'lib/csv_row_model/export.rb', line 19

def initialize(source_model, context={})
  @source_model = source_model
  @context      = OpenStruct.new(context)
end

#to_rowArray

Returns an array of public_send(column_name) of the CSV model.

Returns:

  • (Array)

    an array of public_send(column_name) of the CSV model



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

def to_row
  formatted_attributes.values
end

#to_rowsObject



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

def to_rows
  [to_row]
end