1 # Ruby Treasures 0.4
 2 # Copyright (C) 2002 Paul Brannan <paul@atdesk.com>
 3 # 
 4 # You may distribute this software under the same terms as Ruby (see the file
 5 # COPYING that was distributed with this library).
 6 # 
 7 # $Id: loaders.rb,v 1.11 2002/03/28 18:22:28 cout Exp $
 8 
 9 ##
10 # This should be the first file loaded by any user of this library.  If you
11 # don't load it first, then things may not work as expected.
12 #
13 if not defined?(LOADERS_RB) then
14 LOADERS_RB = true
15 
16 begin
17   require 'rbt/hacks/caller_helpers'
18   require 'rbt/hacks/path_operations'
19   require 'rbt/hacks/verbose_block'
20 rescue LoadError
21   # need this for testing
22   require 'hacks/caller_helpers'
23   require 'hacks/path_operations'
24   require 'hacks/verbose_block'
25 end
26 
27 # TODO: If the user decides to chdir before requiring this file, then these
28 # functions will not work.
29 
30 ##
31 # Require a file from the same directory as the caller
32 #
33 def requirelocal(file)
34   caller_file, caller_lineno, caller_method = parse_caller(caller[0])
35   caller_dir = File.dirname(caller_file_pathname(caller_file))
36   require File.join(caller_dir, file)
37 end
38 
39 ##
40 # Load a file from the same directory as the caller
41 #
42 def loadlocal(file)
43   caller_file, caller_lineno, caller_method = parse_caller(caller[0])
44   caller_dir = File.dirname(caller_file_pathname(caller_file))
45   load File.join(caller_dir, file)
46 end
47 
48 #TODO: I'm not sure a way around this problem yet.
49 
50 terse_block do
51 
52 $__old_require__ = method(:require)
53 
54 $".each do |required_file|
55   file = find_file_in_path(required_file)
56   required_file.sub!(/^.*$/, file)
57 end
58 
59 ##
60 # A fixed version of require that uses the full pathname before checking to
61 # see if it has been required.
62 #
63 def require(file)
64   file_to_require = find_file_in_path(file)
65   $__old_require__.call(realpath(file_to_require))
66 end
67 
68 $__old_load__ = method(:load)
69 
70 ##
71 # A fixed version of load that uses the full pathname before checking to
72 # see if it has been loadd.
73 #
74 def load(file)
75   file_to_load = find_file_in_path(file)
76   $__old_load__.call(file_to_load)
77 end
78 
79 end # terse_block
80 
81 end # if not defined?(LOADERS_RB)
82 
83