lib/rollout.rb in rollout-2.0.0 vs lib/rollout.rb in rollout-2.1.0

- old
+ new

@@ -1,15 +1,18 @@ +require "rollout/version" require "rollout/legacy" require "zlib" class Rollout class Feature - attr_reader :name, :groups, :users, :percentage - attr_writer :percentage, :groups, :users + attr_accessor :groups, :users, :percentage + attr_reader :name, :options - def initialize(name, string = nil) - @name = name + def initialize(name, string = nil, opts = {}) + @options = opts + @name = name + if string raw_percentage,raw_users,raw_groups = string.split("|") @percentage = raw_percentage.to_i @users = (raw_users || "").split(",").map(&:to_s) @groups = (raw_groups || "").split(",").map(&:to_sym) @@ -21,15 +24,16 @@ def serialize "#{@percentage}|#{@users.join(",")}|#{@groups.join(",")}" end def add_user(user) - @users << user.id.to_s unless @users.include?(user.id.to_s) + id = user_id(user) + @users << id unless @users.include?(id) end def remove_user(user) - @users.delete(user.id.to_s) + @users.delete(user_id(user)) end def add_group(group) @groups << group.to_sym unless @groups.include?(group.to_sym) end @@ -46,12 +50,13 @@ def active?(rollout, user) if user.nil? @percentage == 100 else - user_in_percentage?(user) || - user_in_active_users?(user) || + id = user_id(user) + user_in_percentage?(id) || + user_in_active_users?(id) || user_in_active_group?(user, rollout) end end def to_hash @@ -59,29 +64,43 @@ :groups => @groups, :users => @users} end private + def user_id(user) + if user.is_a?(Fixnum) || + user.is_a?(String) + user.to_s + else + user.send(id_user_by).to_s + end + end + + def id_user_by + @options[:id_user_by] || :id + end + def user_in_percentage?(user) - Zlib.crc32(user.id.to_s) % 100 < @percentage + Zlib.crc32(user_id(user)) % 100 < @percentage end def user_in_active_users?(user) - @users.include?(user.id.to_s) + @users.include?(user_id(user)) end def user_in_active_group?(user, rollout) @groups.any? do |g| rollout.active_in_group?(g, user) end end end def initialize(storage, opts = {}) - @storage = storage - @groups = {:all => lambda { |user| true }} - @legacy = Legacy.new(opts[:legacy_storage] || @storage) if opts[:migrate] + @storage = storage + @options = opts + @groups = {:all => lambda { |user| true }} + @legacy = Legacy.new(opts[:legacy_storage] || @storage) if opts[:migrate] end def activate(feature) with_feature(feature) do |f| f.percentage = 100 @@ -92,10 +111,20 @@ with_feature(feature) do |f| f.clear end end + def set(feature, desired_state) + with_feature(feature) do |f| + if desired_state + f.percentage = 100 + else + f.clear + end + end + end + def activate_group(feature, group) with_feature(feature) do |f| f.add_group(group) end end @@ -145,15 +174,16 @@ end def get(feature) string = @storage.get(key(feature)) if string || !migrate? - Feature.new(feature, string) + Feature.new(feature, string, @options) else info = @legacy.info(feature) f = Feature.new(feature) f.percentage = info[:percentage] + f.percentage = 100 if info[:global].include? feature f.groups = info[:groups].map { |g| g.to_sym } f.users = info[:users].map { |u| u.to_s } save(f) f end @@ -161,10 +191,19 @@ def features (@storage.get(features_key) || "").split(",").map(&:to_sym) end + def clear! + features.each do |feature| + with_feature(feature) { |f| f.clear } + @storage.del(key(feature)) + end + + @storage.del(features_key) + end + private def key(name) "feature:#{name}" end @@ -178,10 +217,10 @@ save(f) end def save(feature) @storage.set(key(feature.name), feature.serialize) - @storage.set(features_key, (features | [feature.name]).join(",")) + @storage.set(features_key, (features | [feature.name.to_sym]).join(",")) end def migrate? @legacy end