Class: Flipper::Adapters::Sequel

Inherits:
Object
  • Object
show all
Includes:
Flipper::Adapter
Defined in:
lib/flipper/adapters/sequel.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.



46
47
48
49
50
# File 'lib/flipper/adapters/sequel.rb', line 46

def initialize(options = {})
  @name = options.fetch(:name, :sequel)
  @feature_class = options.fetch(:feature_class) { Feature }
  @gate_class = options.fetch(:gate_class) { Gate }
end

Instance Attribute Details

#nameObject (readonly)

Public: The name of the adapter.



32
33
34
# File 'lib/flipper/adapters/sequel.rb', line 32

def name
  @name
end

Instance Method Details

#add(feature) ⇒ Object

Public: Adds a feature to the set of known features.



58
59
60
61
62
63
# File 'lib/flipper/adapters/sequel.rb', line 58

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.



75
76
77
78
# File 'lib/flipper/adapters/sequel.rb', line 75

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.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/flipper/adapters/sequel.rb', line 151

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.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/flipper/adapters/sequel.rb', line 121

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
    begin
      @gate_class.create(gate_attrs(feature, gate, thing))
    rescue ::Sequel::UniqueConstraintViolation
    end
  else
    unsupported_data_type gate.data_type
  end

  true
end

#featuresObject

Public: The set of known features.



53
54
55
# File 'lib/flipper/adapters/sequel.rb', line 53

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.



83
84
85
86
87
# File 'lib/flipper/adapters/sequel.rb', line 83

def get(feature)
  db_gates = @gate_class.where(feature_key: feature.key.to_s).all

  result_for_feature(feature, db_gates)
end

#get_allObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/flipper/adapters/sequel.rb', line 99

def get_all
  db_gates = @gate_class.fetch(<<-SQL).to_a
    SELECT ff.key AS feature_key, fg.key, fg.value
    FROM #{@feature_class.table_name} ff
    LEFT JOIN #{@gate_class.table_name} fg ON ff.key = fg.feature_key
  SQL
  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



89
90
91
92
93
94
95
96
97
# File 'lib/flipper/adapters/sequel.rb', line 89

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.



66
67
68
69
70
71
72
# File 'lib/flipper/adapters/sequel.rb', line 66

def remove(feature)
  @feature_class.db.transaction do
    @feature_class.where(key: feature.key.to_s).delete
    clear(feature)
  end
  true
end