Class: Module

Inherits:
Object
  • Object
show all
Defined in:
lib/module_ext.rb

Overview

TDD helpers for modules.

Defined Under Namespace

Modules: Reloader

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.by_name(name) ⇒ Object

load a module by name.



30
31
32
33
34
35
36
37
# File 'lib/module_ext.rb', line 30

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

Instance Method Details

#etestObject

reloads the module, and runs the module’s etests.



15
16
17
18
# File 'lib/module_ext.rb', line 15

def etest
  reload if respond_to?(:reload)
  ::Etest.run self.const_get("Etest")
end

#instancesObject

returns all instances of a module



22
23
24
25
26
# File 'lib/module_ext.rb', line 22

def instances                                           #:nodoc:
  r = []
  ObjectSpace.each_object(self) { |mod| r << mod }
  r
end

#reloadObject

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”



48
49
50
51
52
53
# File 'lib/module_ext.rb', line 48

def reload
  Module::Reloader.reload_file("#{to_s.underscore}.rb") || begin
    dlog("Cannot reload module #{self}")
    false
  end
end