Method: Module#module_require

Defined in:
lib/core/facets/module/module_load.rb

#module_require(path) ⇒ Object Also known as: class_require

Require file into module/class namespace.

Unlike load this keeps a per-module cache and will not load the same file into the same module more than once despite repeated attempts.

The cache is kept in a global var called $module_require.

Please use this with careful consideration. It is best suited to loading plugin-type scripts, and should generally not be used as a substitue for Ruby’s standard load system.

NOTE: This method is not a common core extension and is not loaded automatically when using require 'facets'.

CREDIT: Trans

Raises:

  • (LoadError)

Uncommon:

  • require ‘facets/module/module_load’



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
# File 'lib/core/facets/module/module_load.rb', line 53

def module_require( path )
  if path =~ /^[\/~.]/
    file = File.expand_path(path)
  else
    $LOAD_PATH.each do |lp|
      file = File.join(lp,path)
      break if File.exist?(file)
      file += '.rb'
      break if File.exist?(file)
      file = nil
    end
  end
  raise LoadError, "no such file to load -- #{path}" unless file
  # per-module load cache
  $module_require ||= {}
  $module_require[self] ||= {}
  loaded = $module_require[self]
  if loaded.key?(file)
    false
  else
    loaded[file] = true
    script = File.read(file)
    module_eval(script)
    true
  end
end