lib/rollout.rb in rollout-2.0.0b vs lib/rollout.rb in rollout-2.0.0c
- old
+ new
@@ -1,6 +1,7 @@
require "rollout/legacy"
+require "zlib"
class Rollout
class Feature
attr_reader :name, :groups, :users, :percentage
attr_writer :percentage, :groups, :users
@@ -8,11 +9,11 @@
def initialize(name, string = nil)
@name = name
if string
raw_percentage,raw_users,raw_groups = string.split("|")
@percentage = raw_percentage.to_i
- @users = (raw_users || "").split(",").map(&:to_i)
+ @users = (raw_users || "").split(",").map(&:to_s)
@groups = (raw_groups || "").split(",").map(&:to_sym)
else
clear
end
end
@@ -20,19 +21,19 @@
def serialize
"#{@percentage}|#{@users.join(",")}|#{@groups.join(",")}"
end
def add_user(user)
- @users << user.id unless @users.include?(user.id)
+ @users << user.id.to_s unless @users.include?(user.id.to_s)
end
def remove_user(user)
- @users.delete(user.id.to_i)
+ @users.delete(user.id.to_s)
end
def add_group(group)
- @groups << group unless @groups.include?(group)
+ @groups << group.to_sym unless @groups.include?(group.to_sym)
end
def remove_group(group)
@groups.delete(group.to_sym)
end
@@ -59,15 +60,15 @@
:users => @users}
end
private
def user_in_percentage?(user)
- user.id % 100 < @percentage
+ Zlib.crc32(user.id.to_s) % 100 < @percentage
end
def user_in_active_users?(user)
- @users.include?(user.id)
+ @users.include?(user.id.to_s)
end
def user_in_active_group?(user, rollout)
@groups.any? do |g|
rollout.active_in_group?(g, user)
@@ -116,11 +117,11 @@
f.remove_user(user)
end
end
def define_group(group, &block)
- @groups[group] = block
+ @groups[group.to_sym] = block
end
def active?(feature, user = nil)
feature = get(feature)
feature.active?(self, user)
@@ -137,11 +138,11 @@
f.percentage = 0
end
end
def active_in_group?(group, user)
- f = @groups[group]
+ f = @groups[group.to_sym]
f && f.call(user)
end
def get(feature)
string = @storage.get(key(feature))
@@ -149,11 +150,11 @@
Feature.new(feature, string)
else
info = @legacy.info(feature)
f = Feature.new(feature)
f.percentage = info[:percentage]
- f.groups = info[:groups]
- f.users = info[:users]
+ f.groups = info[:groups].map { |g| g.to_sym }
+ f.users = info[:users].map { |u| u.to_s }
save(f)
f
end
end