Module: Philiprehberger::FeatureFlag::Rollout
- Defined in:
- lib/philiprehberger/feature_flag/rollout.rb
Class Method Summary collapse
-
.bucket_key(user_id, context, rollout_by) ⇒ String?
Build the bucket key from
user_idandcontextguided byrollout_by. -
.enabled_for?(flag, user_id, percentage, context: {}, rollout_by: [:user_id]) ⇒ Boolean
Determine whether
flagis enabled for a user given a percentage rollout.
Class Method Details
.bucket_key(user_id, context, rollout_by) ⇒ String?
Build the bucket key from user_id and context guided by rollout_by. When rollout_by is [:user_id] (the default) and the user id is missing, returns nil to preserve the historic “nil user_id disables rollout” behavior.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/philiprehberger/feature_flag/rollout.rb', line 43 def bucket_key(user_id, context, rollout_by) keys = Array(rollout_by).map(&:to_sym) keys = [:user_id] if keys.empty? if keys == [:user_id] return nil if user_id.nil? return user_id.to_s end parts = keys.map do |k| if k == :user_id user_id else context[k] || context[k.to_s] end end return nil if parts.all?(&:nil?) parts.join('|') end |
.enabled_for?(flag, user_id, percentage, context: {}, rollout_by: [:user_id]) ⇒ Boolean
Determine whether flag is enabled for a user given a percentage rollout. The bucket key defaults to the user id, matching the historic behavior. Pass rollout_by to mix additional context values into the bucket key — e.g. rollout_by: [:user_id, :region] assigns a user differently per region.
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/philiprehberger/feature_flag/rollout.rb', line 25 def enabled_for?(flag, user_id, percentage, context: {}, rollout_by: [:user_id]) return false if percentage.nil? return false if percentage <= 0 return true if percentage >= 100 key = bucket_key(user_id, context, rollout_by) return false if key.nil? bucket = Zlib.crc32("#{flag}:#{key}") % 100 bucket < percentage end |