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 / source_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 SourceLinkMapper < Mapper
    attr_accessor :link_column

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

      def source_row(id)
        @source_ds.where(@source_key => id)
      end

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

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

      def configure_classifier(classifier)
        classifier.assume_source_exists!
      end

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

    def sync_class
      SourceLinkSyncOp
    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