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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



9
10
11
# File 'lib/csv_row_model/import/base.rb', line 9

def context
  @context
end

#indexObject (readonly)

Returns the value of attribute index.



9
10
11
# File 'lib/csv_row_model/import/base.rb', line 9

def index
  @index
end

#previousObject (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_headerObject (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_rowObject (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_methodsObject (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.

Parameters:

  • csv (Import::Csv)

    to read from

  • context (Hash) (defaults to: {})

    extra data you want to work with the model

  • prevuous (Import)

    the previous row model

Returns:

  • (Import)

    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_classClass

Returns the Class of the Presenter.

Returns:

  • (Class)

    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.

Returns:

  • (Boolean)

    returns true, if the entire csv file should stop reading



75
76
77
# File 'lib/csv_row_model/import/base.rb', line 75

def abort?
  presenter.abort?
end

#csv_string_modelModel::CsvStringModel

Returns a model with validations related to Model::csv_string_model (values are from format_cell).

Returns:

  • (Model::CsvStringModel)

    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_previousObject

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

Parameters:

  • source_row (Array)

    the csv row

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :index (Integer)

    index in the CSV file

  • :context (Hash)

    extra data you want to work with the model

  • :source_header (Array)

    the csv header row

  • :previous (CsvRowModel::Import)

    the previous row model

  • :parent (CsvRowModel::Import)

    if the instance is a child, pass the parent



22
23
24
25
26
27
28
29
# 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

#mapped_rowHash

Returns a map of column_name => source_row[index_of_column_name].

Returns:

  • (Hash)

    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

#presenterPresenter

Returns the presenter of self.

Returns:



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.

Returns:

  • (Boolean)

    returns true, if this instance should be skipped



68
69
70
# File 'lib/csv_row_model/import/base.rb', line 68

def skip?
  !valid? || presenter.skip?
end

#valid?(*args) ⇒ Boolean

Returns:



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.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