class Shamu::Features::Conditions::Percentage

Match against a limited percentage of total users.

Public Instance Methods

match?( context ) click to toggle source

(see Condition#match?)

# File lib/shamu/features/conditions/percentage.rb, line 11
def match?( context )
  if context.user_id
    ( user_id_hash( context.user_id ) ^ toggle_crc ) % 100 < percentage
  else
    context.sticky!
    Random.rand( 100 ) < percentage
  end
end

Private Instance Methods

percentage() click to toggle source
# File lib/shamu/features/conditions/percentage.rb, line 22
def percentage
  @percentage ||= [ config.to_i, 100 ].min
end
toggle_crc() click to toggle source
# File lib/shamu/features/conditions/percentage.rb, line 34
def toggle_crc
  # Use the name of the toggle to provide consistent semi-random noise
  # into the user selection process.
  @toggle_crc ||= Crc32.calculate( toggle.name, toggle.name.length, 0 )
end
user_id_hash( user_id ) click to toggle source
# File lib/shamu/features/conditions/percentage.rb, line 26
def user_id_hash( user_id )
  if user_id.is_a?( Numeric )
    user_id
  else
    user_id.sub( "-", "" ).to_i( 16 )
  end
end