Class: Ab::AssignedTest

Inherits:
Object
  • Object
show all
Includes:
MissingVariant
Defined in:
lib/ab/assigned_test.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MissingVariant

#method_missing, #respond_to_missing?

Constructor Details

#initialize(test, id) ⇒ AssignedTest

Returns a new instance of AssignedTest.



5
6
7
8
9
10
# File 'lib/ab/assigned_test.rb', line 5

def initialize(test, id)
  @test, @id = test, id
  variants.each do |name|
    define_singleton_method("#{name}?") { name == variant }
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Ab::MissingVariant

Class Attribute Details

.afterObject (readonly)

Returns the value of attribute after.



13
14
15
# File 'lib/ab/assigned_test.rb', line 13

def after
  @after
end

.beforeObject (readonly)

Returns the value of attribute before.



13
14
15
# File 'lib/ab/assigned_test.rb', line 13

def before
  @before
end

Class Method Details

.after_picking_variant(&block) ⇒ Object



18
19
20
# File 'lib/ab/assigned_test.rb', line 18

def after_picking_variant(&block)
  @after = block
end

.before_picking_variant(&block) ⇒ Object



14
15
16
# File 'lib/ab/assigned_test.rb', line 14

def before_picking_variant(&block)
  @before = block
end

Instance Method Details

#end_atObject



41
42
43
# File 'lib/ab/assigned_test.rb', line 41

def end_at
  @test.end_at
end

#start_atObject



37
38
39
# File 'lib/ab/assigned_test.rb', line 37

def start_at
  @test.start_at
end

#variant(run_callbacks = true) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ab/assigned_test.rb', line 23

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

#variantsObject



45
46
47
# File 'lib/ab/assigned_test.rb', line 45

def variants
  @test.variants.map(&:name)
end