class Flipper::Adapters::Mongo

Constants

FeaturesKey

Private: The key that stores the set of known features.

Attributes

collection[R]

Public: The name of the collection storing the feature data.

name[R]

Public: The name of the adapter.

Public Class Methods

new(collection) click to toggle source
# File lib/flipper/adapters/mongo.rb, line 19
def initialize(collection)
  @collection = collection
  @name = :mongo
end

Public Instance Methods

add(feature) click to toggle source

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

# File lib/flipper/adapters/mongo.rb, line 30
def add(feature)
  update FeaturesKey, '$addToSet' => { 'features' => feature.key }
  true
end
clear(feature) click to toggle source

Public: Clears all the gate values for a feature.

# File lib/flipper/adapters/mongo.rb, line 43
def clear(feature)
  delete feature.key
  true
end
disable(feature, gate, thing) click to toggle source

Public: Disables a gate for a given thing.

feature - The Flipper::Feature for the gate. gate - The Flipper::Gate to disable. thing - The Flipper::Type being disabled for the gate.

Returns true.

# File lib/flipper/adapters/mongo.rb, line 101
def disable(feature, gate, thing)
  case gate.data_type
  when :boolean
    delete feature.key
  when :integer
    update feature.key, '$set' => { gate.key.to_s => thing.value.to_s }
  when :set
    update feature.key, '$pull' => { gate.key.to_s => thing.value.to_s }
  else
    unsupported_data_type gate.data_type
  end

  true
end
enable(feature, gate, thing) click to toggle source

Public: Enables a gate for a given thing.

feature - The Flipper::Feature for the gate. gate - The Flipper::Gate to disable. thing - The Flipper::Type being disabled for the gate.

Returns true.

# File lib/flipper/adapters/mongo.rb, line 72
def enable(feature, gate, thing)
  case gate.data_type
  when :boolean
    clear(feature)
    update feature.key, '$set' => {
      gate.key.to_s => thing.value.to_s,
    }
  when :integer
    update feature.key, '$set' => {
      gate.key.to_s => thing.value.to_s,
    }
  when :set
    update feature.key, '$addToSet' => {
      gate.key.to_s => thing.value.to_s,
    }
  else
    unsupported_data_type gate.data_type
  end

  true
end
features() click to toggle source

Public: The set of known features.

# File lib/flipper/adapters/mongo.rb, line 25
def features
  read_feature_keys
end
get(feature) click to toggle source

Public: Gets the values for all gates for a given feature.

Returns a Hash of Flipper::Gate#key => value.

# File lib/flipper/adapters/mongo.rb, line 51
def get(feature)
  doc = find(feature.key)
  result_for_feature(feature, doc)
end
get_all() click to toggle source
# File lib/flipper/adapters/mongo.rb, line 60
def get_all
  features = read_feature_keys.map { |key| Flipper::Feature.new(key, self) }
  read_many_features(features)
end
get_multi(features) click to toggle source
# File lib/flipper/adapters/mongo.rb, line 56
def get_multi(features)
  read_many_features(features)
end
remove(feature) click to toggle source

Public: Removes a feature from the set of known features.

# File lib/flipper/adapters/mongo.rb, line 36
def remove(feature)
  update FeaturesKey, '$pull' => { 'features' => feature.key }
  clear feature
  true
end

Private Instance Methods

delete(key) click to toggle source

Private

# File lib/flipper/adapters/mongo.rb, line 157
def delete(key)
  @collection.find(_id: key.to_s).delete_one
end
find(key) click to toggle source

Private

# File lib/flipper/adapters/mongo.rb, line 137
def find(key)
  @collection.find(_id: key.to_s).limit(1).first || {}
end
find_many(keys) click to toggle source
# File lib/flipper/adapters/mongo.rb, line 141
def find_many(keys)
  docs = @collection.find(_id: { '$in' => keys }).to_a
  result = Hash.new { |hash, key| hash[key] = {} }
  docs.each do |doc|
    result[doc['_id']] = doc
  end
  result
end
read_feature_keys() click to toggle source
# File lib/flipper/adapters/mongo.rb, line 118
def read_feature_keys
  find(FeaturesKey).fetch('features') { Set.new }.to_set
end
read_many_features(features) click to toggle source
# File lib/flipper/adapters/mongo.rb, line 122
def read_many_features(features)
  docs = find_many(features.map(&:key))
  result = {}
  features.each do |feature|
    result[feature.key] = result_for_feature(feature, docs[feature.key])
  end
  result
end
result_for_feature(feature, doc) click to toggle source
# File lib/flipper/adapters/mongo.rb, line 161
def result_for_feature(feature, doc)
  result = {}
  feature.gates.each do |gate|
    result[gate.key] =
      case gate.data_type
      when :boolean, :integer
        doc[gate.key.to_s]
      when :set
        doc.fetch(gate.key.to_s) { Set.new }.to_set
      else
        unsupported_data_type gate.data_type
      end
  end
  result
end
unsupported_data_type(data_type) click to toggle source

Private

# File lib/flipper/adapters/mongo.rb, line 132
def unsupported_data_type(data_type)
  raise "#{data_type} is not supported by this adapter"
end
update(key, updates) click to toggle source

Private

# File lib/flipper/adapters/mongo.rb, line 151
def update(key, updates)
  options = { upsert: true }
  @collection.find(_id: key.to_s).update_one(updates, options)
end