Skip to content
Open
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
9 changes: 9 additions & 0 deletions lib/memoizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ def self.ivar_name(method_name)

module ClassMethods
def memoize(*method_names)
@memoizer_memoized_methods ||= {}

method_names.each do |method_name|
# If the method is already memoized, don't do anything
if @memoizer_memoized_methods[method_name]
next
else
@memoizer_memoized_methods[method_name] = true
end

memoized_ivar_name = Memoizer.ivar_name(method_name)
unmemoized_method = "_unmemoized_#{method_name}"

Expand Down
2 changes: 1 addition & 1 deletion lib/memoizer/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Memoizer
VERSION = '1.0.3'
VERSION = '1.0.4'
end
13 changes: 13 additions & 0 deletions spec/memoizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class MemoizerSpecClass
def no_params() Date.today; end
def with_params?(ndays, an_array) Date.today + ndays + an_array.length; end
def returning_nil!() Date.today; nil; end
def double_memoized() Date.today; end

def with_hash_parameter(ndays, options = {})
subtract = options.fetch(:subtract, false)
Expand Down Expand Up @@ -69,11 +70,14 @@ def with_hash_and_kwargs(options = {}, **kwargs)
memoize :no_params,
:with_params?,
:returning_nil!,
:double_memoized,
:with_hash_and_kwargs,
:with_hash_parameter,
:with_only_hash_parameter,
:with_kwargs,
:with_only_kwargs

memoize :double_memoized
end
class Beepbop < MemoizerSpecClass; end

Expand All @@ -93,6 +97,15 @@ class Beepbop < MemoizerSpecClass; end
end
end

context "for a double-memoized method" do
it "works the same as a calling memoize once" do
Timecop.freeze(today)
expect(object.double_memoized).to eq(today)
Timecop.freeze(tomorrow)
expect(object.double_memoized).to eq(today)
end
end

context "for a method with params (and ending in ?)" do
it "stores memoized value" do
Timecop.freeze(today)
Expand Down