Class: Rollout

Inherits:
Object
  • Object
show all
Defined in:
lib/rollout.rb,
lib/rollout/version.rb

Defined Under Namespace

Classes: Feature

Constant Summary collapse

RAND_BASE =
(2**32 - 1) / 100.0
VERSION =
'2.4.4'

Instance Method Summary collapse

Constructor Details

#initialize(storage, opts = {}) ⇒ Rollout

Returns a new instance of Rollout.



138
139
140
141
142
# File 'lib/rollout.rb', line 138

def initialize(storage, opts = {})
  @storage = storage
  @options = opts
  @groups  = { all: ->(_user) { true } }
end

Instance Method Details

#activate(feature) ⇒ Object



144
145
146
147
148
# File 'lib/rollout.rb', line 144

def activate(feature)
  with_feature(feature) do |f|
    f.percentage = 100
  end
end

#activate_group(feature, group) ⇒ Object



171
172
173
174
175
# File 'lib/rollout.rb', line 171

def activate_group(feature, group)
  with_feature(feature) do |f|
    f.add_group(group)
  end
end

#activate_percentage(feature, percentage) ⇒ Object



232
233
234
235
236
# File 'lib/rollout.rb', line 232

def activate_percentage(feature, percentage)
  with_feature(feature) do |f|
    f.percentage = percentage
  end
end

#activate_user(feature, user) ⇒ Object



183
184
185
186
187
# File 'lib/rollout.rb', line 183

def activate_user(feature, user)
  with_feature(feature) do |f|
    f.add_user(user)
  end
end

#activate_users(feature, users) ⇒ Object



195
196
197
198
199
# File 'lib/rollout.rb', line 195

def activate_users(feature, users)
  with_feature(feature) do |f|
    users.each { |user| f.add_user(user) }
  end
end

#active?(feature, user = nil) ⇒ Boolean

Returns:

  • (Boolean)


218
219
220
221
# File 'lib/rollout.rb', line 218

def active?(feature, user = nil)
  feature = get(feature)
  feature.active?(self, user)
end

#active_features(user = nil) ⇒ Object



281
282
283
284
285
# File 'lib/rollout.rb', line 281

def active_features(user = nil)
  multi_get(*features).select do |f|
    f.active?(self, user)
  end.map(&:name)
end

#active_in_group?(group, user) ⇒ Boolean

Returns:

  • (Boolean)


244
245
246
247
# File 'lib/rollout.rb', line 244

def active_in_group?(group, user)
  f = @groups[group.to_sym]
  f&.call(user)
end

#clear!Object



287
288
289
290
291
292
293
294
# File 'lib/rollout.rb', line 287

def clear!
  features.each do |feature|
    with_feature(feature, &:clear)
    @storage.del(key(feature))
  end

  @storage.del(features_key)
end

#clear_feature_data(feature) ⇒ Object



260
261
262
263
264
# File 'lib/rollout.rb', line 260

def clear_feature_data(feature)
  with_feature(feature) do |f|
    f.data = {}
  end
end

#deactivate(feature) ⇒ Object



150
151
152
# File 'lib/rollout.rb', line 150

def deactivate(feature)
  with_feature(feature, &:clear)
end

#deactivate_group(feature, group) ⇒ Object



177
178
179
180
181
# File 'lib/rollout.rb', line 177

def deactivate_group(feature, group)
  with_feature(feature) do |f|
    f.remove_group(group)
  end
end

#deactivate_percentage(feature) ⇒ Object



238
239
240
241
242
# File 'lib/rollout.rb', line 238

def deactivate_percentage(feature)
  with_feature(feature) do |f|
    f.percentage = 0
  end
end

#deactivate_user(feature, user) ⇒ Object



189
190
191
192
193
# File 'lib/rollout.rb', line 189

def deactivate_user(feature, user)
  with_feature(feature) do |f|
    f.remove_user(user)
  end
end

#deactivate_users(feature, users) ⇒ Object



201
202
203
204
205
# File 'lib/rollout.rb', line 201

def deactivate_users(feature, users)
  with_feature(feature) do |f|
    users.each { |user| f.remove_user(user) }
  end
end

#define_group(group, &block) ⇒ Object



214
215
216
# File 'lib/rollout.rb', line 214

def define_group(group, &block)
  @groups[group.to_sym] = block
end

#delete(feature) ⇒ Object



154
155
156
157
158
159
# File 'lib/rollout.rb', line 154

def delete(feature)
  features = (@storage.get(features_key) || '').split(',')
  features.delete(feature.to_s)
  @storage.set(features_key, features.join(','))
  @storage.del(key(feature))
end

#exists?(feature) ⇒ Boolean

Returns:

  • (Boolean)


296
297
298
# File 'lib/rollout.rb', line 296

def exists?(feature)
  @storage.exists(key(feature))
end

#feature_states(user = nil) ⇒ Object



275
276
277
278
279
# File 'lib/rollout.rb', line 275

def feature_states(user = nil)
  multi_get(*features).each_with_object({}) do |f, hash|
    hash[f.name] = f.active?(self, user)
  end
end

#featuresObject



271
272
273
# File 'lib/rollout.rb', line 271

def features
  (@storage.get(features_key) || '').split(',').map(&:to_sym)
end

#get(feature) ⇒ Object



249
250
251
252
# File 'lib/rollout.rb', line 249

def get(feature)
  string = @storage.get(key(feature))
  Feature.new(feature, string, @options)
end

#inactive?(feature, user = nil) ⇒ Boolean

Returns:

  • (Boolean)


228
229
230
# File 'lib/rollout.rb', line 228

def inactive?(feature, user = nil)
  !active?(feature, user)
end

#multi_get(*features) ⇒ Object



266
267
268
269
# File 'lib/rollout.rb', line 266

def multi_get(*features)
  feature_keys = features.map { |feature| key(feature) }
  @storage.mget(*feature_keys).map.with_index { |string, index| Feature.new(features[index], string, @options) }
end

#set(feature, desired_state) ⇒ Object



161
162
163
164
165
166
167
168
169
# File 'lib/rollout.rb', line 161

def set(feature, desired_state)
  with_feature(feature) do |f|
    if desired_state
      f.percentage = 100
    else
      f.clear
    end
  end
end

#set_feature_data(feature, data) ⇒ Object



254
255
256
257
258
# File 'lib/rollout.rb', line 254

def set_feature_data(feature, data)
  with_feature(feature) do |f|
    f.data.merge!(data) if data.is_a? Hash
  end
end

#set_users(feature, users) ⇒ Object



207
208
209
210
211
212
# File 'lib/rollout.rb', line 207

def set_users(feature, users)
  with_feature(feature) do |f|
    f.users = []
    users.each { |user| f.add_user(user) }
  end
end

#user_in_active_users?(feature, user = nil) ⇒ Boolean

Returns:

  • (Boolean)


223
224
225
226
# File 'lib/rollout.rb', line 223

def user_in_active_users?(feature, user = nil)
  feature = get(feature)
  feature.user_in_active_users?(user)
end