Module: CsvRowModel::Export
- Extended by:
- ActiveSupport::Concern
- Includes:
- DynamicColumns
- Defined in:
- lib/csv_row_model/export.rb,
lib/csv_row_model/export/file.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: DynamicColumns, FileModel Classes: File
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#source_model ⇒ Object
readonly
Returns the value of attribute source_model.
Class Method Summary collapse
- .column(column_name, options = {}) ⇒ Object
-
.define_attribute_method(column_name) ⇒ Object
Define default attribute method for a column.
-
.format_cell(cell, column_name, column_index) ⇒ Object
Safe to override.
- .setup(csv, context = {}, with_headers: true) ⇒ Object
Instance Method Summary collapse
- #initialize(source_model, context = {}) ⇒ Object
-
#to_row ⇒ Array
An array of public_send(column_name) of the CSV model.
- #to_rows ⇒ Object
Methods included from DynamicColumns
define_dynamic_attribute_method, dynamic_column
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
10 11 12 |
# File 'lib/csv_row_model/export.rb', line 10 def context @context end |
#source_model ⇒ Object (readonly)
Returns the value of attribute source_model.
10 11 12 |
# File 'lib/csv_row_model/export.rb', line 10 def source_model @source_model end |
Class Method Details
.column(column_name, options = {}) ⇒ Object
39 40 41 42 |
# File 'lib/csv_row_model/export.rb', line 39 def column(column_name, ={}) super define_attribute_method(column_name) end |
.define_attribute_method(column_name) ⇒ Object
Define default attribute method for a column
46 47 48 49 50 |
# File 'lib/csv_row_model/export.rb', line 46 def define_attribute_method(column_name) define_method(column_name) do self.class.format_cell(source_model.public_send(column_name), column_name, self.class.index(column_name)) end end |
.format_cell(cell, column_name, column_index) ⇒ Object
Safe to override. Method applied to each cell by default
55 56 57 |
# File 'lib/csv_row_model/export.rb', line 55 def format_cell(cell, column_name, column_index) cell end |
.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
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_row ⇒ Array
Returns 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 attributes.values end |
#to_rows ⇒ Object
24 25 26 |
# File 'lib/csv_row_model/export.rb', line 24 def to_rows [to_row] end |