Class: Flipper::Adapters::Memoizable

Inherits:
SimpleDelegator
  • Object
show all
Includes:
Flipper::Adapter
Defined in:
lib/flipper/adapters/memoizable.rb

Overview

Internal: Adapter that wraps another adapter with the ability to memoize adapter calls in memory. Used by flipper dsl and the memoizer middleware to make it possible to memoize adapter calls for the duration of a request.

Constant Summary collapse

FeaturesKey =
:flipper_features

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter, cache = nil) ⇒ Memoizable

Public



23
24
25
26
27
28
29
# File 'lib/flipper/adapters/memoizable.rb', line 23

def initialize(adapter, cache = nil)
  super(adapter)
  @adapter = adapter
  @name = :memoizable
  @cache = cache || {}
  @memoize = false
end

Instance Attribute Details

#adapterObject (readonly)

Internal: The adapter this adapter is wrapping.



20
21
22
# File 'lib/flipper/adapters/memoizable.rb', line 20

def adapter
  @adapter
end

#cacheObject (readonly)

Internal



14
15
16
# File 'lib/flipper/adapters/memoizable.rb', line 14

def cache
  @cache
end

#nameObject (readonly)

Public: The name of the adapter.



17
18
19
# File 'lib/flipper/adapters/memoizable.rb', line 17

def name
  @name
end

Instance Method Details

#add(feature) ⇒ Object

Public



43
44
45
46
47
# File 'lib/flipper/adapters/memoizable.rb', line 43

def add(feature)
  result = @adapter.add(feature)
  cache.delete(FeaturesKey) if memoizing?
  result
end

#clear(feature) ⇒ Object

Public



60
61
62
63
64
# File 'lib/flipper/adapters/memoizable.rb', line 60

def clear(feature)
  result = @adapter.clear(feature)
  cache.delete(feature) if memoizing?
  result
end

#disable(feature, gate, thing) ⇒ Object

Public



83
84
85
86
87
# File 'lib/flipper/adapters/memoizable.rb', line 83

def disable(feature, gate, thing)
  result = @adapter.disable(feature, gate, thing)
  cache.delete(feature) if memoizing?
  result
end

#enable(feature, gate, thing) ⇒ Object

Public



76
77
78
79
80
# File 'lib/flipper/adapters/memoizable.rb', line 76

def enable(feature, gate, thing)
  result = @adapter.enable(feature, gate, thing)
  cache.delete(feature) if memoizing?
  result
end

#featuresObject

Public



32
33
34
35
36
37
38
39
40
# File 'lib/flipper/adapters/memoizable.rb', line 32

def features
  if memoizing?
    cache.fetch(FeaturesKey) {
      cache[FeaturesKey] = @adapter.features
    }
  else
    @adapter.features
  end
end

#get(feature) ⇒ Object

Public



67
68
69
70
71
72
73
# File 'lib/flipper/adapters/memoizable.rb', line 67

def get(feature)
  if memoizing?
    cache.fetch(feature) { cache[feature] = @adapter.get(feature) }
  else
    @adapter.get(feature)
  end
end

#memoize=(value) ⇒ Object

Internal: Turns local caching on/off.

value - The Boolean that decides if local caching is on.



92
93
94
95
# File 'lib/flipper/adapters/memoizable.rb', line 92

def memoize=(value)
  cache.clear
  @memoize = value
end

#memoizing?Boolean

Internal: Returns true for using local cache, false for not.

Returns:

  • (Boolean)


98
99
100
# File 'lib/flipper/adapters/memoizable.rb', line 98

def memoizing?
  !!@memoize
end

#remove(feature) ⇒ Object

Public



50
51
52
53
54
55
56
57
# File 'lib/flipper/adapters/memoizable.rb', line 50

def remove(feature)
  result = @adapter.remove(feature)
  if memoizing?
    cache.delete(FeaturesKey)
    cache.delete(feature)
  end
  result
end