Class: CsvRowModel::Import::File
- Inherits:
-
Object
- Object
- CsvRowModel::Import::File
- 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
-
#context ⇒ Hash
readonly
Context passed to the CsvRowModel::Import.
- #csv ⇒ Csv readonly
-
#current_row_model ⇒ Input
readonly
The current row model set by #next.
-
#index ⇒ Integer
readonly
Current index of the row model.
-
#previous_row_model ⇒ Input
readonly
The previous row model set by #next.
-
#row_model_class ⇒ Input
readonly
Model class returned for importing.
Instance Method Summary collapse
-
#each(context = {}) ⇒ Object
Iterates through the entire csv file and provides the
current_row_modelin a block, while handing aborts and skips via. -
#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.
Methods included from Validations
#_abort?, #_skip?, #abort?, #skip?
Methods included from Validators::ValidateAttributes
Constructor Details
#initialize(file_path, row_model_class, context = {}) ⇒ File
Returns a new instance of File.
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
#context ⇒ Hash (readonly)
Returns context passed to the CsvRowModel::Import.
24 25 26 |
# File 'lib/csv_row_model/import/file.rb', line 24 def context @context end |
#current_row_model ⇒ Input (readonly)
Returns 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 |
#index ⇒ Integer (readonly)
Current index of the row model
18 19 20 |
# File 'lib/csv_row_model/import/file.rb', line 18 def index @index end |
#previous_row_model ⇒ Input (readonly)
Returns 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_class ⇒ Input (readonly)
Returns 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 |
#reset ⇒ Object
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 |