Class: OjSerializers::Memo

Inherits:
Object
  • Object
show all
Defined in:
lib/oj_serializers/memo.rb

Overview

Internal: Provides a simple API on top of Hash for memoization purposes.

Instance Method Summary collapse

Constructor Details

#initializeMemo

Returns a new instance of Memo.



5
6
7
# File 'lib/oj_serializers/memo.rb', line 5

def initialize
  @cache = {}
end

Instance Method Details

#clearObject

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