File: eager_load.rb

package info (click to toggle)
ruby-grape 1.6.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,156 kB
  • sloc: ruby: 25,265; makefile: 7
file content (19 lines) | stat: -rw-r--r-- 629 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# frozen_string_literal: true

# Grape uses autoload https://api.rubyonrails.org/classes/ActiveSupport/Autoload.html.
# When a class/module get added to the list, ActiveSupport doesn't check whether it really exists.
# This method loads all classes/modules defined via autoload to be sure only existing
# classes/modules were listed.
def eager_load!(scope = Grape)
  # get modules
  scope.constants.each do |const_name|
    const = scope.const_get(const_name)

    next unless const.respond_to?(:eager_load!)

    const.eager_load!

    # check its modules, they might need to be loaded as well.
    eager_load!(const)
  end
end