Class: Flipper::Adapters::ActiveRecord
- Inherits:
-
Object
- Object
- Flipper::Adapters::ActiveRecord
- Includes:
- Flipper::Adapter
- Defined in:
- lib/flipper/adapters/active_record.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.
- #get_multi(features) ⇒ Object
-
#initialize(options = {}) ⇒ ActiveRecord
constructor
Public: Initialize a new ActiveRecord adapter instance.
-
#remove(feature) ⇒ Object
Public: Removes a feature from the set of known features.
-
#unsupported_data_type(data_type) ⇒ Object
Private.
Constructor Details
#initialize(options = {}) ⇒ ActiveRecord
Public: Initialize a new ActiveRecord 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/active_record.rb', line 35 def initialize( = {}) @name = .fetch(:name, :active_record) @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/active_record.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 53 54 |
# File 'lib/flipper/adapters/active_record.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 unless @feature_class.where(key: feature.key).first @feature_class.create! { |f| f.key = feature.key } end true end |
#clear(feature) ⇒ Object
Public: Clears the gate values for a feature.
66 67 68 69 |
# File 'lib/flipper/adapters/active_record.rb', line 66 def clear(feature) @gate_class.where(feature_key: feature.key).delete_all 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.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/flipper/adapters/active_record.rb', line 131 def disable(feature, gate, thing) case gate.data_type when :boolean clear(feature) when :integer @gate_class.transaction do @gate_class.where( feature_key: feature.key, key: gate.key ).delete_all @gate_class.create! do |g| g.feature_key = feature.key g.key = gate.key g.value = thing.value.to_s end end when :set @gate_class.where(feature_key: feature.key, key: gate.key, value: thing.value).delete_all 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.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/flipper/adapters/active_record.rb', line 96 def enable(feature, gate, thing) case gate.data_type when :boolean, :integer @gate_class.transaction do @gate_class.where( feature_key: feature.key, key: gate.key ).delete_all @gate_class.create! do |g| g.feature_key = feature.key g.key = gate.key g.value = thing.value.to_s end end when :set @gate_class.create! do |g| g.feature_key = feature.key g.key = gate.key g.value = thing.value.to_s end 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/active_record.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.
74 75 76 77 |
# File 'lib/flipper/adapters/active_record.rb', line 74 def get(feature) db_gates = @gate_class.where(feature_key: feature.key) result_for_feature(feature, db_gates) end |
#get_multi(features) ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/flipper/adapters/active_record.rb', line 79 def get_multi(features) db_gates = @gate_class.where(feature_key: features.map(&:key)) grouped_db_gates = db_gates.group_by { |gate| gate.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.
57 58 59 60 61 62 63 |
# File 'lib/flipper/adapters/active_record.rb', line 57 def remove(feature) @feature_class.transaction do @feature_class.where(key: feature.key).delete_all clear(feature) end true end |
#unsupported_data_type(data_type) ⇒ Object
Private
158 159 160 |
# File 'lib/flipper/adapters/active_record.rb', line 158 def unsupported_data_type(data_type) raise "#{data_type} is not supported by this adapter" end |