Class: Csvbuilder::Import::File

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks
Includes:
ActiveModel::Validations
Defined in:
lib/csvbuilder/importer/public/import/file.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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 Csvbuilder::Import



25
26
27
28
29
30
31
# File 'lib/csvbuilder/importer/public/import/file.rb', line 25

def initialize(file_path, row_model_class, context = {})
  @csv             = ::Csvbuilder::Import::Csv.new(file_path) # Full namespace provided to avoid confusion with Ruby CSV class.
  @row_model_class = row_model_class
  @context         = context.to_h.symbolize_keys
  @interrupt = false
  reset
end

Instance Attribute Details

#contextObject (readonly)

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



12
13
14
# File 'lib/csvbuilder/importer/public/import/file.rb', line 12

def context
  @context
end

#csvObject (readonly)

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



12
13
14
# File 'lib/csvbuilder/importer/public/import/file.rb', line 12

def csv
  @csv
end

#current_row_modelObject (readonly)

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



12
13
14
# File 'lib/csvbuilder/importer/public/import/file.rb', line 12

def current_row_model
  @current_row_model
end

#indexObject (readonly)

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



12
13
14
# File 'lib/csvbuilder/importer/public/import/file.rb', line 12

def index
  @index
end

#interruptObject (readonly)

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



12
13
14
# File 'lib/csvbuilder/importer/public/import/file.rb', line 12

def interrupt
  @interrupt
end

#previous_row_modelObject (readonly)

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



12
13
14
# File 'lib/csvbuilder/importer/public/import/file.rb', line 12

def previous_row_model
  @previous_row_model
end

#row_model_classObject (readonly)

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



12
13
14
# File 'lib/csvbuilder/importer/public/import/file.rb', line 12

def row_model_class
  @row_model_class
end

Instance Method Details

#abort!Object



82
83
84
85
86
# File 'lib/csvbuilder/importer/public/import/file.rb', line 82

def abort!
  @interrupt = true

  nil
end

#abort?Boolean

Returns true, if the file should abort reading

Returns:

  • (Boolean)

    returns true, if the file should abort reading



78
79
80
# File 'lib/csvbuilder/importer/public/import/file.rb', line 78

def abort?
  interrupt || !valid? || !!current_row_model.try(:abort?)
end

#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?



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/csvbuilder/importer/public/import/file.rb', line 63

def each(context = {})
  return to_enum(__callee__, context) 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

#headersObject



33
34
35
36
# File 'lib/csvbuilder/importer/public/import/file.rb', line 33

def headers
  h = csv.headers
  h.instance_of?(Array) ? h : []
end

#next(context = {}) ⇒ Object

Gets the next row model based on the context



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/csvbuilder/importer/public/import/file.rb', line 47

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
    @index += 1
    @current_row_model = row_model_class.next(self, context)
    @current_row_model = @index = nil if end_of_file?
  end

  current_row_model
end

#resetObject

Resets the file back to the top



39
40
41
42
43
44
# File 'lib/csvbuilder/importer/public/import/file.rb', line 39

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

#skip?Boolean

Returns true, if the file should skip ‘current_row_model`

Returns:

  • (Boolean)

    returns true, if the file should skip ‘current_row_model`



89
90
91
# File 'lib/csvbuilder/importer/public/import/file.rb', line 89

def skip?
  !!current_row_model.try(:skip?)
end