class TrailGuide::Algorithms::Static

Public Class Methods

choose!(experiment, metadata: nil, &block) click to toggle source
# File lib/trail_guide/algorithms/static.rb, line 4
def self.choose!(experiment, metadata: nil, &block)
  new(experiment, &block).choose!(metadata: metadata)
end
new(experiment=nil, &block) click to toggle source
Calls superclass method TrailGuide::Algorithms::Algorithm::new
# File lib/trail_guide/algorithms/static.rb, line 8
def initialize(experiment=nil, &block)
  raise ArgumentError, 'You must provide a comparison block when using the static algorithm' unless block_given?
  @block = block
  super(experiment)
end

Public Instance Methods

choose!(metadata: nil) click to toggle source
# File lib/trail_guide/algorithms/static.rb, line 19
def choose!(metadata: nil)
  return control unless metadata.present?

  variant = variants.find do |variant|
    @block.call(variant.metadata, metadata)
  end
  
  variant || control
rescue => e
  TrailGuide.logger.error "#{e.class.name}: #{e.message}"
  TrailGuide.logger.error e.backtrace.first
  control
end
new(experiment) click to toggle source
# File lib/trail_guide/algorithms/static.rb, line 14
def new(experiment)
  TrailGuide.logger.warn "WARNING: Using the Static algorithm for an experiment which is configured with sticky_assignment. You should either use a different algorithm or configure sticky_assignment for the `#{experiment.experiment_name}` experiment." if experiment.configuration.sticky_assignment?
  self.class.new(experiment, &@block)
end