Class: Flipper::Adapters::PStore

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

Overview

Public: Adapter based on Ruby’s pstore database. Perfect for when a local file is good enough for storing features.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Flipper::Adapter

#default_config, #export, #import, included, #name

Constructor Details

#initialize(path = 'flipper.pstore', thread_safe = true) ⇒ PStore

Public



17
18
19
20
21
# File 'lib/flipper/adapters/pstore.rb', line 17

def initialize(path = 'flipper.pstore', thread_safe = true)
  @path = path
  @store = ::PStore.new(path, thread_safe)
  @features_key = :flipper_features
end

Instance Attribute Details

#pathObject (readonly)

Public: The path to where the file is stored.



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

def path
  @path
end

Instance Method Details

#add(feature) ⇒ Object

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



31
32
33
34
35
36
# File 'lib/flipper/adapters/pstore.rb', line 31

def add(feature)
  @store.transaction do
    set_add @features_key, feature.key
  end
  true
end

#clear(feature) ⇒ Object

Public: Clears all the gate values for a feature.



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

def clear(feature)
  @store.transaction do
    clear_gates(feature)
  end
  true
end

#disable(feature, gate, thing) ⇒ Object

Public



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/flipper/adapters/pstore.rb', line 98

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

  true
end

#enable(feature, gate, thing) ⇒ Object

Public



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/flipper/adapters/pstore.rb', line 77

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

  true
end

#featuresObject

Public: The set of known features.



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

def features
  @store.transaction do
    read_feature_keys
  end
end

#get(feature) ⇒ Object

Public



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

def get(feature)
  @store.transaction do
    result_for_feature(feature)
  end
end

#get_allObject



69
70
71
72
73
74
# File 'lib/flipper/adapters/pstore.rb', line 69

def get_all
  @store.transaction do
    features = read_feature_keys.map { |key| Flipper::Feature.new(key, self) }
    read_many_features(features)
  end
end

#get_multi(features) ⇒ Object



63
64
65
66
67
# File 'lib/flipper/adapters/pstore.rb', line 63

def get_multi(features)
  @store.transaction do
    read_many_features(features)
  end
end

#inspectObject

Public



122
123
124
125
126
127
128
129
# File 'lib/flipper/adapters/pstore.rb', line 122

def inspect
  attributes = [
    "name=#{@name.inspect}",
    "path=#{@path.inspect}",
    "store=#{@store}",
  ]
  "#<#{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.



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

def remove(feature)
  @store.transaction do
    set_delete @features_key, feature.key
    clear_gates(feature)
  end
  true
end