class Rollout::Feature
Attributes
data[RW]
groups[RW]
name[R]
options[R]
percentage[RW]
users[RW]
Public Class Methods
new(name, string = nil, opts = {})
click to toggle source
# File lib/rollout/feature.rb, line 8 def initialize(name, string = nil, opts = {}) @options = opts @name = name if string raw_percentage, raw_users, raw_groups, raw_data = string.split('|', 4) @percentage = raw_percentage.to_f @users = users_from_string(raw_users) @groups = groups_from_string(raw_groups) @data = raw_data.nil? || raw_data.strip.empty? ? {} : JSON.parse(raw_data) else clear end end
Public Instance Methods
active?(rollout, user)
click to toggle source
# File lib/rollout/feature.rb, line 51 def active?(rollout, user) if user id = user_id(user) user_in_percentage?(id) || user_in_active_users?(id) || user_in_active_group?(user, rollout) else @percentage == 100 end end
add_group(group)
click to toggle source
# File lib/rollout/feature.rb, line 36 def add_group(group) @groups << group.to_sym unless @groups.include?(group.to_sym) end
add_user(user)
click to toggle source
# File lib/rollout/feature.rb, line 27 def add_user(user) id = user_id(user) @users << id unless @users.include?(id) end
clear()
click to toggle source
# File lib/rollout/feature.rb, line 44 def clear @groups = groups_from_string('') @users = users_from_string('') @percentage = 0 @data = {} end
remove_group(group)
click to toggle source
# File lib/rollout/feature.rb, line 40 def remove_group(group) @groups.delete(group.to_sym) end
remove_user(user)
click to toggle source
# File lib/rollout/feature.rb, line 32 def remove_user(user) @users.delete(user_id(user)) end
serialize()
click to toggle source
# File lib/rollout/feature.rb, line 23 def serialize "#{@percentage}|#{@users.to_a.join(',')}|#{@groups.to_a.join(',')}|#{serialize_data}" end
to_hash()
click to toggle source
# File lib/rollout/feature.rb, line 66 def to_hash { percentage: @percentage, groups: @groups, users: @users, data: @data, } end
user_in_active_users?(user)
click to toggle source
# File lib/rollout/feature.rb, line 62 def user_in_active_users?(user) @users.include?(user_id(user)) end
Private Instance Methods
groups_from_string(raw_groups)
click to toggle source
# File lib/rollout/feature.rb, line 122 def groups_from_string(raw_groups) groups = (raw_groups || '').split(',').map(&:to_sym) if @options[:use_sets] groups.to_set else groups end end
id_user_by()
click to toggle source
# File lib/rollout/feature.rb, line 85 def id_user_by @options[:id_user_by] || :id end
serialize_data()
click to toggle source
# File lib/rollout/feature.rb, line 107 def serialize_data return '' unless @data.is_a? Hash @data.to_json end
user_id(user)
click to toggle source
# File lib/rollout/feature.rb, line 77 def user_id(user) if user.is_a?(Integer) || user.is_a?(String) user.to_s else user.send(id_user_by).to_s end end
user_id_for_percentage(user)
click to toggle source
# File lib/rollout/feature.rb, line 93 def user_id_for_percentage(user) if @options[:randomize_percentage] user_id(user).to_s + @name.to_s else user_id(user) end end
user_in_active_group?(user, rollout)
click to toggle source
# File lib/rollout/feature.rb, line 101 def user_in_active_group?(user, rollout) @groups.any? do |g| rollout.active_in_group?(g, user) end end
user_in_percentage?(user)
click to toggle source
# File lib/rollout/feature.rb, line 89 def user_in_percentage?(user) Zlib.crc32(user_id_for_percentage(user)) < RAND_BASE * @percentage end
users_from_string(raw_users)
click to toggle source
# File lib/rollout/feature.rb, line 113 def users_from_string(raw_users) users = (raw_users || '').split(',').map(&:to_s) if @options[:use_sets] users.to_set else users end end