Class: CanvasSync::Concerns::SyncMapping::Mapping
- Inherits:
 - 
      Object
      
        
- Object
 - CanvasSync::Concerns::SyncMapping::Mapping
 
 
- Defined in:
 - lib/canvas_sync/concerns/sync_mapping.rb
 
Instance Attribute Summary collapse
- 
  
    
      #map_def  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    
Returns the value of attribute map_def.
 
Class Method Summary collapse
- .default_for(key) ⇒ Object
 - .default_mappings ⇒ Object
 - .default_v1_mappings ⇒ Object
 - .normalize_model_name(model) ⇒ Object
 
Instance Method Summary collapse
- #conflict_target(*columns) ⇒ Object
 - 
  
    
      #initialize(map_def, model: nil)  ⇒ Mapping 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    
A new instance of Mapping.
 - #link_column(m = {}, type: nil, **kwargs, &blk) ⇒ Object
 - #reset_links ⇒ Object
 - #unlink_column(key) ⇒ Object
 
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_def ⇒ Object (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_mappings ⇒ Object
      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_mappings ⇒ Object
      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  | 
  
#link_column(m = {}, type: nil, **kwargs, &blk) ⇒ Object
      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  | 
  
#reset_links ⇒ Object
      90 91 92  | 
    
      # File 'lib/canvas_sync/concerns/sync_mapping.rb', line 90 def reset_links @map_def[:columns] = {}.with_indifferent_access end  | 
  
#unlink_column(key) ⇒ Object
      94 95 96  | 
    
      # File 'lib/canvas_sync/concerns/sync_mapping.rb', line 94 def unlink_column(key) @map_def[:columns].delete(key) end  |