Class: Paperclip::AdapterRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/paperclip/io_adapters/registry.rb

Defined Under Namespace

Classes: NoHandlerError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAdapterRegistry

Returns a new instance of AdapterRegistry.



7
8
9
# File 'lib/paperclip/io_adapters/registry.rb', line 7

def initialize
  @registered_handlers = []
end

Instance Attribute Details

#registered_handlersObject (readonly)

Returns the value of attribute registered_handlers.



5
6
7
# File 'lib/paperclip/io_adapters/registry.rb', line 5

def registered_handlers
  @registered_handlers
end

Instance Method Details

#for(target, options = {}) ⇒ Object



32
33
34
# File 'lib/paperclip/io_adapters/registry.rb', line 32

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

#handler_for(target) ⇒ Object

Raises:



19
20
21
22
23
24
# File 'lib/paperclip/io_adapters/registry.rb', line 19

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

#register(handler_class, &block) ⇒ Object



11
12
13
# File 'lib/paperclip/io_adapters/registry.rb', line 11

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

#registered?(target) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/paperclip/io_adapters/registry.rb', line 26

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

#unregister(handler_class) ⇒ Object



15
16
17
# File 'lib/paperclip/io_adapters/registry.rb', line 15

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