-
-
Notifications
You must be signed in to change notification settings - Fork 104
Add support for optimized requires. #45
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,33 @@ | ||
require "rspec/support/version" | ||
|
||
module RSpec | ||
module Support | ||
# @api private | ||
# | ||
# Defines a helper method that is optimized to require files from the | ||
# named lib. The passed block MUST be `{ |f| require_relative f }` | ||
# because for `require_relative` to work properly from within the named | ||
# lib the line of code must be IN that lib. | ||
# | ||
# `require_relative` is preferred when available because it is always O(1), | ||
# regardless of the number of dirs in $LOAD_PATH. `require`, on the other | ||
# hand, does a linear O(N) search over the dirs in the $LOAD_PATH until | ||
# it can resolve the file relative to one of the dirs. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. great comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A++ would read again |
||
def self.define_optimized_require_for_rspec(lib, &require_relative) | ||
name = "require_rspec_#{lib}" | ||
|
||
if Kernel.respond_to?(:require_relative) | ||
(class << self; self; end).__send__(:define_method, name) do |f| | ||
require_relative.call("#{lib}/#{f}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shadowing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I see your point, but I think I still like |
||
end | ||
else | ||
(class << self; self; end).__send__(:define_method, name) do |f| | ||
require "rspec/#{lib}/#{f}" | ||
end | ||
end | ||
end | ||
|
||
define_optimized_require_for_rspec(:support) { |f| require_relative(f) } | ||
require_rspec_support "version" | ||
|
||
# @api private | ||
KERNEL_METHOD_METHOD = ::Kernel.instance_method(:method) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clear explanation, but that is a weird declaration requirement 😀
Now I know another good use case for the binding_of_caller gem… (but since we control all the code here, there's really no need).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wondered about passing in
__FILE__
and building a require to the absolute path, which should also shortcut the lookup?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that would probably work, but I dislike it for a few versions:
require_relative
is the 1.9+ way of doing this; why re-invent it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Less code to repeat ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but Ok I'm convinced