class CsvRowModel::Import::File

Represents a csv file and handles parsing to return ‘Import`

Attributes

context[R]

@return [Hash] context passed to the {Import}

csv[R]

@return [Csv]

current_row_model[R]

@return [Input] the current row model set by {#next}

index[R]

Current index of the row model @return [Integer] returns -1 = start of file, 0 to infinity = index of row_model, nil = end of file, no row_model

previous_row_model[R]

@return [Input] the previous row model set by {#next}

row_model_class[R]

@return [Input] model class returned for importing

Public Class Methods

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

@param [String] file_path path of csv file @param [Import] row_model_class model class returned for importing @param context [Hash] context passed to the {Import}

# File lib/csv_row_model/import/file.rb, line 31
def initialize(file_path, row_model_class, context={})
  @csv, @row_model_class, @context = Csv.new(file_path), row_model_class, context.to_h.symbolize_keys
  reset
end

Public Instance Methods

each(context={}) { |current_row_model| ... } click to toggle source

Iterates through the entire csv file and provides the ‘current_row_model` in a block, while handing aborts and skips via. calling {Model#abort?} and {Model#skip?}

# File lib/csv_row_model/import/file.rb, line 60
def each(context={})
  return to_enum(__callee__) unless block_given?
  return false if _abort?

  while self.next(context)
    run_callbacks :each_iteration do
      return false if _abort?
      next if _skip?

      yield current_row_model
    end
  end
end
next(context={}) click to toggle source

Gets the next row model based on the context

# File lib/csv_row_model/import/file.rb, line 44
def next(context={})
  return if end_of_file?

  run_callbacks :next do
    context = context.to_h.reverse_merge(self.context)
    @previous_row_model = current_row_model
    @current_row_model = row_model_class.next(csv, header, context, previous_row_model)
    @index += 1
    @current_row_model = @index = nil if end_of_file?
  end

  current_row_model
end
reset() click to toggle source

Resets the file back to the top

# File lib/csv_row_model/import/file.rb, line 37
def reset
  csv.reset
  @index = -1
  @current_row_model = nil
end