Skip to content

Commit 7d93617

Browse files
committed
Memoize can be a on it's own line
This allows defining methods like: memoize def foo "string" end This would previous not memoize the method.
1 parent f96c223 commit 7d93617

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/memery.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ def included(base = nil, &block)
3838

3939
module ClassMethods
4040
def memoize(*method_names, condition: nil, ttl: nil)
41+
if method_names.empty?
42+
@_memery_memoize_next_method = { condition: condition, ttl: ttl }
43+
return
44+
end
4145
prepend_memery_module!
4246
method_names.each do |method_name|
4347
define_memoized_method!(method_name, condition: condition, ttl: ttl)
@@ -52,6 +56,15 @@ def memoized?(method_name)
5256
@_memery_module.private_method_defined?(method_name)
5357
end
5458

59+
def method_added(name)
60+
super
61+
62+
return unless @_memery_memoize_next_method
63+
64+
memoize(name, **@_memery_memoize_next_method)
65+
@_memery_memoize_next_method = nil
66+
end
67+
5568
private
5669

5770
def prepend_memery_module!

spec/memery_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ class A
1414
m_private
1515
end
1616

17+
memoize
18+
def m_different_line
19+
CALLS << :m_different_line
20+
:m_different_line
21+
end
22+
1723
def not_memoized; end
1824

1925
memoize def m_nil
@@ -80,6 +86,13 @@ module M
8086
:m
8187
end
8288

89+
memoize
90+
91+
def m_different_line
92+
CALLS << :m_different_line
93+
:m_different_line
94+
end
95+
8396
def not_memoized; end
8497

8598
private
@@ -480,6 +493,12 @@ class H
480493
it { is_expected.to be true }
481494
end
482495

496+
context "memoize is on a different line" do
497+
let(:method_name) { :m_different_line }
498+
499+
it { is_expected.to be true }
500+
end
501+
483502
context "private memoized method" do
484503
let(:method_name) { :m_private }
485504

@@ -516,5 +535,7 @@ class H
516535

517536
include_examples "works correctly"
518537
end
538+
539+
519540
end
520541
end

0 commit comments

Comments
 (0)