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 (ie: Hash). Useful for tests/specs.

Constant Summary collapse

FeaturesKey =
:flipper_features

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source = nil) ⇒ Memory

Public



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

def initialize(source = nil)
  @source = source || {}
  @name = :memory
end

Instance Attribute Details

#nameObject (readonly)

Public: The name of the adapter.



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

def name
  @name
end

Instance Method Details

#add(feature) ⇒ Object

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



27
28
29
30
# File 'lib/flipper/adapters/memory.rb', line 27

def add(feature)
  features.add(feature.key)
  true
end

#clear(feature) ⇒ Object

Public: Clears all the gate values for a feature.



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

def clear(feature)
  feature.gates.each do |gate|
    delete key(feature, gate)
  end
  true
end

#delete(key) ⇒ Object

Private



121
122
123
# File 'lib/flipper/adapters/memory.rb', line 121

def delete(key)
  @source.delete(key.to_s)
end

#disable(feature, gate, thing) ⇒ Object

Public



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/flipper/adapters/memory.rb', line 81

def disable(feature, gate, thing)
  case gate.data_type
  when :boolean
    clear(feature)
  when :integer
    write key(feature, gate), thing.value.to_s
  when :set
    set_delete key(feature, gate), thing.value.to_s
  else
    raise "#{gate} is not supported by this adapter yet"
  end

  true
end

#enable(feature, gate, thing) ⇒ Object

Public



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/flipper/adapters/memory.rb', line 67

def enable(feature, gate, thing)
  case gate.data_type
  when :boolean, :integer
    write key(feature, gate), thing.value.to_s
  when :set
    set_add key(feature, gate), thing.value.to_s
  else
    raise "#{gate} is not supported by this adapter yet"
  end

  true
end

#ensure_set_initialized(key) ⇒ Object

Private



144
145
146
# File 'lib/flipper/adapters/memory.rb', line 144

def ensure_set_initialized(key)
  @source[key.to_s] ||= Set.new
end

#featuresObject

Public: The set of known features.



22
23
24
# File 'lib/flipper/adapters/memory.rb', line 22

def features
  set_members(FeaturesKey)
end

#get(feature) ⇒ Object

Public



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/flipper/adapters/memory.rb', line 49

def get(feature)
  result = {}

  feature.gates.each do |gate|
    result[gate.key] = case gate.data_type
    when :boolean, :integer
      read key(feature, gate)
    when :set
      set_members key(feature, gate)
    else
      raise "#{gate} is not supported by this adapter yet"
    end
  end

  result
end

#inspectObject

Public



97
98
99
100
101
102
103
# File 'lib/flipper/adapters/memory.rb', line 97

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

#key(feature, gate) ⇒ Object

Private



106
107
108
# File 'lib/flipper/adapters/memory.rb', line 106

def key(feature, gate)
  "#{feature.key}/#{gate.key}"
end

#read(key) ⇒ Object

Private



111
112
113
# File 'lib/flipper/adapters/memory.rb', line 111

def read(key)
  @source[key.to_s]
end

#remove(feature) ⇒ Object

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



34
35
36
37
38
# File 'lib/flipper/adapters/memory.rb', line 34

def remove(feature)
  features.delete(feature.name.to_s)
  clear(feature)
  true
end

#set_add(key, value) ⇒ Object

Private



126
127
128
129
# File 'lib/flipper/adapters/memory.rb', line 126

def set_add(key, value)
  ensure_set_initialized(key)
  @source[key.to_s].add(value.to_s)
end

#set_delete(key, value) ⇒ Object

Private



132
133
134
135
# File 'lib/flipper/adapters/memory.rb', line 132

def set_delete(key, value)
  ensure_set_initialized(key)
  @source[key.to_s].delete(value.to_s)
end

#set_members(key) ⇒ Object

Private



138
139
140
141
# File 'lib/flipper/adapters/memory.rb', line 138

def set_members(key)
  ensure_set_initialized(key)
  @source[key.to_s]
end

#write(key, value) ⇒ Object

Private



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

def write(key, value)
  @source[key.to_s] = value.to_s
end