module CsvRowModel::Import::Base
Attributes
Public Class Methods
@param [Array] source_row
the csv row @param options [Hash] @option options [Integer] :index index in the CSV file @option options [Hash] :context extra data you want to work with the model @option options [Array] :source_header the csv header row @option options [CsvRowModel::Import] :previous the previous row model @option options [CsvRowModel::Import] :parent if the instance is a child, pass the parent
# File lib/csv_row_model/import/base.rb, line 22 def initialize(source_row, options={}) options = options.symbolize_keys.reverse_merge(context: {}) @source_row, @context = source_row, OpenStruct.new(options[:context]) @index, @source_header, @previous = options[:index], options[:source_header], options[:previous].try(:dup) previous.try(:free_previous) super(source_row, options) end
Public Instance Methods
Safe to override.
@return [Boolean] returns true, if the entire csv file should stop reading
# File lib/csv_row_model/import/base.rb, line 75 def abort? presenter.abort? end
@return [Model::CsvStringModel] a model with validations related to Model::csv_string_model (values are from format_cell)
# 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` from memory to avoid making a linked list
# File lib/csv_row_model/import/base.rb, line 38 def free_previous @previous = nil end
# 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
@return [Hash] a map of ‘column_name => source_row`
# 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
@param [Import::Csv] csv to read from @param [Hash] context extra data you want to work with the model @param [Import] prevuous the previous row model @return [Import] the next model instance from the csv
# 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
@return [Presenter] the presenter of self
# File lib/csv_row_model/import/base.rb, line 43 def presenter @presenter ||= self.class.presenter_class.new(self) end
@return [Class] the Class of the Presenter
# File lib/csv_row_model/import/base.rb, line 120 def presenter_class @presenter_class ||= inherited_custom_class(:presenter_class, Presenter) end
Safe to override.
@return [Boolean] returns true, if this instance should be skipped
# File lib/csv_row_model/import/base.rb, line 68 def skip? !valid? || presenter.skip? end
# File lib/csv_row_model/import/base.rb, line 79 def valid?(*args) super proc = -> do csv_string_model.valid?(*args) errors.messages.merge!(csv_string_model.errors.messages.reject {|k, v| v.empty? }) errors.empty? end if using_warnings? csv_string_model.using_warnings(&proc) else proc.call end end