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

#respond_to_missing?

Constructor Details

#initialize(test, id) ⇒ AssignedTest

Returns a new instance of AssignedTest.



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

def initialize(test, id)
  @test = test
  @id = id
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



21
22
23
24
25
# 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

Class Attribute Details

.afterObject (readonly)

Returns the value of attribute after.



11
12
13
# File 'lib/ab/assigned_test.rb', line 11

def after
  @after
end

.beforeObject (readonly)

Returns the value of attribute before.



11
12
13
# File 'lib/ab/assigned_test.rb', line 11

def before
  @before
end

Class Method Details

.after_picking_variant(&block) ⇒ Object



16
17
18
# File 'lib/ab/assigned_test.rb', line 16

def after_picking_variant(&block)
  @after = block
end

.before_picking_variant(&block) ⇒ Object



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

def before_picking_variant(&block)
  @before = block
end

Instance Method Details

#end_atObject



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

def end_at
  @test.end_at
end

#start_atObject



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

def start_at
  @test.start_at
end

#variant(run_callbacks = true) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# 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

#variantsObject



49
50
51
# File 'lib/ab/assigned_test.rb', line 49

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