Sha256: 9499d79e2c1c32a6c1844eb782ebe7616f6f98ad612b44d3c0588c16ba45966f

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

module CanvasSync::JobUniqueness
  module OnConflict
    extend ActiveSupport::Autoload

    autoload :Base

    autoload :Log
    autoload :NullStrategy
    autoload :Raise
    autoload :Reject
    # autoload :Replace
    autoload :Reschedule

    class << self
      def lookup(strategy)
        matching_strategy(strategy.to_s.camelize) ||
          CanvasSync::JobUniqueness.config.conflict_strategies[strategy] ||
          raise(StrategyNotFound, "on_conflict: #{strategy} is not found. Is it declared in the configuration?")
      end

      def validate!(on_conflict, lock_strategy)
        on_conflict = { enqueue: on_conflict, perform: on_conflict } unless on_conflict.is_a?(Hash)

        lock_strategy = Strategy.lookup(lock_strategy) if lock_strategy.is_a?(Symbol)
        on_conflict.each do |origin, strategy|
          strategy = OnConflict.lookup(strategy) if strategy.is_a?(Symbol)

          if lock_strategy.locks_on.include?(origin) && !strategy.valid_for.include?(origin)
            raise ArgumentError, "(#{origin.to_s.titleize}) conflict strategy #{strategy.name.underscore} is not valid for lock strategy #{lock_strategy.name.underscore}"
          end
        end
      end

      private

      def matching_strategy(const)
        const_get(const, false) if const_defined?(const, false)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
canvas_sync-0.22.0.beta1 lib/canvas_sync/job_uniqueness/on_conflict.rb