Sha256: 62cfa2e9bf5b773dcb42a4d4259caabaf60793f7ba9d4e03849ea84c7f420a2e

Contents?: true

Size: 1.17 KB

Versions: 54

Compression:

Stored size: 1.17 KB

Contents

require 'digest'

class TestTrack::VariantCalculator
  include TestTrack::RequiredOptions

  attr_reader :visitor, :split_name

  delegate :split_registry, to: :visitor

  def initialize(opts = {})
    @visitor = require_option!(opts, :visitor)
    @split_name = require_option!(opts, :split_name)
    raise "unknown opts: #{opts.keys.to_sentence}" if opts.present?
  end

  def variant
    return nil unless split_registry
    @variant ||= _variant || raise("Assignment bucket out of range. #{assignment_bucket} unmatched in #{split_name}: #{weighting}")
  end

  def _variant
    bucket_ceiling = 0
    sorted_variants.detect do |variant|
      bucket_ceiling += weighting[variant]
      bucket_ceiling > assignment_bucket
    end
  end

  def sorted_variants
    weighting.keys.sort
  end

  def weighting
    @weighting ||= split_registry[split_name] || raise("TestTrack split '#{split_name}' not found. Need to write/run a migration?")
  end

  def assignment_bucket
    @assignment_bucket ||= hash_fixnum % 100
  end

  def hash_fixnum
    split_visitor_hash.slice(0, 8).to_i(16)
  end

  def split_visitor_hash
    Digest::MD5.new.update(split_name.to_s + visitor.id.to_s).hexdigest
  end
end

Version data entries

54 entries across 54 versions & 1 rubygems

Version Path
test_track_rails_client-4.0.0.alpha26 app/models/test_track/variant_calculator.rb
test_track_rails_client-4.0.0.alpha25 app/models/test_track/variant_calculator.rb
test_track_rails_client-4.0.0.alpha24 app/models/test_track/variant_calculator.rb
test_track_rails_client-4.0.0.alpha23 app/models/test_track/variant_calculator.rb
test_track_rails_client-4.0.0.alpha22 app/models/test_track/variant_calculator.rb
test_track_rails_client-4.0.0.alpha21 app/models/test_track/variant_calculator.rb
test_track_rails_client-4.0.0.alpha20 app/models/test_track/variant_calculator.rb
test_track_rails_client-4.0.0.alpha19 app/models/test_track/variant_calculator.rb
test_track_rails_client-4.0.0.alpha18 app/models/test_track/variant_calculator.rb
test_track_rails_client-4.0.0.alpha17 app/models/test_track/variant_calculator.rb
test_track_rails_client-4.0.0.alpha16 app/models/test_track/variant_calculator.rb
test_track_rails_client-4.0.0.alpha15 app/models/test_track/variant_calculator.rb
test_track_rails_client-4.0.0.alpha14 app/models/test_track/variant_calculator.rb
test_track_rails_client-4.0.0.alpha13 app/models/test_track/variant_calculator.rb
test_track_rails_client-4.0.0.alpha12 app/models/test_track/variant_calculator.rb
test_track_rails_client-4.0.0.alpha11 app/models/test_track/variant_calculator.rb
test_track_rails_client-4.0.0.alpha10 app/models/test_track/variant_calculator.rb
test_track_rails_client-4.0.0.alpha9 app/models/test_track/variant_calculator.rb
test_track_rails_client-4.0.0.alpha8 app/models/test_track/variant_calculator.rb
test_track_rails_client-4.0.0.alpha7 app/models/test_track/variant_calculator.rb