Module: Split::Helper

Defined in:
lib/split/helper.rb

Instance Method Summary collapse

Instance Method Details

#ab_test(experiment_name, control = nil, *alternatives) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/split/helper.rb', line 4

def ab_test(experiment_name, control=nil, *alternatives)
  if RUBY_VERSION.match(/1\.8/) && alternatives.length.zero? && ! control.nil?
    puts 'WARNING: You should always pass the control alternative through as the second argument with any other alternatives as the third because the order of the hash is not preserved in ruby 1.8'
  end

  begin
    ret = if Split.configuration.enabled
      load_and_start_trial(experiment_name, control, alternatives)
    else
      control_variable(control)
    end

  rescue => e
    raise(e) unless Split.configuration.db_failover
    Split.configuration.db_failover_on_db_error.call(e)

    if Split.configuration.db_failover_allow_parameter_override && override_present?(experiment_name)
      ret = override_alternative(experiment_name)
    end
  ensure
    if ret.nil?
      ret = control_variable(control)
    end
  end

  if block_given?
    if defined?(capture) # a block in a rails view
      block = Proc.new { yield(ret) }
      concat(capture(ret, &block))
      false
    else
      yield(ret)
    end
  else
    ret
  end
end

#ab_userObject



91
92
93
# File 'lib/split/helper.rb', line 91

def ab_user
  @ab_user ||= Split::Persistence.adapter.new(self)
end

#begin_experiment(experiment, alternative_name = nil) ⇒ Object



85
86
87
88
89
# File 'lib/split/helper.rb', line 85

def begin_experiment(experiment, alternative_name = nil)
  alternative_name ||= experiment.control.name
  ab_user[experiment.key] = alternative_name
  alternative_name
end

#clean_old_versions(experiment) ⇒ Object



107
108
109
110
111
# File 'lib/split/helper.rb', line 107

def clean_old_versions(experiment)
  old_versions(experiment).each do |old_key|
    ab_user.delete old_key
  end
end

#doing_other_tests?(experiment_key) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/split/helper.rb', line 103

def doing_other_tests?(experiment_key)
  keys_without_experiment(ab_user.keys, experiment_key).length > 0
end

#exclude_visitor?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/split/helper.rb', line 95

def exclude_visitor?
  is_robot? || is_ignored_ip_address?
end

#finish_experiment(experiment, options = {:reset => true}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/split/helper.rb', line 46

def finish_experiment(experiment, options = {:reset => true})
  should_reset = experiment.resettable? && options[:reset]
  if ab_user[experiment.finished_key] && !should_reset
    return true
  else
    alternative_name = ab_user[experiment.key]
    trial = Trial.new(:experiment => experiment, :alternative_name => alternative_name)
    trial.complete!
    if should_reset
      reset!(experiment)
    else
      ab_user[experiment.finished_key] = true
    end
  end
end

#finished(metric_name, options = {:reset => true}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/split/helper.rb', line 63

def finished(metric_name, options = {:reset => true})
  return if exclude_visitor? || Split.configuration.disabled?
  experiments = Metric.possible_experiments(metric_name)

  if experiments.any?
    experiments.each do |experiment|
      finish_experiment(experiment, options)
    end
  end
rescue => e
  raise unless Split.configuration.db_failover
  Split.configuration.db_failover_on_db_error.call(e)
end

#is_ignored_ip_address?Boolean

Returns:

  • (Boolean)


126
127
128
129
130
131
132
# File 'lib/split/helper.rb', line 126

def is_ignored_ip_address?
  if Split.configuration.ignore_ip_addresses.any?
    Split.configuration.ignore_ip_addresses.include?(request.ip)
  else
    false
  end
end

#is_robot?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/split/helper.rb', line 122

def is_robot?
  request.user_agent =~ Split.configuration.robot_regex
end

#not_allowed_to_test?(experiment_key) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/split/helper.rb', line 99

def not_allowed_to_test?(experiment_key)
  !Split.configuration.allow_multiple_experiments && doing_other_tests?(experiment_key)
end

#old_versions(experiment) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/split/helper.rb', line 113

def old_versions(experiment)
  if experiment.version > 0
    keys = ab_user.keys.select { |k| k.match(Regexp.new(experiment.name)) }
    keys_without_experiment(keys, experiment.key)
  else
    []
  end
end

#override_alternative(experiment_name) ⇒ Object



81
82
83
# File 'lib/split/helper.rb', line 81

def override_alternative(experiment_name)
  params[experiment_name] if override_present?(experiment_name)
end

#override_present?(experiment_name) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/split/helper.rb', line 77

def override_present?(experiment_name)
  defined?(params) && params[experiment_name]
end

#reset!(experiment) ⇒ Object



42
43
44
# File 'lib/split/helper.rb', line 42

def reset!(experiment)
  ab_user.delete(experiment.key)
end