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.



38
39
40
41
42
43
44
45
# File 'lib/module_ext.rb', line 38

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

#etest(test = nil) ⇒ Object

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



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/module_ext.rb', line 15

def etest(test = nil)
  reload if respond_to?(:reload)
    
  begin
    old_argv = Object.const_get "ARGV"
    
    Object.const_set "ARGV", [ "-n", test.to_s ] if test    # hack!
    ::Etest.run self.const_get("Etest")
  ensure
    Object.const_set "ARGV", old_argv
  end
end

#instancesObject

returns all instances of a module



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

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”



56
57
58
59
60
61
# File 'lib/module_ext.rb', line 56

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