module TrailGuide::Experiments::Lifecycle::ClassMethods

Public Instance Methods

calibrating?() click to toggle source
# File lib/trail_guide/experiments/lifecycle.rb, line 87
def calibrating?
  enable_calibration? && start_manually? && !started?
end
clear_winner!() click to toggle source
# File lib/trail_guide/experiments/lifecycle.rb, line 103
def clear_winner!
  adapter.delete(:winner)
end
declare_winner!(variant, context=nil) click to toggle source
# File lib/trail_guide/experiments/lifecycle.rb, line 95
def declare_winner!(variant, context=nil)
  variant = variants.find { |var| var == variant } unless variant.is_a?(Variant)
  return false unless variant.present? && variant.experiment == self
  run_callbacks(:on_winner, variant, context)
  adapter.set(:winner, variant.name)
  variant
end
fresh?() click to toggle source
# File lib/trail_guide/experiments/lifecycle.rb, line 91
def fresh?
  !started? && !scheduled? && !winner?
end
pause!(context=nil) click to toggle source
# File lib/trail_guide/experiments/lifecycle.rb, line 27
def pause!(context=nil)
  return false unless running? && configuration.can_resume?
  paused = adapter.set(:paused_at, Time.now.to_i)
  run_callbacks(:on_pause, context)
  paused
end
paused?() click to toggle source
# File lib/trail_guide/experiments/lifecycle.rb, line 73
def paused?
  time = paused_at
  time && time <= Time.now
end
paused_at() click to toggle source
# File lib/trail_guide/experiments/lifecycle.rb, line 53
def paused_at
  paused = adapter.get(:paused_at)
  return Time.at(paused.to_i) if paused
end
resume!(context=nil) click to toggle source
# File lib/trail_guide/experiments/lifecycle.rb, line 41
def resume!(context=nil)
  return false unless paused? && configuration.can_resume?
  resumed = adapter.delete(:paused_at)
  run_callbacks(:on_resume, context)
  !!resumed
end
run_callbacks(hook, *args) click to toggle source
# File lib/trail_guide/experiments/lifecycle.rb, line 115
def run_callbacks(hook, *args)
  return unless callbacks[hook]
  return args[0] if hook == :rollout_winner # TODO do we need to account for this case here at the class level?
  args.unshift(self)
  callbacks[hook].each do |callback|
    if callback.respond_to?(:call)
      callback.call(*args)
    else
      send(callback, *args)
    end
  end
end
running?() click to toggle source
# File lib/trail_guide/experiments/lifecycle.rb, line 83
def running?
  started? && !paused? && !stopped?
end
schedule!(start_at, stop_at=nil, context=nil) click to toggle source
# File lib/trail_guide/experiments/lifecycle.rb, line 18
def schedule!(start_at, stop_at=nil, context=nil)
  return false if started?
  save! unless persisted?
  scheduled = adapter.set(:started_at, start_at.to_i)
  adapter.set(:stopped_at, stop_at.to_i) if stop_at
  run_callbacks(:on_schedule, start_at, stop_at, context)
  scheduled
end
scheduled?() click to toggle source
# File lib/trail_guide/experiments/lifecycle.rb, line 68
def scheduled?
  time = started_at
  time && time > Time.now
end
start!(context=nil) click to toggle source
# File lib/trail_guide/experiments/lifecycle.rb, line 10
def start!(context=nil)
  return false if started?
  save! unless persisted?
  started = adapter.set(:started_at, Time.now.to_i)
  run_callbacks(:on_start, context)
  started
end
started?() click to toggle source
# File lib/trail_guide/experiments/lifecycle.rb, line 63
def started?
  time = started_at
  time && time <= Time.now
end
started_at() click to toggle source
# File lib/trail_guide/experiments/lifecycle.rb, line 48
def started_at
  started = adapter.get(:started_at)
  return Time.at(started.to_i) if started
end
stop!(context=nil) click to toggle source
# File lib/trail_guide/experiments/lifecycle.rb, line 34
def stop!(context=nil)
  return false unless started? && !stopped?
  stopped = adapter.set(:stopped_at, Time.now.to_i)
  run_callbacks(:on_stop, context)
  stopped
end
stopped?() click to toggle source
# File lib/trail_guide/experiments/lifecycle.rb, line 78
def stopped?
  time = stopped_at
  time && time <= Time.now
end
stopped_at() click to toggle source
# File lib/trail_guide/experiments/lifecycle.rb, line 58
def stopped_at
  stopped = adapter.get(:stopped_at)
  return Time.at(stopped.to_i) if stopped
end
winner?() click to toggle source
# File lib/trail_guide/experiments/lifecycle.rb, line 107
def winner?
  if combined?
    combined.all? { |combo| TrailGuide.catalog.find(combo).winner? }
  else
    adapter.exists?(:winner)
  end
end