This repository was archived by the owner on Nov 30, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 753
Refactor memoized helpers #937
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -190,16 +190,12 @@ module ClassMethods | |
# end | ||
# end | ||
def let(name, &block) | ||
# We have to pass the block directly to `define_method` to | ||
# allow it to use method constructs like `super` and `return`. | ||
raise "#let or #subject called without a block" if block.nil? | ||
MemoizedHelpers.module_for(self).send(:define_method, name, &block) | ||
|
||
# Apply the memoization. The method has been defined in an ancestor | ||
# module so we can use `super` here to get the value. | ||
define_method(name) do | ||
__memoized.fetch(name) { |k| __memoized[k] = super(&nil) } | ||
end | ||
MemoizedHelpers.define_memoized_method( | ||
self, | ||
name, | ||
MemoizedHelpers.memoized_method_name_for(name, 'let'), | ||
&block | ||
) | ||
end | ||
|
||
# Just like `let`, except the block is invoked by an implicit `before` | ||
|
@@ -291,10 +287,11 @@ def let!(name, &block) | |
# @see MemoizedHelpers#should | ||
def subject(name=nil, &block) | ||
if name | ||
let(name, &block) | ||
subject_method_name = MemoizedHelpers.memoized_method_name_for(name, "subject") | ||
MemoizedHelpers.define_memoized_method(self, name, subject_method_name, &block) | ||
alias_method :subject, name | ||
|
||
self::NamedSubjectPreventSuper.send(:define_method, name) do | ||
self::NamedSubjectPreventSuper.__send__(:define_method, subject_method_name) do | ||
raise NotImplementedError, "`super` in named subjects is not supported" | ||
end | ||
else | ||
|
@@ -477,6 +474,26 @@ def self.module_for(example_group) | |
end | ||
end | ||
|
||
# @api private | ||
def self.memoized_method_name_for(name, let_or_subject) | ||
"__rspec_#{let_or_subject}_definition_#{name}" | ||
end | ||
|
||
# @api private | ||
def self.define_memoized_method(example_group, name, memoized_method_name, &block) | ||
# We have to pass the block directly to `define_method` to | ||
# allow it to use method constructs like `super` and `return`. | ||
raise "#let or #subject called without a block" if block.nil? | ||
|
||
MemoizedHelpers.module_for(example_group).__send__(:define_method, memoized_method_name, &block) | ||
|
||
# Apply the memoization. The method has been defined in an ancestor | ||
# module so we can use `super` here to get the value. | ||
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. The comment about |
||
example_group.__send__(:define_method, name) do | ||
__memoized.fetch(name) { |k| __memoized[k] = send(memoized_method_name, &nil) } | ||
end | ||
end | ||
|
||
if Module.method(:const_defined?).arity == 1 # for 1.8 | ||
# @api private | ||
# | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -616,6 +616,24 @@ def define_and_run_group | |
}) | ||
end | ||
end | ||
|
||
context "when included modules have hooks that define memoized helpers" do | ||
it "allows memoized helpers to override methods in previously included modules" do | ||
group = ExampleGroup.describe do | ||
include Module.new { | ||
def self.included(m); m.let(:unrelated) { :unrelated }; end | ||
} | ||
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. Why does this module have to be included and a |
||
|
||
include Module.new { | ||
def hello_message; "Hello from module"; end | ||
} | ||
|
||
let(:hello_message) { "Hello from let" } | ||
end | ||
|
||
expect(group.new.hello_message).to eq("Hello from let") | ||
end | ||
end | ||
end | ||
|
||
describe "#let!" do | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This comment would make more sense just above the line that calls
define_method
, rather theraise
line...