Class: CanvasSync::Concerns::SyncMapping::Mapping

Inherits:
Object
  • Object
show all
Defined in:
lib/canvas_sync/concerns/sync_mapping.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(map_def, model: nil) ⇒ Mapping

Returns a new instance of Mapping.



31
32
33
34
35
36
37
# File 'lib/canvas_sync/concerns/sync_mapping.rb', line 31

def initialize(map_def, model: nil)
  @model = model
  @map_def = map_def
  @map_def[:conflict_target] ||= []
  @map_def[:columns] ||= {}
  @map_def[:row_alterations] ||= []
end

Instance Attribute Details

#map_defObject (readonly)

Returns the value of attribute map_def.



29
30
31
# File 'lib/canvas_sync/concerns/sync_mapping.rb', line 29

def map_def
  @map_def
end

Class Method Details

.default_for(key) ⇒ Object



44
45
46
# File 'lib/canvas_sync/concerns/sync_mapping.rb', line 44

def self.default_for(key)
  default_mappings[key]
end

.default_mappingsObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/canvas_sync/concerns/sync_mapping.rb', line 48

def self.default_mappings
  @mappings ||= begin
    maps = {}
    default_v1_mappings.each do |mname, legacy|
      m = maps[mname] = {}

      m[:conflict_target] = Array(legacy[:conflict_target]).map(&:to_sym).map do |lct|
        legacy[:report_columns]&.[](lct)&.[](:database_column_name)
      end

      m[:columns] = {}

      legacy[:report_columns].each do |rcol, opts|
        m[:columns][opts[:database_column_name]] = opts.except(:database_column_name).merge!(
          report_column: rcol,
        ).freeze
      end
    end
    maps.with_indifferent_access.freeze
  end
end

.default_v1_mappingsObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/canvas_sync/concerns/sync_mapping.rb', line 70

def self.default_v1_mappings
  @legacy_mappings ||= begin
    mapping = YAML.load_file(File.join(__dir__, '../processors', "model_mappings.yml")).deep_symbolize_keys!
    # Default columns can be excluded if the table has not been migrated to accept them
    mapping.each do |mname, legacy|
      legacy[:report_columns].select! do |rcol, opts|
        model = mname.to_s&.classify&.safe_constantize
        # we need to make sure this is a model otherwise they will be systematically removed - some mappings are not models (e.g xlists)
        model.present? && !model.column_names.include?(opts[:database_column_name].to_s) ? false : true
      end
    end

    override_filepath = Rails.root.join("config/canvas_sync_provisioning_mapping.yml")

    if File.file?(override_filepath)
      override = YAML.load_file(override_filepath).deep_symbolize_keys!
      mapping = mapping.merge(override)
    end

    mapping.freeze
  end
end

.normalize_model_name(model) ⇒ Object



39
40
41
42
# File 'lib/canvas_sync/concerns/sync_mapping.rb', line 39

def self.normalize_model_name(model)
  model = model.name unless model.is_a?(String)
  model.pluralize.underscore
end

Instance Method Details

#alter_rows(&blk) ⇒ Object



109
110
111
# File 'lib/canvas_sync/concerns/sync_mapping.rb', line 109

def alter_rows(&blk)
  @map_def[:row_alterations] << blk
end

#conflict_target(*columns) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/canvas_sync/concerns/sync_mapping.rb', line 93

def conflict_target(*columns)
  if columns.count == 0
    @map_def[:conflict_target]
  else
    @map_def[:conflict_target] = columns.flatten.compact
  end
end


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/canvas_sync/concerns/sync_mapping.rb', line 113

def link_column(m = {}, type: nil, **kwargs, &blk)
  if m.is_a?(Hash)
    m = m.merge(kwargs)
    raise "Hash should have exactly 1 entry" if m && m.count != 1
    @map_def[:columns][m.values[0]] = {
      report_column: m.keys[0],
      type: type,
      transform: blk,
    }
  elsif m.is_a?(Symbol)
    raise "Unrecognized keyword arguments" if kwargs.present?
    @map_def[:columns][m] = {
      report_column: m,
      type: type,
      transform: blk,
    }
  else
    raise "Cannot handle argument of type #{m.class}"
  end
end


101
102
103
# File 'lib/canvas_sync/concerns/sync_mapping.rb', line 101

def reset_links
  @map_def[:columns] = {}.with_indifferent_access
end


105
106
107
# File 'lib/canvas_sync/concerns/sync_mapping.rb', line 105

def unlink_column(key)
  @map_def[:columns].delete(key)
end