class Ab::AssignedTest

Attributes

after[R]
before[R]

Public Class Methods

after_picking_variant(&block) click to toggle source
# File lib/ab/assigned_test.rb, line 16
def after_picking_variant(&block)
  @after = block
end
before_picking_variant(&block) click to toggle source
# File lib/ab/assigned_test.rb, line 12
def before_picking_variant(&block)
  @before = block
end
new(test, id) click to toggle source
# File lib/ab/assigned_test.rb, line 5
def initialize(test, id)
  @test = test
  @id = id
end

Public Instance Methods

end_at() click to toggle source
# File lib/ab/assigned_test.rb, line 45
def end_at
  @test.end_at
end
method_missing(name, *args, &block) click to toggle source
Calls superclass method Ab::MissingVariant#method_missing
# File lib/ab/assigned_test.rb, line 21
def method_missing(name, *args, &block)
  variant_query = name.to_s[0..-2]
  return variant == variant_query if variant_method?(name) && variants.include?(variant_query)
  super
end
start_at() click to toggle source
# File lib/ab/assigned_test.rb, line 41
def start_at
  @test.start_at
end
variant(run_callbacks = true) click to toggle source
# File lib/ab/assigned_test.rb, line 27
def variant(run_callbacks = true)
  @variant ||= begin
    return unless part_of_test?
    return unless running?

    AssignedTest.before.call(name) if run_callbacks && AssignedTest.before.respond_to?(:call)
    picked_variant = @test.variants.find { |v| v.accumulated_chance_weight > weight_id }

    result = picked_variant.name if picked_variant
    AssignedTest.after.call(name, result) if run_callbacks && AssignedTest.after.respond_to?(:call)
    result
  end
end
variants() click to toggle source
# File lib/ab/assigned_test.rb, line 49
def variants
  @test.variants.map(&:name)
end

Private Instance Methods

bucket_id() click to toggle source
# File lib/ab/assigned_test.rb, line 64
def bucket_id
  @bucket_id ||= digest("#{@test.salt}#{@id}") % @test.bucket_count
end
digest(string) click to toggle source
# File lib/ab/assigned_test.rb, line 80
def digest(string)
  Digest::SHA256.hexdigest(string).to_i(16)
end
name() click to toggle source
# File lib/ab/assigned_test.rb, line 55
def name
  @test.name
end
part_of_test?() click to toggle source
# File lib/ab/assigned_test.rb, line 59
def part_of_test?
  @test.all_buckets? ||
    @test.buckets && @test.buckets.include?(bucket_id)
end
positive_weight_sum() click to toggle source
# File lib/ab/assigned_test.rb, line 76
def positive_weight_sum
  @test.weight_sum > 0 ? @test.weight_sum : 1
end
running?() click to toggle source
# File lib/ab/assigned_test.rb, line 68
def running?
  DateTime.now.between?(start_at, end_at)
end
weight_id() click to toggle source
# File lib/ab/assigned_test.rb, line 72
def weight_id
  @variant_digest ||= digest("#{@test.seed}#{@id}") % positive_weight_sum
end