Class: Flipper::Adapters::Mongo

Inherits:
Object
  • Object
show all
Includes:
Flipper::Adapter
Defined in:
lib/flipper/adapters/mongo.rb

Constant Summary collapse

FeaturesKey =

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

:flipper_features

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ Mongo

Returns a new instance of Mongo.



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

def initialize(collection)
  @collection = collection
  @name = :mongo
end

Instance Attribute Details

#nameObject (readonly)

Public: The name of the adapter.



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

def name
  @name
end

Instance Method Details

#add(feature) ⇒ Object

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



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

def add(feature)
  update FeaturesKey, '$addToSet' => { 'features' => feature.key }
  true
end

#clear(feature) ⇒ Object

Public: Clears all the gate values for a feature.



40
41
42
43
# File 'lib/flipper/adapters/mongo.rb', line 40

def clear(feature)
  delete feature.key
  true
end

#disable(feature, gate, thing) ⇒ Object

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.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/flipper/adapters/mongo.rb', line 93

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) ⇒ Object

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.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/flipper/adapters/mongo.rb', line 69

def enable(feature, gate, thing)
  case gate.data_type
  when :boolean, :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

#featuresObject

Public: The set of known features.



22
23
24
# File 'lib/flipper/adapters/mongo.rb', line 22

def features
  read_feature_keys
end

#get(feature) ⇒ Object

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

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



48
49
50
51
# File 'lib/flipper/adapters/mongo.rb', line 48

def get(feature)
  doc = find(feature.key)
  result_for_feature(feature, doc)
end

#get_allObject



57
58
59
60
# File 'lib/flipper/adapters/mongo.rb', line 57

def get_all
  features = read_feature_keys.map { |key| Flipper::Feature.new(key, self) }
  read_many_features(features)
end

#get_multi(features) ⇒ Object



53
54
55
# File 'lib/flipper/adapters/mongo.rb', line 53

def get_multi(features)
  read_many_features(features)
end

#remove(feature) ⇒ Object

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



33
34
35
36
37
# File 'lib/flipper/adapters/mongo.rb', line 33

def remove(feature)
  update FeaturesKey, '$pull' => { 'features' => feature.key }
  clear feature
  true
end