Class: AdvancedAR::BatchMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/advanced_ar/batch_matcher.rb

Overview

Matches a set of rows (eg from a CSV) against Rows in the Database.

Examples:

rule_matcher = BatchMatcher.new(
  Rule, rows,
  validate_all: false,
  columns: [[:rule_id, :id, 'ID'], [:rule_import_id, :import_id, 'Import ID']]
)
context_matcher = BatchMatcher.new(
  [Account, Course], rows,
  polymorphic_on: :rule_context,
  columns: [[:canvas_context_id, :canvas_id, 'Canvas ID'], [:sis_context_id, :sis_id, 'SIS ID']]
)
role_matcher = BatchMatcher.new(
  Role, rows,
  columns: [[:canvas_role_id, :canvas_id, 'Canvas Role ID'], [:role_label, :label, 'Role Label']]
)

Params:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(clazz, rows, columns:, polymorphic_on: false, validate_all: true) ⇒ BatchMatcher

Returns a new instance of BatchMatcher.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/advanced_ar/batch_matcher.rb', line 27

def initialize(clazz, rows, columns:, polymorphic_on: false, validate_all: true)
  # columns: [csv_key, db_key, human_name]
  @options = {
    mode: :eager,
    polymorphic_on: polymorphic_on,
    validate_all: validate_all
  }
  @clazz = clazz
  @columns = columns
  @primary_column = columns[0]
  @rows = rows
  @maps = {}
  @loaded_columns = {}
  @mode = :eager
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



25
26
27
# File 'lib/advanced_ar/batch_matcher.rb', line 25

def columns
  @columns
end

#mapsObject (readonly)

Returns the value of attribute maps.



25
26
27
# File 'lib/advanced_ar/batch_matcher.rb', line 25

def maps
  @maps
end

#primary_columnObject (readonly)

Returns the value of attribute primary_column.



25
26
27
# File 'lib/advanced_ar/batch_matcher.rb', line 25

def primary_column
  @primary_column
end

#rowsObject (readonly)

Returns the value of attribute rows.



25
26
27
# File 'lib/advanced_ar/batch_matcher.rb', line 25

def rows
  @rows
end

Instance Method Details

#get_for_row(row) ⇒ Object



47
48
49
50
51
# File 'lib/advanced_ar/batch_matcher.rb', line 47

def get_for_row(row)
  get_for_row!(row)
rescue ActiveRecord::RecordNotFound
  nil
end

#get_for_row!(row) ⇒ Object



43
44
45
# File 'lib/advanced_ar/batch_matcher.rb', line 43

def get_for_row!(row)
  resolve_row_value(row, :get_by_column)
end

#get_primary_for_row(row) ⇒ Object



57
58
59
60
61
# File 'lib/advanced_ar/batch_matcher.rb', line 57

def get_primary_for_row(row)
  get_primary_for_row!(row)
rescue ActiveRecord::RecordNotFound
  nil
end

#get_primary_for_row!(row) ⇒ Object



53
54
55
# File 'lib/advanced_ar/batch_matcher.rb', line 53

def get_primary_for_row!(row)
  resolve_row_value(row, :column_to_primary_key)
end

#should_match?(row) ⇒ Boolean

Returns True if the row has a value for any of this Matcher’s Columns

Returns:

  • (Boolean)


64
65
66
# File 'lib/advanced_ar/batch_matcher.rb', line 64

def should_match?(row)
  @columns.any? { |col| row[col[0]].present? }
end