Sha256: 9b45553675c49f3fe55b977888a1a68be9fd474d9d8f1b8e1722f047530d63e7

Contents?: true

Size: 853 Bytes

Versions: 14

Compression:

Stored size: 853 Bytes

Contents

module Paperclip
  class AdapterRegistry
    class NoHandlerError < Paperclip::Error; end

    attr_reader :registered_handlers

    def initialize
      @registered_handlers = []
    end

    def register(handler_class, &block)
      @registered_handlers << [block, handler_class]
    end

    def unregister(handler_class)
      @registered_handlers.reject! { |_, klass| klass == handler_class }
    end

    def handler_for(target)
      @registered_handlers.each do |tester, handler|
        return handler if tester.call(target)
      end
      raise NoHandlerError.new("No handler found for #{target.inspect}")
    end

    def registered?(target)
      @registered_handlers.any? do |_tester, handler|
        handler === target
      end
    end

    def for(target, options = {})
      handler_for(target).new(target, options)
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
kt-paperclip-7.2.2 lib/paperclip/io_adapters/registry.rb
kt-paperclip-7.2.1 lib/paperclip/io_adapters/registry.rb
kt-paperclip-7.2.0 lib/paperclip/io_adapters/registry.rb
kt-paperclip-6.4.2 lib/paperclip/io_adapters/registry.rb
kt-paperclip-7.1.1 lib/paperclip/io_adapters/registry.rb
kt-paperclip-7.1.0 lib/paperclip/io_adapters/registry.rb
kt-paperclip-7.0.1 lib/paperclip/io_adapters/registry.rb
kt-paperclip-7.0.0 lib/paperclip/io_adapters/registry.rb
kt-paperclip-6.4.1 lib/paperclip/io_adapters/registry.rb
kt-paperclip-6.4.0 lib/paperclip/io_adapters/registry.rb
kt-paperclip-6.3.0 lib/paperclip/io_adapters/registry.rb
kt-paperclip-6.2.2 lib/paperclip/io_adapters/registry.rb
kt-paperclip-6.2.1 lib/paperclip/io_adapters/registry.rb
kt-paperclip-6.2.0 lib/paperclip/io_adapters/registry.rb