Sha256: 8f0213c0153f10f47ae337b221f3c3f65ccda4a3a157432e3d2c1824065d4ed5

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

module Kernel
  def self.silent(&block)
    old_verbose, $VERBOSE = $VERBOSE, nil
    yield
  ensure
    $VERBOSE = old_verbose
  end
end

# 
# TDD helpers for modules. 
class Module
  #
  # reloads the module, and runs the module's etests.
  def etest(*args)
    reload if respond_to?(:reload)
    ::EtestUnit.run self.const_get("Etest"), *args
  end

  #
  # returns all instances of a module
  def instances                                           #:nodoc:
    r = []
    ObjectSpace.each_object(self) { |mod| r << mod }
    r
  end
  
  #
  # load a module by name. 
  def self.by_name(name)                                  #:nodoc:
    Kernel.silent do
      r = eval(name, nil, __FILE__, __LINE__)
      r if r.is_a?(Module) && r.name == name
    end
  rescue NameError, LoadError
    nil
  end
  
  #
  # tries to reload the source file for this module. THIS IS A DEVELOPMENT
  # helper, don't try to use it in production mode!
  #
  # Limitations:
  #
  # To reload a module with a name of "X::Y" we try to load (in that order) 
  # "x/y.rb", "x.rb"
  #
  def reload
    Module::Reloader.reload_file("#{to_s.underscore}.rb") || begin
      STDERR.puts("Warning: Cannot reload module #{self}")
      false
    end
  end

  module Reloader                                       #:nodoc:
    def self.reload_file(file)
      begin
        load(file) && file
      rescue LoadError
        nfile = file.gsub(/\/[^\/]+\.rb/, ".rb")
        nfile != file && reload_file(nfile)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
etest-unit-0.7.5 lib/module_ext.rb
etest-unit-0.7.4 lib/module_ext.rb
etest-unit-0.7.3 lib/module_ext.rb