Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Refactor memoized helpers #937

Merged
merged 2 commits into from
Jun 10, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions lib/rspec/core/memoized_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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`.
Copy link
Member

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 the raise line...

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment about super is inaccurate and a bit confusing now.

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
#
Expand Down
18 changes: 18 additions & 0 deletions spec/rspec/core/memoized_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this module have to be included and a let defined? It's unclear. A comment would be nice.


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
Expand Down