class TrailGuide::Variant

Attributes

experiment[R]
metadata[R]
name[R]
weight[R]

Public Class Methods

new(experiment, name, metadata: {}, weight: 1, control: false) click to toggle source
# 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 = metadata
  @weight = weight
  @control = control
end

Public Instance Methods

==(other) click to toggle source
# File lib/trail_guide/variant.rb, line 21
def ==(other)
  if other.is_a?(self.class)
    # TODO eventually remove the experiment requirement here once we start
    # taking advantage of === below
    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
===(other) click to toggle source
# File lib/trail_guide/variant.rb, line 31
def ===(other)
  return false unless other.is_a?(self.class)
  return name == other.name && experiment == other.experiment
end
adapter() click to toggle source
# File lib/trail_guide/variant.rb, line 17
def adapter
  @adapter ||= TrailGuide::Adapters::Variants::Redis.new(self)
end
as_json(opts={}) click to toggle source

export the variant state (not config) as json

# File lib/trail_guide/variant.rb, line 115
def as_json(opts={})
  if experiment.goals.empty?
    conversions = converted
  else
    conversions = experiment.goals.map { |g| [g.name, converted(g)] }.to_h
  end

  { name => { participants: participants,
              converted: conversions } }
end
control!() click to toggle source

mark this variant as the control

# File lib/trail_guide/variant.rb, line 40
def control!
  @control = true
end
control?() click to toggle source

check if this variant is the control

# File lib/trail_guide/variant.rb, line 45
def control?
  !!@control
end
converted(checkpoint=nil) click to toggle source
# File lib/trail_guide/variant.rb, line 75
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?
    (adapter.get(:converted) || 0).to_i
  elsif !checkpoint.nil?
    goal = experiment.goals.find { |g| g == checkpoint }
    raise InvalidGoalError, "Invalid goal checkpoint `#{checkpoint}` for experiment `#{experiment.experiment_name}`." if goal.nil?
    (adapter.get(goal.name) || 0).to_i
  else
    experiment.goals.sum do |goal|
      (adapter.get(goal.name) || 0).to_i
    end
  end
end
delete!() click to toggle source
# File lib/trail_guide/variant.rb, line 62
def delete!
  adapter.destroy
end
dup(experiment) click to toggle source
# File lib/trail_guide/variant.rb, line 5
def dup(experiment)
  self.class.new(experiment, name, metadata: metadata, weight: weight, control: control?)
end
increment_conversion!(checkpoint=nil) click to toggle source
# File lib/trail_guide/variant.rb, line 105
def increment_conversion!(checkpoint=nil)
  if checkpoint.nil?
    checkpoint = :converted
  else
    checkpoint = experiment.goals.find { |g| g == checkpoint }.name
  end
  adapter.increment(checkpoint)
end
increment_participation!() click to toggle source
# File lib/trail_guide/variant.rb, line 101
def increment_participation!
  adapter.increment(:participants)
end
measure(goal=nil, against=nil) click to toggle source
# File lib/trail_guide/variant.rb, line 94
def measure(goal=nil, against=nil)
  superset = against ? converted(against) : participants
  converts = converted(goal)
  return 0 if superset.zero? || converts.zero?
  converts.to_f / superset.to_f
end
participants() click to toggle source
# File lib/trail_guide/variant.rb, line 71
def participants
  (adapter.get(:participants) || 0).to_i
end
persisted?() click to toggle source
# File lib/trail_guide/variant.rb, line 54
def persisted?
  adapter.persisted?
end
reset!() click to toggle source
# File lib/trail_guide/variant.rb, line 66
def reset!
  delete!
  save!
end
save!() click to toggle source
# File lib/trail_guide/variant.rb, line 58
def save!
  adapter.setnx(:name, name)
end
storage_key() click to toggle source
# File lib/trail_guide/variant.rb, line 130
def storage_key
  "#{experiment.experiment_name}:#{name}"
end
to_s() click to toggle source
# File lib/trail_guide/variant.rb, line 126
def to_s
  name.to_s
end
unconverted() click to toggle source
# File lib/trail_guide/variant.rb, line 90
def unconverted
  participants - converted
end
variant!() click to toggle source

unmark this variant as the control

# File lib/trail_guide/variant.rb, line 50
def variant!
  @control = false
end