Method: Bundler::Runtime#require

Defined in:
lib/bundler/runtime.rb

#require(*groups) ⇒ Object



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
# File 'lib/bundler/runtime.rb', line 40

def require(*groups)
  groups.map!(&:to_sym)
  groups = [:default] if groups.empty?

  dependencies = @definition.dependencies.select do |dep|
    # Select the dependency if it is in any of the requested groups, and
    # for the current platform, and matches the gem constraints.
    (dep.groups & groups).any? && dep.should_include?
  end

  Plugin.hook(Plugin::Events::GEM_BEFORE_REQUIRE_ALL, dependencies)

  dependencies.each do |dep|
    Plugin.hook(Plugin::Events::GEM_BEFORE_REQUIRE, dep)

    # Loop through all the specified autorequires for the
    # dependency. If there are none, use the dependency's name
    # as the autorequire.
    Array(dep.autorequire || dep.name).each do |file|
      # Allow `require: true` as an alias for `require: <name>`
      file = dep.name if file == true
      required_file = file
      begin
        Kernel.require required_file
      rescue LoadError => e
        if dep.autorequire.nil? && e.path == required_file
          if required_file.include?("-")
            required_file = required_file.tr("-", "/")
            retry
          end
        else
          raise Bundler::GemRequireError.new e,
            "There was an error while trying to load the gem '#{file}'."
        end
      rescue RuntimeError => e
        raise Bundler::GemRequireError.new e,
          "There was an error while trying to load the gem '#{file}'."
      end
    end

    Plugin.hook(Plugin::Events::GEM_AFTER_REQUIRE, dep)
  end

  Plugin.hook(Plugin::Events::GEM_AFTER_REQUIRE_ALL, dependencies)

  dependencies
end