lib/rollout.rb in rollout-2.4.6 vs lib/rollout.rb in rollout-2.5.0

- old
+ new

@@ -1,22 +1,34 @@ # frozen_string_literal: true require 'rollout/feature' +require 'rollout/logging' require 'rollout/version' require 'zlib' require 'set' require 'json' +require 'observer' class Rollout + include Observable + RAND_BASE = (2**32 - 1) / 100.0 + attr_reader :options, :storage + def initialize(storage, opts = {}) @storage = storage @options = opts @groups = { all: ->(_user) { true } } + + extend(Logging) if opts[:logging] end + def groups + @groups.keys + end + def activate(feature) with_feature(feature) do |f| f.percentage = 100 end end @@ -28,10 +40,14 @@ def delete(feature) features = (@storage.get(features_key) || '').split(',') features.delete(feature.to_s) @storage.set(features_key, features.join(',')) @storage.del(key(feature)) + + if respond_to?(:logging) + logging.delete(feature) + end end def set(feature, desired_state) with_feature(feature) do |f| if desired_state @@ -177,23 +193,32 @@ else @storage.exists(key(feature)) end end + def with_feature(feature) + f = get(feature) + + if count_observers > 0 + before = Marshal.load(Marshal.dump(f)) + yield(f) + save(f) + changed + notify_observers(:update, before, f) + else + yield(f) + save(f) + end + end + private def key(name) "feature:#{name}" end def features_key 'feature:__features__' - end - - def with_feature(feature) - f = get(feature) - yield(f) - save(f) end def save(feature) @storage.set(key(feature.name), feature.serialize) @storage.set(features_key, (features | [feature.name.to_sym]).join(','))