matthewrudy / memoist

ActiveSupport::Memoizable with a few enhancements
MIT License
920 stars 99 forks source link

Flush Memoized Class Methods #63

Open omgitsbillryan opened 7 years ago

omgitsbillryan commented 7 years ago

Given a class:

  class Foo
    class << self
      extend Memoist

      def bar
        "hello"
      end
      memoize :bar

    end
  end

Foo.flush_cache spits out:

NoMethodError: undefined method `all_memoized_structs' for Foo:Class

I also found this related issue: https://github.com/matthewrudy/memoist/issues/54

RogerPodacter commented 7 years ago

FWIW you can hack around this with something like Foo.remove_instance_variable(:@_memoized_bar).

Not pretty but half a loaf is better than none!

PikachuEXE commented 7 years ago

67 merged

Can this be closed?

omgitsbillryan commented 7 years ago

I believe so. Just ran a manual test:

2.3.0 :001 >   class Foo
2.3.0 :002?>       class << self
2.3.0 :003?>           extend Memoist
2.3.0 :004?>     
2.3.0 :005 >             def bar
2.3.0 :006?>               "hello"
2.3.0 :007?>             end
2.3.0 :008?>           memoize :bar
2.3.0 :009?>     
2.3.0 :010 >           end
2.3.0 :011?>     end
 => :bar 
2.3.0 :012 > Foo.bar
 => "hello" 
2.3.0 :013 > Foo.flush_cache
 => [#<struct Memoist::MemoizedMethod memoized_method=:bar, ivar="@_memoized_bar", arity=0>] 
2.3.0 :014 >