Class: Flipper::Adapters::Sequel
- Inherits:
-
Object
- Object
- Flipper::Adapters::Sequel
- Includes:
- Flipper::Adapter
- Defined in:
- lib/flipper/adapters/sequel.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Public: The name of the adapter.
Instance Method Summary collapse
-
#add(feature) ⇒ Object
Public: Adds a feature to the set of known features.
-
#clear(feature) ⇒ Object
Public: Clears the gate values for a feature.
-
#disable(feature, gate, thing) ⇒ Object
Public: Disables a gate for a given thing.
-
#enable(feature, gate, thing) ⇒ Object
Public: Enables a gate for a given thing.
-
#features ⇒ Object
Public: The set of known features.
-
#get(feature) ⇒ Object
Public: Gets the values for all gates for a given feature.
-
#initialize(options = {}) ⇒ Sequel
constructor
Public: Initialize a new Sequel adapter instance.
-
#remove(feature) ⇒ Object
Public: Removes a feature from the set of known features.
Constructor Details
#initialize(options = {}) ⇒ Sequel
Public: Initialize a new Sequel adapter instance.
name - The Symbol name for this adapter. Optional (default :active_record) feature_class - The AR class responsible for the features table. gate_class - The AR class responsible for the gates table.
Allowing the overriding of name is so you can differentiate multiple instances of this adapter from each other, if, for some reason, that is a thing you do.
Allowing the overriding of the default feature/gate classes means you can roll your own tables and what not, if you so desire.
35 36 37 38 39 |
# File 'lib/flipper/adapters/sequel.rb', line 35 def initialize( = {}) @name = .fetch(:name, :sequel) @feature_class = .fetch(:feature_class) { Feature } @gate_class = .fetch(:gate_class) { Gate } end |
Instance Attribute Details
#name ⇒ Object (readonly)
Public: The name of the adapter.
21 22 23 |
# File 'lib/flipper/adapters/sequel.rb', line 21 def name @name end |
Instance Method Details
#add(feature) ⇒ Object
Public: Adds a feature to the set of known features.
47 48 49 50 51 52 |
# File 'lib/flipper/adapters/sequel.rb', line 47 def add(feature) # race condition, but add is only used by enable/disable which happen # super rarely, so it shouldn't matter in practice @feature_class.find_or_create(key: feature.key.to_s) true end |
#clear(feature) ⇒ Object
Public: Clears the gate values for a feature.
64 65 66 67 |
# File 'lib/flipper/adapters/sequel.rb', line 64 def clear(feature) @gate_class.where(feature_key: feature.key.to_s).delete 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.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/flipper/adapters/sequel.rb', line 128 def disable(feature, gate, thing) case gate.data_type when :boolean clear(feature) when :integer @gate_class.db.transaction do args = { feature_key: feature.key.to_s, key: gate.key.to_s } @gate_class.where(args).delete @gate_class.create(gate_attrs(feature, gate, thing)) end when :set @gate_class.where(gate_attrs(feature, gate, thing)) .delete 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.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/flipper/adapters/sequel.rb', line 100 def enable(feature, gate, thing) case gate.data_type when :boolean, :integer @gate_class.db.transaction do args = { feature_key: feature.key, key: gate.key.to_s } @gate_class.where(args).delete @gate_class.create(gate_attrs(feature, gate, thing)) end when :set @gate_class.create(gate_attrs(feature, gate, thing)) else unsupported_data_type gate.data_type end true end |
#features ⇒ Object
Public: The set of known features.
42 43 44 |
# File 'lib/flipper/adapters/sequel.rb', line 42 def features @feature_class.all.map(&:key).to_set end |
#get(feature) ⇒ Object
Public: Gets the values for all gates for a given feature.
Returns a Hash of Flipper::Gate#key => value.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/flipper/adapters/sequel.rb', line 72 def get(feature) db_gates = @gate_class.where(feature_key: feature.key.to_s).all feature.gates.each_with_object({}) do |gate, result| result[gate.key] = case gate.data_type when :boolean if db_gate = db_gates.detect { |db_gate| db_gate.key == gate.key.to_s } db_gate.value end when :integer if db_gate = db_gates.detect { |db_gate| db_gate.key == gate.key.to_s } db_gate.value end when :set db_gates.select { |db_gate| db_gate.key == gate.key.to_s }.map(&:value).to_set else unsupported_data_type gate.data_type end end end |
#remove(feature) ⇒ Object
Public: Removes a feature from the set of known features.
55 56 57 58 59 60 61 |
# File 'lib/flipper/adapters/sequel.rb', line 55 def remove(feature) @feature_class.db.transaction do @feature_class.where(key: feature.key.to_s).delete clear(feature) end true end |