Class: AdvancedAR::BatchMatcher
- Inherits:
-
Object
- Object
- AdvancedAR::BatchMatcher
- 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
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#maps ⇒ Object
readonly
Returns the value of attribute maps.
-
#primary_column ⇒ Object
readonly
Returns the value of attribute primary_column.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
Instance Method Summary collapse
- #get_for_row(row) ⇒ Object
- #get_for_row!(row) ⇒ Object
- #get_primary_for_row(row) ⇒ Object
- #get_primary_for_row!(row) ⇒ Object
-
#initialize(clazz, rows, columns:, polymorphic_on: false, validate_all: true) ⇒ BatchMatcher
constructor
A new instance of BatchMatcher.
-
#should_match?(row) ⇒ Boolean
Returns True if the row has a value for any of this Matcher’s Columns.
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
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
25 26 27 |
# File 'lib/advanced_ar/batch_matcher.rb', line 25 def columns @columns end |
#maps ⇒ Object (readonly)
Returns the value of attribute maps.
25 26 27 |
# File 'lib/advanced_ar/batch_matcher.rb', line 25 def maps @maps end |
#primary_column ⇒ Object (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 |
#rows ⇒ Object (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
64 65 66 |
# File 'lib/advanced_ar/batch_matcher.rb', line 64 def should_match?(row) @columns.any? { |col| row[col[0]].present? } end |