Class: Csvbuilder::Import::File
- Inherits:
-
Object
- Object
- Csvbuilder::Import::File
- 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
-
#context ⇒ Object
readonly
-1 = start of file, 0 to infinity = index of row_model, nil = end of file, no row_model.
-
#csv ⇒ Object
readonly
-1 = start of file, 0 to infinity = index of row_model, nil = end of file, no row_model.
-
#current_row_model ⇒ Object
readonly
-1 = start of file, 0 to infinity = index of row_model, nil = end of file, no row_model.
-
#index ⇒ Object
readonly
-1 = start of file, 0 to infinity = index of row_model, nil = end of file, no row_model.
-
#interrupt ⇒ Object
readonly
-1 = start of file, 0 to infinity = index of row_model, nil = end of file, no row_model.
-
#previous_row_model ⇒ Object
readonly
-1 = start of file, 0 to infinity = index of row_model, nil = end of file, no row_model.
-
#row_model_class ⇒ Object
readonly
-1 = start of file, 0 to infinity = index of row_model, nil = end of file, no row_model.
Instance Method Summary collapse
- #abort! ⇒ Object
-
#abort? ⇒ Boolean
Returns true, if the file should abort reading.
-
#each(context = {}) ⇒ Object
Iterates through the entire csv file and provides the ‘current_row_model` in a block, while handing aborts and skips via.
- #headers ⇒ Object
-
#initialize(file_path, row_model_class, context = {}) ⇒ File
constructor
A new instance of File.
-
#next(context = {}) ⇒ Object
Gets the next row model based on the context.
-
#reset ⇒ Object
Resets the file back to the top.
-
#skip? ⇒ Boolean
Returns true, if the file should skip ‘current_row_model`.
Constructor Details
#initialize(file_path, row_model_class, context = {}) ⇒ File
Returns a new instance of File.
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
#context ⇒ Object (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 |
#csv ⇒ Object (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_model ⇒ Object (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 |
#index ⇒ Object (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 |
#interrupt ⇒ Object (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_model ⇒ Object (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_class ⇒ Object (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
91 92 93 94 95 |
# File 'lib/csvbuilder/importer/public/import/file.rb', line 91 def abort! @interrupt = true nil end |
#abort? ⇒ Boolean
Returns true, if the file should abort reading
87 88 89 |
# File 'lib/csvbuilder/importer/public/import/file.rb', line 87 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?
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/csvbuilder/importer/public/import/file.rb', line 72 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 |
#headers ⇒ Object
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 60 61 62 63 64 65 66 67 68 |
# 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) # Check if the headers are valid if previous_row_model.nil? # First row headers_count headers_mismatch abort! if errors.where(:headers).any? end @current_row_model = @index = nil if end_of_file? end current_row_model end |
#reset ⇒ Object
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`
98 99 100 |
# File 'lib/csvbuilder/importer/public/import/file.rb', line 98 def skip? !!current_row_model.try(:skip?) end |