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

#criteria(key) ⇒ Object

Private



134
135
136
# File 'lib/flipper/adapters/mongo.rb', line 134

def criteria(key)
  {:_id => key.to_s}
end

#delete(key) ⇒ Object

Private



129
130
131
# File 'lib/flipper/adapters/mongo.rb', line 129

def delete(key)
  @collection.find(criteria(key)).delete_one
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.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/flipper/adapters/mongo.rb', line 97

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.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/flipper/adapters/mongo.rb', line 73

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
  find(FeaturesKey).fetch('features') { Set.new }.to_set
end

#find(key) ⇒ Object

Private



118
119
120
# File 'lib/flipper/adapters/mongo.rb', line 118

def find(key)
  @collection.find(criteria(key)).limit(1).first || {}
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
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/flipper/adapters/mongo.rb', line 48

def get(feature)
  result = {}
  doc = find(feature.key)

  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

#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

#unsupported_data_type(data_type) ⇒ Object

Private



113
114
115
# File 'lib/flipper/adapters/mongo.rb', line 113

def unsupported_data_type(data_type)
  raise "#{data_type} is not supported by this adapter"
end

#update(key, updates) ⇒ Object

Private



123
124
125
126
# File 'lib/flipper/adapters/mongo.rb', line 123

def update(key, updates)
  options = {:upsert => true}
  @collection.find(criteria(key)).update_one(updates, options)
end