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.

Constant Summary collapse

FeaturesKey =
:flipper_features

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Flipper::Adapter

#default_config, #import, included

Constructor Details

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

Public



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

def initialize(path = 'flipper.pstore', thread_safe = false)
  @path = path
  @store = ::PStore.new(path, thread_safe)
  @name = :pstore
end

Instance Attribute Details

#nameObject (readonly)

Public: The name of the adapter.



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

def name
  @name
end

#pathObject (readonly)

Public: The path to where the file is stored.



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

def path
  @path
end

#thread_safeObject (readonly)

Public: PStore’s thread_safe option.



21
22
23
# File 'lib/flipper/adapters/pstore.rb', line 21

def thread_safe
  @thread_safe
end

Instance Method Details

#add(feature) ⇒ Object

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



38
39
40
41
42
43
# File 'lib/flipper/adapters/pstore.rb', line 38

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

#clear(feature) ⇒ Object

Public: Clears all the gate values for a feature.



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

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

#disable(feature, gate, thing) ⇒ Object

Public



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

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
  else
    raise "#{gate} is not supported by this adapter yet"
  end

  true
end

#enable(feature, gate, thing) ⇒ Object

Public



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/flipper/adapters/pstore.rb', line 84

def enable(feature, gate, thing)
  @store.transaction do
    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
  end

  true
end

#featuresObject

Public: The set of known features.



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

def features
  @store.transaction do
    read_feature_keys
  end
end

#get(feature) ⇒ Object

Public



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

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

#get_allObject



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

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



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

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

#inspectObject

Public



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

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.



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

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