Module: CsvRowModel::Import

Extended by:
ActiveSupport::Concern
Includes:
Concerns::Inspect, Attributes, DynamicColumns
Defined in:
lib/csv_row_model/import.rb,
lib/csv_row_model/import/csv.rb,
lib/csv_row_model/import/file.rb,
lib/csv_row_model/import/presenter.rb,
lib/csv_row_model/import/attributes.rb,
lib/csv_row_model/import/file_model.rb,
lib/csv_row_model/import/file/callbacks.rb,
lib/csv_row_model/import/dynamic_columns.rb,
lib/csv_row_model/import/file/validations.rb

Overview

Include this to with Model to have a RowModel for importing csvs that represents just one model. It needs CsvRowModel::Import

Defined Under Namespace

Modules: Attributes, DynamicColumns, FileModel Classes: Csv, File, Presenter

Constant Summary

Constants included from Attributes

Attributes::CLASS_TO_PARSE_LAMBDA, Attributes::PARSE_VALIDATION_CLASSES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DynamicColumns

define_dynamic_attribute_method, dynamic_column, dynamic_source_headers, #dynamic_source_headers, #dynamic_source_row, format_dynamic_column_cells, #original_attribute, #original_attributes

Methods included from Attributes

add_type_validation, #default_changes, default_lambda, define_attribute_method, format_cell, #original_attribute, #original_attribute_memoized?, #original_attributes, parse_lambda

Methods included from Model::Comparison

#eql?, #hash

Methods included from Concerns::Inspect

#inspect

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



15
16
17
# File 'lib/csv_row_model/import.rb', line 15

def context
  @context
end

#indexObject (readonly)

Returns the value of attribute index.



15
16
17
# File 'lib/csv_row_model/import.rb', line 15

def index
  @index
end

#previousObject (readonly)

Returns the value of attribute previous.



15
16
17
# File 'lib/csv_row_model/import.rb', line 15

def previous
  @previous
end

#source_headerObject (readonly)

Returns the value of attribute source_header.



15
16
17
# File 'lib/csv_row_model/import.rb', line 15

def source_header
  @source_header
end

#source_rowObject (readonly)

Returns the value of attribute source_row.



15
16
17
# File 'lib/csv_row_model/import.rb', line 15

def source_row
  @source_row
end

Class Method Details

.column(column_name, options = {}) ⇒ Object



104
105
106
107
# File 'lib/csv_row_model/import.rb', line 104

def column(column_name, options={})
  super
  define_attribute_method(column_name)
end

.inspect_methodsObject (protected)



138
139
140
# File 'lib/csv_row_model/import.rb', line 138

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



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/csv_row_model/import.rb', line 113

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



143
144
145
# File 'lib/csv_row_model/import.rb', line 143

def presenter(&block)
  presenter_class.class_eval(&block)
end

.presenter_classClass

Returns the Class of the Presenter.

Returns:

  • (Class)

    the Class of the Presenter



133
134
135
# File 'lib/csv_row_model/import.rb', line 133

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



82
83
84
# File 'lib/csv_row_model/import.rb', line 82

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)



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/csv_row_model/import.rb', line 55

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



45
46
47
# File 'lib/csv_row_model/import.rb', line 45

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



29
30
31
32
33
34
35
36
# File 'lib/csv_row_model/import.rb', line 29

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]



39
40
41
42
# File 'lib/csv_row_model/import.rb', line 39

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:



50
51
52
# File 'lib/csv_row_model/import.rb', line 50

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



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

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

#valid?(*args) ⇒ Boolean

Returns:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/csv_row_model/import.rb', line 86

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