Sha256: d55f0a2453f7873c50f3f7237fb9b5c57a30ba9d45e509aa30ffbb5df72d0dbb

Contents?: true

Size: 1.68 KB

Versions: 3

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

module Split
  class ExperimentCatalog
    # Return all experiments
    def self.all
      # Call compact to prevent nil experiments from being returned -- seems to happen during gem upgrades
      Split.redis.smembers(:experiments).map { |e| find(e) }.compact
    end

    # Return experiments without a winner (considered "active") first
    def self.all_active_first
      all.partition { |e| not e.winner }.map { |es| es.sort_by(&:name) }.flatten
    end

    def self.find(name)
      Experiment.find(name)
    end

    def self.find_or_initialize(metric_descriptor, control = nil, *alternatives)
      # Check if array is passed to ab_test
      # e.g. ab_test('name', ['Alt 1', 'Alt 2', 'Alt 3'])
      if control.is_a?(Array) && alternatives.length.zero?
        control, alternatives = control.first, control[1..-1]
      end

      experiment_name_with_version, goals = normalize_experiment(metric_descriptor)
      experiment_name = experiment_name_with_version.to_s.split(":")[0]
      Split::Experiment.new(experiment_name,
          alternatives: [control].compact + alternatives, goals: goals)
    end

    def self.find_or_create(metric_descriptor, control = nil, *alternatives)
      experiment = find_or_initialize(metric_descriptor, control, *alternatives)
      experiment.save
    end

    def self.normalize_experiment(metric_descriptor)
      if Hash === metric_descriptor
        experiment_name = metric_descriptor.keys.first
        goals = Array(metric_descriptor.values.first)
      else
        experiment_name = metric_descriptor
        goals = []
      end
      return experiment_name, goals
    end
    private_class_method :normalize_experiment
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
split-4.0.4 lib/split/experiment_catalog.rb
split-4.0.3 lib/split/experiment_catalog.rb
split-4.0.2 lib/split/experiment_catalog.rb