Class: Magick::Targeting::Percentage

Inherits:
Base
  • Object
show all
Defined in:
lib/magick/targeting/percentage.rb

Instance Method Summary collapse

Constructor Details

#initialize(percentage, feature_name, user_id = nil) ⇒ Percentage

Returns a new instance of Percentage.



8
9
10
11
12
# File 'lib/magick/targeting/percentage.rb', line 8

def initialize(percentage, feature_name, user_id = nil)
  @percentage = percentage.to_f
  @feature_name = feature_name.to_s
  @user_id = user_id&.to_s
end

Instance Method Details

#matches?(context) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
# File 'lib/magick/targeting/percentage.rb', line 14

def matches?(context)
  user_id = (@user_id || context[:user_id])&.to_s
  return false unless user_id

  hash = Digest::MD5.hexdigest("#{@feature_name}:#{user_id}")
  hash_value = hash[0..7].to_i(16)
  (hash_value % 100) < @percentage
end