Class: Flipper::Adapters::Sequel
- Inherits:
-
Object
- Object
- Flipper::Adapters::Sequel
- Includes:
- Flipper::Adapter
- Defined in:
- lib/flipper/adapters/sequel.rb
Constant Summary collapse
- VALUE_TO_TEXT_WARNING =
<<-EOS Your database needs migrated to use the latest Flipper features. See https://github.com/flippercloud/flipper/issues/557 EOS
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.
- #get_all ⇒ Object
- #get_multi(features) ⇒ Object
-
#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 :sequel) 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.
49 50 51 52 53 54 55 |
# File 'lib/flipper/adapters/sequel.rb', line 49 def initialize( = {}) @name = .fetch(:name, :sequel) @feature_class = .fetch(:feature_class) { Feature } @gate_class = .fetch(:gate_class) { Gate } warn VALUE_TO_TEXT_WARNING if value_not_text? end |
Instance Method Details
#add(feature) ⇒ Object
Public: Adds a feature to the set of known features.
63 64 65 66 67 68 |
# File 'lib/flipper/adapters/sequel.rb', line 63 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.
80 81 82 83 |
# File 'lib/flipper/adapters/sequel.rb', line 80 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.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/flipper/adapters/sequel.rb', line 154 def disable(feature, gate, thing) case gate.data_type when :boolean clear(feature) when :integer set(feature, gate, thing) when :json delete(feature, gate) when :set @gate_class.where(gate_attrs(feature, gate, thing, json: gate.data_type == :json)).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 enable. thing - The Flipper::Type being enabled for the gate.
Returns true.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/flipper/adapters/sequel.rb', line 130 def enable(feature, gate, thing) case gate.data_type when :boolean set(feature, gate, thing, clear: true) when :integer set(feature, gate, thing) when :set enable_multi(feature, gate, thing) when :json set(feature, gate, thing, json: true) else unsupported_data_type gate.data_type end true end |
#features ⇒ Object
Public: The set of known features.
58 59 60 |
# File 'lib/flipper/adapters/sequel.rb', line 58 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.
88 89 90 91 92 |
# File 'lib/flipper/adapters/sequel.rb', line 88 def get(feature) db_gates = @gate_class.where(feature_key: feature.key.to_s).all result_for_feature(feature, db_gates) end |
#get_all ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/flipper/adapters/sequel.rb', line 104 def get_all feature_table = @feature_class.table_name.to_sym gate_table = @gate_class.table_name.to_sym features_sql = @feature_class.select(::Sequel.qualify(feature_table, :key).as(:feature_key)) .select_append(::Sequel.qualify(gate_table, :key)) .select_append(::Sequel.qualify(gate_table, :value)) .left_join(@gate_class.table_name.to_sym, feature_key: :key) .sql db_gates = @gate_class.fetch(features_sql).to_a grouped_db_gates = db_gates.group_by(&:feature_key) result = Hash.new { |hash, key| hash[key] = default_config } features = grouped_db_gates.keys.map { |key| Flipper::Feature.new(key, self) } features.each do |feature| result[feature.key] = result_for_feature(feature, grouped_db_gates[feature.key]) end result end |
#get_multi(features) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/flipper/adapters/sequel.rb', line 94 def get_multi(features) db_gates = @gate_class.where(feature_key: features.map(&:key)).to_a grouped_db_gates = db_gates.group_by(&:feature_key) result = {} features.each do |feature| result[feature.key] = result_for_feature(feature, grouped_db_gates[feature.key]) end result end |
#remove(feature) ⇒ Object
Public: Removes a feature from the set of known features.
71 72 73 74 75 76 77 |
# File 'lib/flipper/adapters/sequel.rb', line 71 def remove(feature) @feature_class.db.transaction do @feature_class.where(key: feature.key.to_s).delete clear(feature) end true end |