lib/flipper/adapters/sequel.rb in flipper-sequel-0.17.1 vs lib/flipper/adapters/sequel.rb in flipper-sequel-0.17.2
- old
+ new
@@ -118,19 +118,14 @@
# thing - The Flipper::Type being disabled for the gate.
#
# Returns true.
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 :boolean
+ set(feature, gate, thing, clear: true)
+ when :integer
+ set(feature, gate, thing)
when :set
begin
@gate_class.create(gate_attrs(feature, gate, thing))
rescue ::Sequel::UniqueConstraintViolation
end
@@ -151,19 +146,11 @@
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
+ set(feature, gate, thing)
when :set
@gate_class.where(gate_attrs(feature, gate, thing))
.delete
else
unsupported_data_type gate.data_type
@@ -174,9 +161,23 @@
private
def unsupported_data_type(data_type)
raise "#{data_type} is not supported by this adapter"
+ end
+
+ def set(feature, gate, thing, options = {})
+ clear_feature = options.fetch(:clear, false)
+ args = {
+ feature_key: feature.key,
+ key: gate.key.to_s,
+ }
+
+ @gate_class.db.transaction do
+ clear(feature) if clear_feature
+ @gate_class.where(args).delete
+ @gate_class.create(gate_attrs(feature, gate, thing))
+ end
end
def gate_attrs(feature, gate, thing)
{
feature_key: feature.key.to_s,