Class: MrCommon::CSVExporter
- Inherits:
-
Object
- Object
- MrCommon::CSVExporter
- Defined in:
- app/models/mr_common/csv_exporter.rb
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#exportable ⇒ Object
readonly
Returns the value of attribute exportable.
-
#format_rules ⇒ Object
readonly
Returns the value of attribute format_rules.
Instance Method Summary collapse
- #format_row(row) ⇒ Object
- #generate_csv ⇒ Object
-
#initialize(exportable, adapter: nil, format_rules: nil) ⇒ CSVExporter
constructor
A new instance of CSVExporter.
Constructor Details
#initialize(exportable, adapter: nil, format_rules: nil) ⇒ CSVExporter
Returns a new instance of CSVExporter.
9 10 11 12 13 |
# File 'app/models/mr_common/csv_exporter.rb', line 9 def initialize(exportable, adapter: nil, format_rules: nil) @exportable = exportable @adapter = adapter ? adapter.new(exportable) : DefaultCSVExportAdapter.new(exportable) @format_rules = format_rules || {} end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
6 7 8 |
# File 'app/models/mr_common/csv_exporter.rb', line 6 def adapter @adapter end |
#exportable ⇒ Object (readonly)
Returns the value of attribute exportable.
5 6 7 |
# File 'app/models/mr_common/csv_exporter.rb', line 5 def exportable @exportable end |
#format_rules ⇒ Object (readonly)
Returns the value of attribute format_rules.
7 8 9 |
# File 'app/models/mr_common/csv_exporter.rb', line 7 def format_rules @format_rules end |
Instance Method Details
#format_row(row) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'app/models/mr_common/csv_exporter.rb', line 26 def format_row(row) format_rules.each do |column_name, proc| idx = adapter.headers.index(column_name.to_s) row[idx] = proc.call(row[idx]) end row end |
#generate_csv ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'app/models/mr_common/csv_exporter.rb', line 15 def generate_csv CSV.generate do |csv| csv << adapter.headers adapter.collection.each do |item| row = adapter.row_from_item(item) csv << format_row(row) end end end |