Class: OjSerializers::Memo
- Inherits:
-
Object
- Object
- OjSerializers::Memo
- Defined in:
- lib/oj_serializers/memo.rb
Overview
Internal: Provides a simple API on top of Hash for memoization purposes.
Instance Method Summary collapse
-
#clear ⇒ Object
Internal: Allows to clear the cache when binding the serializer to a different object.
-
#fetch(key) ⇒ Object
Public: Allows to use a simple memoization pattern that also works for falsey values.
-
#initialize ⇒ Memo
constructor
A new instance of Memo.
Constructor Details
#initialize ⇒ Memo
Returns a new instance of Memo.
5 6 7 |
# File 'lib/oj_serializers/memo.rb', line 5 def initialize @cache = {} end |
Instance Method Details
#clear ⇒ Object
Internal: Allows to clear the cache when binding the serializer to a different object.
11 12 13 |
# File 'lib/oj_serializers/memo.rb', line 11 def clear @cache.clear end |
#fetch(key) ⇒ Object
Public: Allows to use a simple memoization pattern that also works for falsey values.
17 18 19 |
# File 'lib/oj_serializers/memo.rb', line 17 def fetch(key) @cache.fetch(key) { @cache[key] = yield } end |