mirror of
https://github.com/lostisland/faraday.git
synced 2025-10-04 00:02:03 -04:00
reimplement const-lookup from Faraday::RackBuilder::Handler
This commit is contained in:
parent
04b90eb43e
commit
e8382421aa
38
lib/faraday/adapter_registry.rb
Normal file
38
lib/faraday/adapter_registry.rb
Normal file
@ -0,0 +1,38 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'monitor'
|
||||
|
||||
module Faraday
|
||||
# AdapterRegistry registers adapter class names so they can be looked up by a
|
||||
# String or Symbol name.
|
||||
class AdapterRegistry
|
||||
def initialize
|
||||
@lock = Monitor.new
|
||||
@constants = nil
|
||||
end
|
||||
|
||||
def get(name)
|
||||
klass = @constants && @constants[name]
|
||||
return klass if klass
|
||||
|
||||
klass =
|
||||
if name.respond_to?(:constantize)
|
||||
name.constantize
|
||||
else
|
||||
Object.const_get(name)
|
||||
end
|
||||
|
||||
set(klass, name)
|
||||
|
||||
klass
|
||||
end
|
||||
|
||||
def set(klass, name = nil)
|
||||
name ||= klass.to_s
|
||||
@lock.synchronize do
|
||||
@constants ||= {}
|
||||
@constants[name] = klass
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user