From 021434909b1cc97498965ca43ce0ef206659bdb1 Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Mon, 2 Jun 2014 11:33:15 +0900 Subject: [PATCH] Enable to set value to memoized method externally --- lib/memoist.rb | 5 +++++ test/memoist_test.rb | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/lib/memoist.rb b/lib/memoist.rb index dbe6541..8dc99f4 100644 --- a/lib/memoist.rb +++ b/lib/memoist.rb @@ -81,6 +81,11 @@ def flush_cache(*method_names) remove_instance_variable(ivar) if instance_variable_defined?(ivar) end end + + def set_cache(method_name, value) + memoized_ivar = Memoist.memoized_ivar_for(method_name) + instance_variable_set(memoized_ivar, value) + end end def memoize(*method_names) diff --git a/test/memoist_test.rb b/test/memoist_test.rb index 6cd3abb..ca2a649 100644 --- a/test/memoist_test.rb +++ b/test/memoist_test.rb @@ -331,4 +331,11 @@ def test_private_method_memoization assert_equal 1, person.is_developer_calls end + def test_memoization_with_custom_value + person = Person.new + person.set_cache(:is_developer?, "No") + assert_equal "No", person.send(:is_developer?) + assert_equal 0, person.is_developer_calls + end + end