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') ⇒ PStore

Public



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

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

Instance Attribute Details

#nameObject (readonly)

Public: The name of the adapter.



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

def name
  @name
end

#pathObject (readonly)

Public: The path to where the file is stored.



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

def path
  @path
end

Instance Method Details

#add(feature) ⇒ Object

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



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

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.



52
53
54
55
56
57
# File 'lib/flipper/adapters/pstore.rb', line 52

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

#disable(feature, gate, thing) ⇒ Object

Public



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/flipper/adapters/pstore.rb', line 96

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



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

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.



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

def features
  @store.transaction do
    read_feature_keys
  end
end

#get(feature) ⇒ Object

Public



60
61
62
63
64
# File 'lib/flipper/adapters/pstore.rb', line 60

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

#get_allObject



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

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



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

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

#inspectObject

Public



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

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.



43
44
45
46
47
48
49
# File 'lib/flipper/adapters/pstore.rb', line 43

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