Sha256: a764e8d05c69ff3a29523f50ef3ad034abc0257b36f23b89c2d887cb0cdb6d88

Contents?: true

Size: 1.9 KB

Versions: 7

Compression:

Stored size: 1.9 KB

Contents

require 'forwardable'

module Split
  class User
    extend Forwardable
    def_delegators :@user, :keys, :[], :[]=, :delete
    attr_reader :user

    def initialize(context, adapter=nil)
      @user = adapter || Split::Persistence.adapter.new(context)
    end

    def cleanup_old_experiments!
      keys_without_finished(user.keys).each do |key|
        experiment = ExperimentCatalog.find key_without_version(key)
        if experiment.nil? || experiment.has_winner? || experiment.start_time.nil?
          user.delete key
          user.delete Experiment.finished_key(key)
        end
      end
    end

    def max_experiments_reached?(experiment_key)
      if Split.configuration.allow_multiple_experiments == 'control'
        experiments = active_experiments
        count_control = experiments.count {|k,v| k == experiment_key || v == 'control'}
        experiments.size > count_control
      else
        !Split.configuration.allow_multiple_experiments &&
          keys_without_experiment(user.keys, experiment_key).length > 0
      end
    end

    def cleanup_old_versions!(experiment)
      keys = user.keys.select { |k| k.match(Regexp.new(experiment.name)) }
      keys_without_experiment(keys, experiment.key).each { |key| user.delete(key) }
    end

    def active_experiments
      experiment_pairs = {}
      user.keys.each do |key|
        Metric.possible_experiments(key_without_version(key)).each do |experiment|
          if !experiment.has_winner?
            experiment_pairs[key_without_version(key)] = user[key]
          end
        end
      end
      experiment_pairs
    end

    private

    def keys_without_experiment(keys, experiment_key)
      keys.reject { |k| k.match(Regexp.new("^#{experiment_key}(:finished)?$")) }
    end

    def keys_without_finished(keys)
      keys.reject { |k| k.include?(":finished") }
    end

    def key_without_version(key)
      key.split(/\:\d(?!\:)/)[0]
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
split-3.3.2 lib/split/user.rb
split-3.3.1 lib/split/user.rb
split-3.3.0 lib/split/user.rb
split-3.2.0 lib/split/user.rb
split-3.1.1 lib/split/user.rb
split-3.1.0 lib/split/user.rb
split-3.0.0 lib/split/user.rb