Sha256: 0c0bc497918ed805b658d530fc9a388252d2391832af81d13b481163e10ef015

Contents?: true

Size: 1.8 KB

Versions: 9

Compression:

Stored size: 1.8 KB

Contents

class Module

  # Load file directly into module/class namespace.
  #
  # 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.
  #
  # CREDIT: Trans

  def module_load( 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 = nil
      end
    end
    raise LoadError, "no such file to load -- #{path}" unless file
    module_eval(File.read(file))
  end

  # 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.
  #
  # CREDIT: Trans

  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

  alias_method :class_require, :module_require
  alias_method :class_load, :module_load

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
facets-2.9.0 lib/tour/facets/module/module_load.rb
facets-2.9.0.pre.2 lib/tour/facets/module/module_load.rb
facets-2.9.0.pre.1 lib/tour/facets/module/module_load.rb
facets-2.8.4 lib/core/facets/module/module_load.rb
facets-2.8.3 lib/core/facets/module/module_load.rb
facets-2.8.2 lib/core/facets/module/module_load.rb
facets-2.8.1 lib/core/facets/module/module_load.rb
facets-2.8.0 lib/core/facets/module/module_load.rb
facets-2.7.0 lib/core/facets/module/module_load.rb