Class: Flipper::Adapters::ActiveRecord
- Inherits:
-
Object
- Object
- Flipper::Adapters::ActiveRecord
- Includes:
- Flipper::Adapter
- Defined in:
- lib/flipper/adapters/activerecord.rb,
lib/flipper/adapters/activerecord/version.rb
Constant Summary collapse
- VERSION =
"0.1.3"
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 all 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 ⇒ ActiveRecord
constructor
A new instance of ActiveRecord.
-
#remove(feature) ⇒ Object
Public: Removes a feature from the set of known features.
-
#unsupported_data_type(data_type) ⇒ Object
Private.
Constructor Details
#initialize ⇒ ActiveRecord
Returns a new instance of ActiveRecord.
16 17 18 |
# File 'lib/flipper/adapters/activerecord.rb', line 16 def initialize @name = :activerecord end |
Instance Attribute Details
#name ⇒ Object (readonly)
Public: The name of the adapter.
14 15 16 |
# File 'lib/flipper/adapters/activerecord.rb', line 14 def name @name end |
Instance Method Details
#add(feature) ⇒ Object
Public: Adds a feature to the set of known features.
26 27 28 29 |
# File 'lib/flipper/adapters/activerecord.rb', line 26 def add(feature) Flipper::ActiveRecord::Feature.find_or_create_by!(name: feature.name.to_s) true end |
#clear(feature) ⇒ Object
Public: Clears all the gate values for a feature.
38 39 40 41 42 |
# File 'lib/flipper/adapters/activerecord.rb', line 38 def clear(feature) f = Flipper::ActiveRecord::Feature.find_by(name: feature.key) f.gates.destroy_all if f 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.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/flipper/adapters/activerecord.rb', line 117 def disable(feature, gate, thing) scope = Flipper::ActiveRecord::Gate.joins(:feature).where(flipper_features: {name: feature.key}) g = case gate.data_type when :boolean scope.destroy_all when :integer scope.where(name: gate.key.to_s).limit(1). update_all(value: thing.value.to_s) when :set scope.where(name: gate.key.to_s, value: thing.value.to_s).destroy_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.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/flipper/adapters/activerecord.rb', line 79 def enable(feature, gate, thing) case gate.data_type when :boolean, :integer g = Flipper::ActiveRecord::Gate.joins(:feature). where(flipper_features: {name: feature.key}). find_or_initialize_by({ name: gate.key.to_s, }) g.value = thing.value.to_s unless g.persisted? g.feature = Flipper::ActiveRecord::Feature.select(:id).find_or_create_by!(name: feature.key) end g.save! when :set g = Flipper::ActiveRecord::Gate.joins(:feature). where(flipper_features: {name: feature.key}). find_or_initialize_by({ name: gate.key.to_s, value: thing.value.to_s, }) unless g.persisted? g.feature = Flipper::ActiveRecord::Feature.select(:id).find_or_create_by!(name: feature.key) end g.save! else unsupported_data_type gate.data_type end true end |
#features ⇒ Object
Public: The set of known features.
21 22 23 |
# File 'lib/flipper/adapters/activerecord.rb', line 21 def features Flipper::ActiveRecord::Feature.select(:name).map(&:name).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.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/flipper/adapters/activerecord.rb', line 47 def get(feature) result = {} f = Flipper::ActiveRecord::Feature.eager_load(:gates).find_by(name: feature.key) feature.gates.each do |gate| result[gate.key] = case gate.data_type when :boolean, :integer if f g = f.gates.detect {|g| g.name == gate.key.to_s} g.value if g end when :set if f f.gates.select {|g| g.name == gate.key.to_s}.map{|g| g.value }.to_set else Set.new end else unsupported_data_type gate.data_type end end result end |
#remove(feature) ⇒ Object
Public: Removes a feature from the set of known features.
32 33 34 35 |
# File 'lib/flipper/adapters/activerecord.rb', line 32 def remove(feature) Flipper::ActiveRecord::Feature.where(name: feature.key).destroy_all true end |
#unsupported_data_type(data_type) ⇒ Object
Private
136 137 138 |
# File 'lib/flipper/adapters/activerecord.rb', line 136 def unsupported_data_type(data_type) raise "#{data_type} is not supported by this adapter" end |