Class: CsvRowModel::Export::File
- Inherits:
-
Object
- Object
- CsvRowModel::Export::File
- Defined in:
- lib/csv_row_model/export/file.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#csv ⇒ Object
readonly
Returns the value of attribute csv.
-
#export_model_class ⇒ Object
readonly
Returns the value of attribute export_model_class.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
Instance Method Summary collapse
-
#append_model(source_model, context = {}) ⇒ Object
(also: #<<)
Add a row_model to the.
-
#generate(with_headers: true) ⇒ Object
Open a block to generate a file.
-
#generated? ⇒ Boolean
True, if a csv file is generated.
- #headers ⇒ Object
-
#initialize(export_model_class, context = {}) ⇒ File
constructor
A new instance of File.
- #to_s ⇒ Object
Constructor Details
#initialize(export_model_class, context = {}) ⇒ File
Returns a new instance of File.
7 8 9 10 |
# File 'lib/csv_row_model/export/file.rb', line 7 def initialize(export_model_class, context={}) @export_model_class = export_model_class @context = context.to_h.symbolize_keys end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
4 5 6 |
# File 'lib/csv_row_model/export/file.rb', line 4 def context @context end |
#csv ⇒ Object (readonly)
Returns the value of attribute csv.
4 5 6 |
# File 'lib/csv_row_model/export/file.rb', line 4 def csv @csv end |
#export_model_class ⇒ Object (readonly)
Returns the value of attribute export_model_class.
4 5 6 |
# File 'lib/csv_row_model/export/file.rb', line 4 def export_model_class @export_model_class end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
4 5 6 |
# File 'lib/csv_row_model/export/file.rb', line 4 def file @file end |
Instance Method Details
#append_model(source_model, context = {}) ⇒ Object Also known as: <<
Add a row_model to the
19 20 21 22 23 |
# File 'lib/csv_row_model/export/file.rb', line 19 def append_model(source_model, context={}) export_model_class.new(source_model, context.to_h.reverse_merge(self.context)).to_rows.each do |row| csv << row end end |
#generate(with_headers: true) ⇒ Object
Open a block to generate a file
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/csv_row_model/export/file.rb', line 33 def generate(with_headers: true) @file = Tempfile.new([export_model_class.name, ".csv"]) CSV.open(file.path, "wb") do |csv| @csv = csv export_model_class.setup(csv, context, with_headers: with_headers) yield self end ensure @csv = nil end |
#generated? ⇒ Boolean
Returns true, if a csv file is generated.
27 28 29 |
# File 'lib/csv_row_model/export/file.rb', line 27 def generated? !!file end |
#headers ⇒ Object
12 13 14 |
# File 'lib/csv_row_model/export/file.rb', line 12 def headers export_model_class.headers end |
#to_s ⇒ Object
44 45 46 |
# File 'lib/csv_row_model/export/file.rb', line 44 def to_s file.read end |