Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
uoy-faculty-datasync / lib / datasync / target_link_mapper.rb
Size: Mime:
# 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 TargetLinkMapper < Mapper
    attr_accessor :link_column

    # Specialized SyncOperation which knows how to handle linking / unlinking
    class TargetLinkSyncOp < SyncOperation
      def initialize(mapper)
        super
        @link_column = mapper.link_column
      end

      def link(source_id, target_id)
        target_row(target_id).update(@link_column => source_id)
      end

      def unlink(source_id, target_id)
        target_row(target_id).where(@link_column => source_id).update(@link_column => nil)
      end

      def configure_classifier(classifier)
        classifier.assume_target_exists!
      end

      def links
        @target_data.map do |id, row|
          { target_id: id, source_id: row[@link_column] }
        end
      end
    end

    def sync_class
      TargetLinkSyncOp
    end

    def option(name, value)
      case name
      when :link_column then @link_column = value
      else raise OptionError, "Unknown option #{name}"
      end
    end

    def validate!
      super
      raise ValidationError, 'Link column must be specified' unless @link_column
    end
  end
end