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.



5
6
7
8
9
10
11
# File 'lib/trail_guide/variant.rb', line 5

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



13
14
15
16
17
18
19
# File 'lib/trail_guide/variant.rb', line 13

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



86
87
88
89
90
91
92
93
# File 'lib/trail_guide/variant.rb', line 86

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

#control!Object

mark this variant as the control



25
26
27
# File 'lib/trail_guide/variant.rb', line 25

def control!
  @control = true
end

#control?Boolean

check if this variant is the control

Returns:

  • (Boolean)


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

def control?
  !!@control
end

#converted(checkpoint = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/trail_guide/variant.rb', line 59

def converted(checkpoint=nil)
  if experiment.funnels.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.funnels.any? { |funnel| funnel == checkpoint.to_s.underscore.to_sym }
    (TrailGuide.redis.hget(storage_key, checkpoint.to_s.underscore) || 0).to_i
  else
    experiment.funnels.sum do |checkpoint|
      (TrailGuide.redis.hget(storage_key, checkpoint.to_s.underscore) || 0).to_i
    end
  end
end

#delete!Object



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

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

#increment_conversion!(checkpoint = nil) ⇒ Object



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

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

#increment_participation!Object



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

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

#participantsObject



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

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

#persisted?Boolean

Returns:

  • (Boolean)


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

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

#reset!Object



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

def reset!
  delete! && save!
end

#save!Object



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

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

#storage_keyObject



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

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

#to_sObject



95
96
97
# File 'lib/trail_guide/variant.rb', line 95

def to_s
  name.to_s
end

#unconvertedObject



73
74
75
# File 'lib/trail_guide/variant.rb', line 73

def unconverted
  participants - converted
end

#variant!Object

unmark this variant as the control



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

def variant!
  @control = false
end