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
# 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] ||= {}
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



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

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

.default_mappingsObject



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

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



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/canvas_sync/concerns/sync_mapping.rb', line 68

def self.default_v1_mappings
  @legacy_mappings ||= begin
    mapping = YAML.load_file(File.join(__dir__, '../processors', "model_mappings.yml")).deep_symbolize_keys!
    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



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

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

Instance Method Details

#conflict_target(*columns) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/canvas_sync/concerns/sync_mapping.rb', line 82

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


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/canvas_sync/concerns/sync_mapping.rb', line 98

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


90
91
92
# File 'lib/canvas_sync/concerns/sync_mapping.rb', line 90

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


94
95
96
# File 'lib/canvas_sync/concerns/sync_mapping.rb', line 94

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