lib/flipper/adapters/mongo.rb in flipper-mongo-1.0.0 vs lib/flipper/adapters/mongo.rb in flipper-mongo-1.1.0
- old
+ new
@@ -5,38 +5,32 @@
module Flipper
module Adapters
class Mongo
include ::Flipper::Adapter
- # Private: The key that stores the set of known features.
- FeaturesKey = :flipper_features
-
- # Public: The name of the adapter.
- attr_reader :name
-
# Public: The name of the collection storing the feature data.
attr_reader :collection
def initialize(collection)
@collection = collection
- @name = :mongo
+ @features_key = :flipper_features
end
# Public: The set of known features.
def features
read_feature_keys
end
# Public: Adds a feature to the set of known features.
def add(feature)
- update FeaturesKey, '$addToSet' => { 'features' => feature.key }
+ update @features_key, '$addToSet' => { 'features' => feature.key }
true
end
# Public: Removes a feature from the set of known features.
def remove(feature)
- update FeaturesKey, '$pull' => { 'features' => feature.key }
+ update @features_key, '$pull' => { 'features' => feature.key }
clear feature
true
end
# Public: Clears all the gate values for a feature.
@@ -82,10 +76,14 @@
}
when :set
update feature.key, '$addToSet' => {
gate.key.to_s => thing.value.to_s,
}
+ when :json
+ update feature.key, '$set' => {
+ gate.key.to_s => Typecast.to_json(thing.value),
+ }
else
unsupported_data_type gate.data_type
end
true
@@ -101,24 +99,32 @@
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 }
+ 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 }
+ update feature.key, '$pull' => {
+ gate.key.to_s => thing.value.to_s,
+ }
+ when :json
+ update feature.key, '$set' => {
+ gate.key.to_s => nil,
+ }
else
unsupported_data_type gate.data_type
end
true
end
private
def read_feature_keys
- find(FeaturesKey).fetch('features') { Set.new }.to_set
+ find(@features_key).fetch('features') { Set.new }.to_set
end
def read_many_features(features)
docs = find_many(features.map(&:key))
result = {}
@@ -165,9 +171,12 @@
case gate.data_type
when :boolean, :integer
doc[gate.key.to_s]
when :set
doc.fetch(gate.key.to_s) { Set.new }.to_set
+ when :json
+ value = doc[gate.key.to_s]
+ Typecast.from_json(value)
else
unsupported_data_type gate.data_type
end
end
result