class Flipper::Adapters::PStore

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

Constants

FeaturesKey

Attributes

name[R]

Public: The name of the adapter.

path[R]

Public: The path to where the file is stored.

thread_safe[R]

Public: PStore's #thread_safe option.

Public Class Methods

new(path = 'flipper.pstore', thread_safe = false) click to toggle source

Public

# File lib/flipper/adapters/pstore.rb, line 23
def initialize(path = 'flipper.pstore', thread_safe = false)
  @path = path
  @store = ::PStore.new(path, thread_safe)
  @name = :pstore
end

Public Instance Methods

add(feature) click to toggle source

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

# File lib/flipper/adapters/pstore.rb, line 37
def add(feature)
  @store.transaction do
    set_add FeaturesKey, feature.key
  end
  true
end
clear(feature) click to toggle source

Public: Clears all the gate values for a feature.

# File lib/flipper/adapters/pstore.rb, line 55
def clear(feature)
  @store.transaction do
    clear_gates(feature)
  end
  true
end
disable(feature, gate, thing) click to toggle source

Public

# File lib/flipper/adapters/pstore.rb, line 99
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) click to toggle source

Public

# File lib/flipper/adapters/pstore.rb, line 83
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
features() click to toggle source

Public: The set of known features.

# File lib/flipper/adapters/pstore.rb, line 30
def features
  @store.transaction do
    read_feature_keys
  end
end
get(feature) click to toggle source

Public

# File lib/flipper/adapters/pstore.rb, line 63
def get(feature)
  @store.transaction do
    result_for_feature(feature)
  end
end
get_all() click to toggle source
# File lib/flipper/adapters/pstore.rb, line 75
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) click to toggle source
# File lib/flipper/adapters/pstore.rb, line 69
def get_multi(features)
  @store.transaction do
    read_many_features(features)
  end
end
inspect() click to toggle source

Public

# File lib/flipper/adapters/pstore.rb, line 119
def inspect
  attributes = [
    "name=#{@name.inspect}",
    "path=#{@path.inspect}",
    "store=#{@store}",
  ]
  "#<#{self.class.name}:#{object_id} #{attributes.join(', ')}>"
end
remove(feature) click to toggle source

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

# File lib/flipper/adapters/pstore.rb, line 46
def remove(feature)
  @store.transaction do
    set_delete FeaturesKey, feature.key
    clear_gates(feature)
  end
  true
end

Private Instance Methods

clear_gates(feature) click to toggle source
# File lib/flipper/adapters/pstore.rb, line 130
def clear_gates(feature)
  feature.gates.each do |gate|
    delete key(feature, gate)
  end
end
delete(key) click to toggle source

Private

# File lib/flipper/adapters/pstore.rb, line 182
def delete(key)
  @store.delete(key.to_s)
end
key(feature, gate) click to toggle source

Private

# File lib/flipper/adapters/pstore.rb, line 167
def key(feature, gate)
  "#{feature.key}/#{gate.key}"
end
read(key) click to toggle source

Private

# File lib/flipper/adapters/pstore.rb, line 172
def read(key)
  @store[key.to_s]
end
read_feature_keys() click to toggle source
# File lib/flipper/adapters/pstore.rb, line 136
def read_feature_keys
  set_members FeaturesKey
end
read_many_features(features) click to toggle source
# File lib/flipper/adapters/pstore.rb, line 140
def read_many_features(features)
  result = {}
  features.each do |feature|
    result[feature.key] = result_for_feature(feature)
  end
  result
end
result_for_feature(feature) click to toggle source
# File lib/flipper/adapters/pstore.rb, line 148
def result_for_feature(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
set_add(key, value) click to toggle source

Private

# File lib/flipper/adapters/pstore.rb, line 187
def set_add(key, value)
  set_members(key) do |members|
    members.add(value.to_s)
  end
end
set_delete(key, value) click to toggle source

Private

# File lib/flipper/adapters/pstore.rb, line 194
def set_delete(key, value)
  set_members(key) do |members|
    members.delete(value.to_s)
  end
end
set_members(key) { |store| ... } click to toggle source

Private

# File lib/flipper/adapters/pstore.rb, line 201
def set_members(key)
  key = key.to_s

  @store[key] ||= Set.new

  if block_given?
    yield @store[key]
  else
    @store[key]
  end
end
write(key, value) click to toggle source

Private

# File lib/flipper/adapters/pstore.rb, line 177
def write(key, value)
  @store[key.to_s] = value.to_s
end