Guest User

Untitled

a guest
Jul 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. #--
  2. # Copyright 2009 by Stefan Rusterholz.
  3. # All rights reserved.
  4. # See LICENSE.txt for permissions.
  5. #++
  6.  
  7.  
  8.  
  9. #--
  10. # Taken from baretest. Do NOT modify this file. Instead modify
  11. # baretest/lib/baretest/utilities.rb and copy back here
  12. #++
  13.  
  14.  
  15.  
  16. module Kernel
  17. # All extensions Kernel#require_path and Kernel#expanded_require_path will try
  18. # for a given filename
  19. RequireExtensions = %w[.rb .so .dll .bundle .dylib]
  20.  
  21. # Require a whole directory
  22. def require_dir(dir, recursive=true)
  23. glob = recursive ? "#{dir}/**/*" : "#{dir}/*"
  24. glob_requirable(glob) do |path| require path end
  25. end
  26.  
  27. # Find all requirable files that match the glob
  28. def glob_requirable(glob, extensions=nil, &block)
  29. extensions = (extensions || ::Kernel::RequireExtensions).join(',')
  30. Dir.glob("{#{$LOAD_PATH.join(',')}}/#{glob}{#{extensions}}", &block)
  31. end
  32.  
  33. # Returns the path to the file require would load, also see Kernel#expanded_require_path
  34. def require_path(name, extensions=nil)
  35. extensions = (extensions || ::Kernel::RequireExtensions).join(',')
  36. Dir.glob("{#{$LOAD_PATH.join(',')}}/#{name}{#{extensions}}") { |path|
  37. return path
  38. }
  39. nil
  40. end
  41.  
  42. # Returns the absolute path to the file require would load, also see Kernel#require_path
  43. def expanded_require_path(name, extensions=nil)
  44. path = require_path(name, extensions)
  45. path && File.expand_path(path)
  46. end
  47.  
  48. # Will load the given file like load (but accepts files without .rb in the end, like require),
  49. # but evaluate it into the module given with the second arg (defaulting to Module.new).
  50. # It uses Kernel#expanded_require_path with '' and '.rb' as extensions to determine the file to
  51. # load uses the returned path for error messages (second argument to Module#modul_eval).
  52. def load_into(name, mod=nil)
  53. mod ||= Module.new
  54. path = expanded_require_path(name, ['', '.rb'])
  55. raise LoadError, "No such file to load -- #{name}" unless path
  56. mod.module_eval(File.read(path), path)
  57.  
  58. mod
  59. end
  60.  
  61. module_function :require_dir,
  62. :glob_requirable,
  63. :require_path,
  64. :expanded_require_path,
  65. :load_into
  66. end
Add Comment
Please, Sign In to add comment