Class: Flipper::Adapters::Memoizable

Inherits:
Object
  • 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



21
22
23
24
25
26
# File 'lib/flipper/adapters/memoizable.rb', line 21

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

Instance Attribute Details

#adapterObject (readonly)

Internal: The adapter this adapter is wrapping.



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

def adapter
  @adapter
end

#cacheObject (readonly)

Internal



12
13
14
# File 'lib/flipper/adapters/memoizable.rb', line 12

def cache
  @cache
end

#nameObject (readonly)

Public: The name of the adapter.



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

def name
  @name
end

Instance Method Details

#add(feature) ⇒ Object

Public



40
41
42
43
44
# File 'lib/flipper/adapters/memoizable.rb', line 40

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

#clear(feature) ⇒ Object

Public



57
58
59
60
61
# File 'lib/flipper/adapters/memoizable.rb', line 57

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

#disable(feature, gate, thing) ⇒ Object

Public



80
81
82
83
84
# File 'lib/flipper/adapters/memoizable.rb', line 80

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

#enable(feature, gate, thing) ⇒ Object

Public



73
74
75
76
77
# File 'lib/flipper/adapters/memoizable.rb', line 73

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

#featuresObject

Public



29
30
31
32
33
34
35
36
37
# File 'lib/flipper/adapters/memoizable.rb', line 29

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

#get(feature) ⇒ Object

Public



64
65
66
67
68
69
70
# File 'lib/flipper/adapters/memoizable.rb', line 64

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.



89
90
91
92
# File 'lib/flipper/adapters/memoizable.rb', line 89

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

#memoizing?Boolean

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

Returns:

  • (Boolean)


95
96
97
# File 'lib/flipper/adapters/memoizable.rb', line 95

def memoizing?
  !!@memoize
end

#remove(feature) ⇒ Object

Public



47
48
49
50
51
52
53
54
# File 'lib/flipper/adapters/memoizable.rb', line 47

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