-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathautoload.rb
38 lines (33 loc) · 1.07 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
# frozen_string_literal: true
module GraphQL
# @see GraphQL::Railtie for automatic Rails integration
module Autoload
# Register a constant named `const_name` to be loaded from `path`.
# This is like `Kernel#autoload` but it tracks the constants so they can be eager-loaded with {#eager_load!}
# @param const_name [Symbol]
# @param path [String]
# @return [void]
def autoload(const_name, path)
@_eagerloaded_constants ||= []
@_eagerloaded_constants << const_name
super const_name, path
end
# Call this to load this constant's `autoload` dependents and continue calling recursively
# @return [void]
def eager_load!
@_eager_loading = true
if @_eagerloaded_constants
@_eagerloaded_constants.each { |const_name| const_get(const_name) }
@_eagerloaded_constants = nil
end
nil
ensure
@_eager_loading = false
end
private
# @return [Boolean] `true` if GraphQL-Ruby is currently eager-loading its constants
def eager_loading?
@_eager_loading ||= false
end
end
end