Module: CsvRowModel::Import::Base
- Extended by:
- ActiveSupport::Concern
- Included in:
- CsvRowModel::Import
- Defined in:
- lib/csv_row_model/import/base.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#previous ⇒ Object
readonly
Returns the value of attribute previous.
-
#source_header ⇒ Object
readonly
Returns the value of attribute source_header.
-
#source_row ⇒ Object
readonly
Returns the value of attribute source_row.
Class Method Summary collapse
- .inspect_methods ⇒ Object protected
-
.next(csv, source_header, context = {}, previous = nil) ⇒ Import
The next model instance from the csv.
-
.presenter(&block) ⇒ Object
protected
Call to define the presenter.
-
.presenter_class ⇒ Class
The Class of the Presenter.
Instance Method Summary collapse
-
#abort? ⇒ Boolean
Safe to override.
-
#csv_string_model ⇒ Model::CsvStringModel
A model with validations related to Model::csv_string_model (values are from format_cell).
-
#free_previous ⇒ Object
Free
previousfrom memory to avoid making a linked list. - #initialize(source_row, options = {}) ⇒ Object
-
#mapped_row ⇒ Hash
A map of
column_name => source_row[index_of_column_name]. -
#presenter ⇒ Presenter
The presenter of self.
-
#skip? ⇒ Boolean
Safe to override.
- #valid?(*args) ⇒ Boolean
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
9 10 11 |
# File 'lib/csv_row_model/import/base.rb', line 9 def context @context end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
9 10 11 |
# File 'lib/csv_row_model/import/base.rb', line 9 def index @index end |
#previous ⇒ Object (readonly)
Returns the value of attribute previous.
9 10 11 |
# File 'lib/csv_row_model/import/base.rb', line 9 def previous @previous end |
#source_header ⇒ Object (readonly)
Returns the value of attribute source_header.
9 10 11 |
# File 'lib/csv_row_model/import/base.rb', line 9 def source_header @source_header end |
#source_row ⇒ Object (readonly)
Returns the value of attribute source_row.
9 10 11 |
# File 'lib/csv_row_model/import/base.rb', line 9 def source_row @source_row end |
Class Method Details
.inspect_methods ⇒ Object (protected)
125 126 127 |
# File 'lib/csv_row_model/import/base.rb', line 125 def inspect_methods @inspect_methods ||= i[mapped_row initialized_at parent context previous].freeze end |
.next(csv, source_header, context = {}, previous = nil) ⇒ Import
Returns the next model instance from the csv.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/csv_row_model/import/base.rb', line 100 def next(csv, source_header, context={}, previous=nil) csv.skip_header row_model = nil loop do # loop until the next parent or end_of_file? (need to read children rows) csv.read_row row_model ||= new(csv.current_row, index: csv.index, source_header: source_header, context: context, previous: previous) return row_model if csv.end_of_file? next_row_is_parent = !row_model.append_child(csv.next_row) return row_model if next_row_is_parent end end |
.presenter(&block) ⇒ Object (protected)
Call to define the presenter
130 131 132 |
# File 'lib/csv_row_model/import/base.rb', line 130 def presenter(&block) presenter_class.class_eval(&block) end |
.presenter_class ⇒ Class
Returns the Class of the Presenter.
120 121 122 |
# File 'lib/csv_row_model/import/base.rb', line 120 def presenter_class @presenter_class ||= inherited_custom_class(:presenter_class, Presenter) end |
Instance Method Details
#abort? ⇒ Boolean
Safe to override.
75 76 77 |
# File 'lib/csv_row_model/import/base.rb', line 75 def abort? presenter.abort? end |
#csv_string_model ⇒ Model::CsvStringModel
Returns a model with validations related to Model::csv_string_model (values are from format_cell).
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/csv_row_model/import/base.rb', line 48 def csv_string_model @csv_string_model ||= begin if source_row column_names = self.class.column_names hash = column_names.zip( column_names.map.with_index do |column_name, index| self.class.format_cell(source_row[index], column_name, index, context) end ).to_h else hash = {} end self.class.csv_string_model_class.new(hash) end end |
#free_previous ⇒ Object
Free previous from memory to avoid making a linked list
38 39 40 |
# File 'lib/csv_row_model/import/base.rb', line 38 def free_previous @previous = nil end |
#initialize(source_row, options = {}) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/csv_row_model/import/base.rb', line 22 def initialize(source_row, ={}) = .symbolize_keys.reverse_merge(context: {}) @source_row, @context = source_row, OpenStruct.new([:context]) @index, @source_header, @previous = [:index], [:source_header], [:previous].try(:dup) previous.try(:free_previous) super(source_row, ) end |
#mapped_row ⇒ Hash
Returns a map of column_name => source_row[index_of_column_name].
32 33 34 35 |
# File 'lib/csv_row_model/import/base.rb', line 32 def mapped_row return {} unless source_row @mapped_row ||= self.class.column_names.zip(source_row).to_h end |
#presenter ⇒ Presenter
Returns the presenter of self.
43 44 45 |
# File 'lib/csv_row_model/import/base.rb', line 43 def presenter @presenter ||= self.class.presenter_class.new(self) end |
#skip? ⇒ Boolean
Safe to override.
68 69 70 |
# File 'lib/csv_row_model/import/base.rb', line 68 def skip? !valid? || presenter.skip? end |
#valid?(*args) ⇒ Boolean
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/csv_row_model/import/base.rb', line 79 def valid?(*args) super proc = -> do csv_string_model.valid?(*args) errors..merge!(csv_string_model.errors..reject {|k, v| v.empty? }) errors.empty? end if using_warnings? csv_string_model.using_warnings(&proc) else proc.call end end |