Class: Flipper::Adapters::Mongo

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Flipper::Adapter

#default_config, #export, #import, included, #name

Constructor Details

#initialize(collection) ⇒ Mongo

Returns a new instance of Mongo.



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

def initialize(collection)
  @collection = collection
  @features_key = :flipper_features
end

Instance Attribute Details

#collectionObject (readonly)

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



11
12
13
# File 'lib/flipper/adapters/mongo.rb', line 11

def collection
  @collection
end

Instance Method Details

#add(feature) ⇒ Object

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



24
25
26
27
# File 'lib/flipper/adapters/mongo.rb', line 24

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

#clear(feature) ⇒ Object

Public: Clears all the gate values for a feature.



37
38
39
40
# File 'lib/flipper/adapters/mongo.rb', line 37

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.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/flipper/adapters/mongo.rb', line 99

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,
    }
  when :json
    update feature.key, '$set' => {
      gate.key.to_s => nil,
    }
  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.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/flipper/adapters/mongo.rb', line 66

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,
    }
  when :json
    update feature.key, '$set' => {
      gate.key.to_s => JSON.dump(thing.value),
    }
  else
    unsupported_data_type gate.data_type
  end

  true
end

#featuresObject

Public: The set of known features.



19
20
21
# File 'lib/flipper/adapters/mongo.rb', line 19

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.



45
46
47
48
# File 'lib/flipper/adapters/mongo.rb', line 45

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

#get_allObject



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

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

#get_multi(features) ⇒ Object



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

def get_multi(features)
  read_many_features(features)
end

#remove(feature) ⇒ Object

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



30
31
32
33
34
# File 'lib/flipper/adapters/mongo.rb', line 30

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