Class: CsvRowModel::Import::File

Inherits:
Object
  • Object
show all
Includes:
Callbacks, Validations
Defined in:
lib/csv_row_model/import/file.rb,
lib/csv_row_model/import/file/callbacks.rb,
lib/csv_row_model/import/file/validations.rb

Overview

Represents a csv file and handles parsing to return Import

Defined Under Namespace

Modules: Callbacks, Validations

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validations

#_abort?, #_skip?, #abort?, #skip?

Methods included from Validators::ValidateAttributes

validate_attributes

Constructor Details

#initialize(file_path, row_model_class, context = {}) ⇒ File

Returns a new instance of File.

Parameters:

  • file_path (String)

    path of csv file

  • row_model_class (Import)

    model class returned for importing

  • context (Hash) (defaults to: {})

    context passed to the CsvRowModel::Import



31
32
33
34
# 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

Instance Attribute Details

#contextHash (readonly)

Returns context passed to the CsvRowModel::Import.

Returns:



24
25
26
# File 'lib/csv_row_model/import/file.rb', line 24

def context
  @context
end

#csvCsv (readonly)

Returns:



12
13
14
# File 'lib/csv_row_model/import/file.rb', line 12

def csv
  @csv
end

#current_row_modelInput (readonly)

Returns the current row model set by #next.

Returns:

  • (Input)

    the current row model set by #next



20
21
22
# File 'lib/csv_row_model/import/file.rb', line 20

def current_row_model
  @current_row_model
end

#indexInteger (readonly)

Current index of the row model

Returns:

  • (Integer)

    returns -1 = start of file, 0 to infinity = index of row_model, nil = end of file, no row_model



18
19
20
# File 'lib/csv_row_model/import/file.rb', line 18

def index
  @index
end

#previous_row_modelInput (readonly)

Returns the previous row model set by #next.

Returns:

  • (Input)

    the previous row model set by #next



22
23
24
# File 'lib/csv_row_model/import/file.rb', line 22

def previous_row_model
  @previous_row_model
end

#row_model_classInput (readonly)

Returns model class returned for importing.

Returns:

  • (Input)

    model class returned for importing



14
15
16
# File 'lib/csv_row_model/import/file.rb', line 14

def row_model_class
  @row_model_class
end

Instance Method Details

#each(context = {}) ⇒ Object

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?



60
61
62
63
64
65
66
67
68
69
70
71
72
# 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 = {}) ⇒ Object

Gets the next row model based on the context



44
45
46
47
48
49
50
51
52
53
54
55
56
# 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

#resetObject

Resets the file back to the top



37
38
39
40
41
# File 'lib/csv_row_model/import/file.rb', line 37

def reset
  csv.reset
  @index = -1
  @current_row_model = nil
end