class CsvRowModel::Export::File

Attributes

context[R]
csv[R]
export_model_class[R]
file[R]

Public Class Methods

new(export_model_class, context={}) click to toggle source

@param [Export] export_model export model class

# 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

Public Instance Methods

<<(source_model, context={})
Alias for: append_model
append_model(source_model, context={}) click to toggle source

Add a row_model to the @param [] source_model the source model of the export row model @param [Hash] context the extra context given to the instance of the row model @return [CsvRowModel::Export] the row model appended

# File lib/csv_row_model/export/file.rb, line 20
def append_model(source_model, context={})
  row_model = export_model_class.new(source_model, context.reverse_merge(self.context))
  row_model.to_rows.each do |row|
    csv << row
  end
  row_model
end
Also aliased as: <<
generate(with_headers: true) { |proxy| ... } click to toggle source

Open a block to generate a file @param [Boolean] with_headers adds the header to the file if true

# File lib/csv_row_model/export/file.rb, line 36
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 Proxy.new(self)
  end
ensure
  @csv = nil
end
generated?() click to toggle source

@return [Boolean] true, if a csv file is generated

# File lib/csv_row_model/export/file.rb, line 30
def generated?
  !!file
end
headers() click to toggle source
# File lib/csv_row_model/export/file.rb, line 12
def headers
  export_model_class.headers(self.context)
end
to_s() click to toggle source
# File lib/csv_row_model/export/file.rb, line 47
def to_s
  file.read
end