-
Notifications
You must be signed in to change notification settings - Fork 968
/
autoload.rb
88 lines (82 loc) · 2.47 KB
/
autoload.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# frozen_string_literal: true
module Faraday
# Adds the ability for other modules to manage autoloadable
# constants.
#
# @api private
module AutoloadHelper
# Registers the constants to be auto loaded.
#
# @param prefix [String] The require prefix. If the path is inside Faraday, then
# it will be prefixed with the root path of this loaded Faraday
# version.
# @param options [{ Symbol => String }] library names.
#
# @example
#
# Faraday.autoload_all 'faraday/foo',
# Bar: 'bar'
#
# # requires faraday/foo/bar to load Faraday::Bar.
# Faraday::Bar
#
#
# @return [void]
def autoload_all(prefix, options)
if prefix =~ /^faraday(\/|$)/i
prefix = File.join(Faraday.root_path, prefix)
end
options.each do |const_name, path|
autoload const_name, File.join(prefix, path)
end
end
# Loads each autoloaded constant. If thread safety is a concern,
# wrap this in a Mutex.
#
# @return [void]
def load_autoloaded_constants
constants.each do |const|
const_get(const) if autoload?(const)
end
end
# Filters the module's contents with those that have been already
# autoloaded.
#
# @return [Array<Class, Module>]
def all_loaded_constants
constants.map { |c| const_get(c) }.
select { |a| a.respond_to?(:loaded?) && a.loaded? }
end
end
class Adapter
extend AutoloadHelper
autoload_all 'faraday/adapter',
NetHttp: 'net_http',
NetHttpPersistent: 'net_http_persistent',
EMSynchrony: 'em_synchrony',
EMHttp: 'em_http',
Typhoeus: 'typhoeus',
Patron: 'patron',
Excon: 'excon',
Test: 'test',
Rack: 'rack',
HTTPClient: 'httpclient'
end
class Request
extend AutoloadHelper
autoload_all 'faraday/request',
UrlEncoded: 'url_encoded',
Multipart: 'multipart',
Retry: 'retry',
Authorization: 'authorization',
BasicAuthentication: 'basic_authentication',
TokenAuthentication: 'token_authentication',
Instrumentation: 'instrumentation'
end
class Response
extend AutoloadHelper
autoload_all 'faraday/response',
RaiseError: 'raise_error',
Logger: 'logger'
end
end