Class: Split::User

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/split/user.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, adapter = nil) ⇒ User

Returns a new instance of User.



7
8
9
# File 'lib/split/user.rb', line 7

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

Instance Attribute Details

#userObject (readonly)

Returns the value of attribute user.



5
6
7
# File 'lib/split/user.rb', line 5

def user
  @user
end

Instance Method Details

#active_experimentsObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/split/user.rb', line 37

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

#cleanup_old_experiments!Object



11
12
13
14
15
16
17
18
19
# File 'lib/split/user.rb', line 11

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

#cleanup_old_versions!(experiment) ⇒ Object



32
33
34
35
# File 'lib/split/user.rb', line 32

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

#max_experiments_reached?(experiment_key) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
# File 'lib/split/user.rb', line 21

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