Class: Paperclip::AdapterRegistry
- Inherits:
-
Object
- Object
- Paperclip::AdapterRegistry
- Defined in:
- lib/paperclip/io_adapters/registry.rb
Defined Under Namespace
Classes: NoHandlerError
Instance Attribute Summary collapse
-
#registered_handlers ⇒ Object
readonly
Returns the value of attribute registered_handlers.
Instance Method Summary collapse
- #for(target, options = {}) ⇒ Object
- #handler_for(target) ⇒ Object
-
#initialize ⇒ AdapterRegistry
constructor
A new instance of AdapterRegistry.
- #register(handler_class, &block) ⇒ Object
- #registered?(target) ⇒ Boolean
- #unregister(handler_class) ⇒ Object
Constructor Details
#initialize ⇒ AdapterRegistry
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_handlers ⇒ Object (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, = {}) handler_for(target).new(target, ) end |
#handler_for(target) ⇒ Object
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
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 |