Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement BlocksNotSupported exception #41

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 17 additions & 2 deletions lib/memoist.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'memoist/core_ext/singleton_class'
require 'memoist/blocks_not_supported'

module Memoist

Expand Down Expand Up @@ -114,6 +115,8 @@ def memoize(*method_names)
identifier = method_names.pop[:identifier]
end

memoize_caller = caller(1..1).first

Memoist.memoist_eval(self) do
def self.memoized_methods
@_memoized_methods ||= []
Expand Down Expand Up @@ -157,7 +160,13 @@ def self.memoized_methods
# end

module_eval <<-EOS, __FILE__, __LINE__ + 1
def #{method_name}(reload = false)
def #{method_name}(reload = false, &block)
if block
raise Memoist::BlocksNotSupported,
"Calls with block is not alowed for memoized method #{method_name}\n" \
"memoist called at #{memoize_caller}"
end

skip_cache = reload || !instance_variable_defined?("#{memoized_ivar}")
set_cache = skip_cache && !frozen?

Expand Down Expand Up @@ -199,7 +208,13 @@ def #{method_name}(reload = false)
# end

module_eval <<-EOS, __FILE__, __LINE__ + 1
def #{method_name}(*args)
def #{method_name}(*args, &block)
if block
raise Memoist::BlocksNotSupported,
"Calls with block is not alowed for memoized method #{method_name}\n" \
"memoist called at #{memoize_caller}"
end

reload = Memoist.extract_reload!(method(#{unmemoized_method.inspect}), args)

skip_cache = reload || !(instance_variable_defined?(#{memoized_ivar.inspect}) && #{memoized_ivar} && #{memoized_ivar}.has_key?(args))
Expand Down
4 changes: 4 additions & 0 deletions lib/memoist/blocks_not_supported.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Memoist
# Memoist does not support calls with block to cached method_names
BlocksNotSupported = Class.new(StandardError)
end
16 changes: 16 additions & 0 deletions test/memoist_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -461,4 +461,20 @@ def test_private_method_memoization
assert_equal 1, person.is_developer_calls
end

def test_blocks_supported
person = Person.new

assert_raises(Memoist::BlocksNotSupported) { person.name { :block } }
assert_raises(Memoist::BlocksNotSupported) { person.update('smth') { :block } }
end

def test_block_contains_proper_caller
person = Person.new

begin
person.name { :block }
rescue Memoist::BlocksNotSupported => e
assert(e.message =~ /memoist_test.rb:\d+/, "message should contain caller of memoize")
end
end
end