Class: TrailGuide::Variant

Inherits:
Object
  • Object
show all
Defined in:
lib/trail_guide/variant.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(experiment, name, metadata: {}, weight: 1, control: false) ⇒ Variant

Returns a new instance of Variant.



9
10
11
12
13
14
15
# File 'lib/trail_guide/variant.rb', line 9

def initialize(experiment, name, metadata: {}, weight: 1, control: false)
  @experiment = experiment
  @name = name.to_s.underscore.to_sym
  @metadata = 
  @weight = weight
  @control = control
end

Instance Attribute Details

#experimentObject (readonly)

Returns the value of attribute experiment.



3
4
5
# File 'lib/trail_guide/variant.rb', line 3

def experiment
  @experiment
end

#metadataObject (readonly)

Returns the value of attribute metadata.



3
4
5
# File 'lib/trail_guide/variant.rb', line 3

def 
  @metadata
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/trail_guide/variant.rb', line 3

def name
  @name
end

#weightObject (readonly)

Returns the value of attribute weight.



3
4
5
# File 'lib/trail_guide/variant.rb', line 3

def weight
  @weight
end

Instance Method Details

#==(other) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/trail_guide/variant.rb', line 17

def ==(other)
  if other.is_a?(self.class)
    return name == other.name && experiment == other.experiment
  elsif other.is_a?(String) || other.is_a?(Symbol)
    return name == other.to_s.underscore.to_sym
  end
end

#as_json(opts = {}) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/trail_guide/variant.rb', line 91

def as_json(opts={})
  {
    name: name,
    control: control?,
    weight: weight,
    metadata: .as_json,
  }
end

#control!Object

mark this variant as the control



29
30
31
# File 'lib/trail_guide/variant.rb', line 29

def control!
  @control = true
end

#control?Boolean

check if this variant is the control

Returns:

  • (Boolean)


34
35
36
# File 'lib/trail_guide/variant.rb', line 34

def control?
  !!@control
end

#converted(checkpoint = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/trail_guide/variant.rb', line 64

def converted(checkpoint=nil)
  if experiment.goals.empty?
    raise InvalidGoalError, "You provided the checkpoint `#{checkpoint}` but the experiment `#{experiment.experiment_name}` does not have any goals defined." unless checkpoint.nil?
    (TrailGuide.redis.hget(storage_key, 'converted') || 0).to_i
  elsif !checkpoint.nil?
    raise InvalidGoalError, "Invalid goal checkpoint `#{checkpoint}` for experiment `#{experiment.experiment_name}`." unless experiment.goals.any? { |goal| goal == checkpoint.to_s.underscore.to_sym }
    (TrailGuide.redis.hget(storage_key, checkpoint.to_s.underscore) || 0).to_i
  else
    experiment.goals.sum do |checkpoint|
      (TrailGuide.redis.hget(storage_key, checkpoint.to_s.underscore) || 0).to_i
    end
  end
end

#delete!Object



51
52
53
# File 'lib/trail_guide/variant.rb', line 51

def delete!
  TrailGuide.redis.del(storage_key)
end

#dup(experiment) ⇒ Object



5
6
7
# File 'lib/trail_guide/variant.rb', line 5

def dup(experiment)
  self.class.new(experiment, name, metadata: , weight: weight, control: control?)
end

#increment_conversion!(checkpoint = nil) ⇒ Object



86
87
88
89
# File 'lib/trail_guide/variant.rb', line 86

def increment_conversion!(checkpoint=nil)
  checkpoint ||= :converted
  TrailGuide.redis.hincrby(storage_key, checkpoint.to_s.underscore, 1)
end

#increment_participation!Object



82
83
84
# File 'lib/trail_guide/variant.rb', line 82

def increment_participation!
  TrailGuide.redis.hincrby(storage_key, 'participants', 1)
end

#participantsObject



60
61
62
# File 'lib/trail_guide/variant.rb', line 60

def participants
  (TrailGuide.redis.hget(storage_key, 'participants') || 0).to_i
end

#persisted?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/trail_guide/variant.rb', line 43

def persisted?
  TrailGuide.redis.exists(storage_key)
end

#reset!Object



55
56
57
58
# File 'lib/trail_guide/variant.rb', line 55

def reset!
  delete!
  save!
end

#save!Object



47
48
49
# File 'lib/trail_guide/variant.rb', line 47

def save!
  TrailGuide.redis.hsetnx(storage_key, 'name', name)
end

#storage_keyObject



104
105
106
# File 'lib/trail_guide/variant.rb', line 104

def storage_key
  "#{experiment.experiment_name}:#{name}"
end

#to_sObject



100
101
102
# File 'lib/trail_guide/variant.rb', line 100

def to_s
  name.to_s
end

#unconvertedObject



78
79
80
# File 'lib/trail_guide/variant.rb', line 78

def unconverted
  participants - converted
end

#variant!Object

unmark this variant as the control



39
40
41
# File 'lib/trail_guide/variant.rb', line 39

def variant!
  @control = false
end