rrc.rb

Koichi Sasada, 08/12/2013 03:35 PM

Download (841 Bytes)

 
1

    
2
module RequireRelativeChecker
3
  CHECKED_FEATURES = {}
4
  def require feature
5
    caller_loc = caller_locations(1)[0]
6
    caller_dir = File.dirname(caller_loc.path)
7
    if $LOAD_PATH.include? caller_dir
8
      caller_dir = File.join(caller_dir, File.basename(caller_loc.path, '.rb'))
9
    end
10
    old_features = $LOADED_FEATURES.dup
11
    super
12
    new_features = $LOADED_FEATURES.dup
13
    (new_features - old_features).each{|new_feature|
14
      if new_feature.start_with?(caller_dir) && !CHECKED_FEATURES.include?(new_feature)
15
        CHECKED_FEATURES[new_feature] = true
16
        # p [:begin_with, caller_dir, new_feature]
17
        STDERR.puts "#{caller_loc.path}:#{caller_loc.lineno}: WARNING: Use require_relative() to require #{new_feature}."
18
      end
19
    }
20
  end
21
end
22

    
23
class Object
24
  prepend RequireRelativeChecker
25
end