Repository URL to install this package:
|
Version:
0.3.5 ▾
|
# frozen_string_literal: true
require 'set'
require 'datasync/exceptions'
require 'datasync/sync_operation'
module Datasync
# Implicit mapper handles sync between a source and destination, linking on a common field
class LinkTableMapper < Mapper
attr_reader :link_table_proc, :mapping_name
# Specialized SyncOperation which knows how to handle linking / unlinking
class LinkTableSyncOp < SyncOperation
def initialize(mapper)
super
@link_ds = mapper.link_table_proc.call
@mapping_name = mapper.mapping_name
end
def link(source_id, target_id)
@link_ds.insert(mapping: @mapping_name, source_id: source_id, target_id: target_id)
end
def unlink(source_id, target_id)
@link_ds.where(mapping: @mapping_name, source_id: source_id, target_id: target_id).delete
end
def configure_classifier(classifier)
classifier.assume_nothing!
end
def links
@link_ds.where(mapping: @mapping_name).select(:source_id, :target_id).all
end
end
def sync_class
LinkTableSyncOp
end
def option(name, value)
case name
when :link_table then @link_table_proc = value
when :mapping_name then @mapping_name = value
else raise OptionError, "Unknown option #{name}"
end
end
def validate!
super
raise ValidationError, 'Link table must be specified' unless @link_table_proc
raise ValidationError, 'Mapping name must be specified' unless @mapping_name
end
end
end