Class: Flipper::Adapters::Memory

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

Overview

Public: Adapter for storing everything in memory. Useful for tests/specs.

Instance Method Summary collapse

Methods included from Flipper::Adapter

#default_config, #export, included, #name

Constructor Details

#initialize(source = nil, threadsafe: true) ⇒ Memory

Public



12
13
14
15
16
# File 'lib/flipper/adapters/memory.rb', line 12

def initialize(source = nil, threadsafe: true)
  @source = Typecast.features_hash(source)
  @lock = Mutex.new if threadsafe
  reset
end

Instance Method Details

#add(feature) ⇒ Object

Public: Adds a feature to the set of known features.



24
25
26
27
# File 'lib/flipper/adapters/memory.rb', line 24

def add(feature)
  synchronize { @source[feature.key] ||= default_config }
  true
end

#clear(feature) ⇒ Object

Public: Clears all the gate values for a feature.



37
38
39
40
# File 'lib/flipper/adapters/memory.rb', line 37

def clear(feature)
  synchronize { @source[feature.key] = default_config }
  true
end

#disable(feature, gate, thing) ⇒ Object

Public



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/flipper/adapters/memory.rb', line 85

def disable(feature, gate, thing)
  synchronize do
    @source[feature.key] ||= default_config

    case gate.data_type
    when :boolean
      @source[feature.key] = default_config
    when :integer
      @source[feature.key][gate.key] = thing.value.to_s
    when :set
      @source[feature.key][gate.key].delete thing.value.to_s
    when :json
      @source[feature.key].delete(gate.key)
    else
      raise "#{gate} is not supported by this adapter yet"
    end

    true
  end
end

#enable(feature, gate, thing) ⇒ Object

Public



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/flipper/adapters/memory.rb', line 62

def enable(feature, gate, thing)
  synchronize do
    @source[feature.key] ||= default_config

    case gate.data_type
    when :boolean
      @source[feature.key] = default_config
      @source[feature.key][gate.key] = thing.value.to_s
    when :integer
      @source[feature.key][gate.key] = thing.value.to_s
    when :set
      @source[feature.key][gate.key] << thing.value.to_s
    when :json
      @source[feature.key][gate.key] = thing.value
    else
      raise "#{gate} is not supported by this adapter yet"
    end

    true
  end
end

#featuresObject

Public: The set of known features.



19
20
21
# File 'lib/flipper/adapters/memory.rb', line 19

def features
  synchronize { @source.keys }.to_set
end

#get(feature) ⇒ Object

Public



43
44
45
# File 'lib/flipper/adapters/memory.rb', line 43

def get(feature)
  synchronize { @source[feature.key] } || default_config
end

#get_allObject



57
58
59
# File 'lib/flipper/adapters/memory.rb', line 57

def get_all
  synchronize { Typecast.features_hash(@source) }
end

#get_multi(features) ⇒ Object



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

def get_multi(features)
  synchronize do
    result = {}
    features.each do |feature|
      result[feature.key] = @source[feature.key] || default_config
    end
    result
  end
end

#import(source) ⇒ Object

Public: a more efficient implementation of import for this adapter



116
117
118
119
120
121
# File 'lib/flipper/adapters/memory.rb', line 116

def import(source)
  adapter = self.class.from(source)
  get_all = Typecast.features_hash(adapter.get_all)
  synchronize { @source.replace(get_all) }
  true
end

#inspectObject

Public



107
108
109
110
111
112
113
# File 'lib/flipper/adapters/memory.rb', line 107

def inspect
  attributes = [
    'name=:memory',
    "source=#{@source.inspect}",
  ]
  "#<#{self.class.name}:#{object_id} #{attributes.join(', ')}>"
end

#remove(feature) ⇒ Object

Public: Removes a feature from the set of known features and clears all the values for the feature.



31
32
33
34
# File 'lib/flipper/adapters/memory.rb', line 31

def remove(feature)
  synchronize { @source.delete(feature.key) }
  true
end