Class: Split::Experiment
- Inherits:
-
Object
- Object
- Split::Experiment
- Defined in:
- lib/split/experiment.rb
Instance Attribute Summary collapse
-
#alternative_names ⇒ Object
Returns the value of attribute alternative_names.
-
#name ⇒ Object
Returns the value of attribute name.
-
#version ⇒ Object
Returns the value of attribute version.
-
#winner ⇒ Object
Returns the value of attribute winner.
Class Method Summary collapse
- .all ⇒ Object
- .find(name) ⇒ Object
- .find_or_create(key, *alternatives) ⇒ Object
- .initialize_alternatives(alternatives, name) ⇒ Object
- .load_alternatives_for(name) ⇒ Object
Instance Method Summary collapse
- #alternatives ⇒ Object
- #control ⇒ Object
- #delete ⇒ Object
- #increment_version ⇒ Object
-
#initialize(name, *alternative_names) ⇒ Experiment
constructor
A new instance of Experiment.
- #key ⇒ Object
- #new_record? ⇒ Boolean
- #next_alternative ⇒ Object
- #random_alternative ⇒ Object
- #reset ⇒ Object
- #reset_winner ⇒ Object
- #save ⇒ Object
Constructor Details
#initialize(name, *alternative_names) ⇒ Experiment
Returns a new instance of Experiment.
8 9 10 11 12 13 14 15 |
# File 'lib/split/experiment.rb', line 8 def initialize(name, *alternative_names) @name = name.to_s @alternative_names = alternative_names.map do |alternative| Split::Alternative.new(alternative, name) end.map(&:name) @version = (Split.redis.get("#{name.to_s}:version").to_i || 0) end |
Instance Attribute Details
#alternative_names ⇒ Object
Returns the value of attribute alternative_names.
4 5 6 |
# File 'lib/split/experiment.rb', line 4 def alternative_names @alternative_names end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/split/experiment.rb', line 3 def name @name end |
#version ⇒ Object
Returns the value of attribute version.
6 7 8 |
# File 'lib/split/experiment.rb', line 6 def version @version end |
#winner ⇒ Object
Returns the value of attribute winner.
5 6 7 |
# File 'lib/split/experiment.rb', line 5 def winner @winner end |
Class Method Details
.all ⇒ Object
111 112 113 |
# File 'lib/split/experiment.rb', line 111 def self.all Array(Split.redis.smembers(:experiments)).map {|e| find(e)} end |
.find(name) ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/split/experiment.rb', line 115 def self.find(name) if Split.redis.exists(name) self.new(name, *load_alternatives_for(name)) else raise 'Experiment not found' end end |
.find_or_create(key, *alternatives) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/split/experiment.rb', line 123 def self.find_or_create(key, *alternatives) name = key.to_s.split(':')[0] if alternatives.length == 1 if alternatives[0].is_a? Hash alternatives = alternatives[0].map{|k,v| {k => v} } else raise InvalidArgument, 'You must declare at least 2 alternatives' end end alts = initialize_alternatives(alternatives, name) if Split.redis.exists(name) if load_alternatives_for(name) == alts.map(&:name) experiment = self.new(name, *load_alternatives_for(name)) else exp = self.new(name, *load_alternatives_for(name)) exp.reset exp.alternatives.each(&:delete) experiment = self.new(name, *alternatives) experiment.save end else experiment = self.new(name, *alternatives) experiment.save end return experiment end |
.initialize_alternatives(alternatives, name) ⇒ Object
154 155 156 157 158 159 160 161 162 163 |
# File 'lib/split/experiment.rb', line 154 def self.initialize_alternatives(alternatives, name) if alternatives.reject {|a| Split::Alternative.valid? a}.any? raise InvalidArgument, 'Alternatives must be strings' end alternatives.map do |alternative| Split::Alternative.new(alternative, name) end end |
.load_alternatives_for(name) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/split/experiment.rb', line 99 def self.load_alternatives_for(name) case Split.redis.type(name) when 'set' # convert legacy sets to lists alts = Split.redis.smembers(name) Split.redis.del(name) alts.reverse.each {|a| Split.redis.lpush(name, a) } Split.redis.lrange(name, 0, -1) else Split.redis.lrange(name, 0, -1) end end |
Instance Method Details
#alternatives ⇒ Object
37 38 39 |
# File 'lib/split/experiment.rb', line 37 def alternatives @alternative_names.map {|a| Split::Alternative.find_or_create(a, name)} end |
#control ⇒ Object
25 26 27 |
# File 'lib/split/experiment.rb', line 25 def control alternatives.first end |
#delete ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/split/experiment.rb', line 80 def delete alternatives.each(&:delete) reset_winner Split.redis.srem(:experiments, name) Split.redis.del(name) increment_version end |
#increment_version ⇒ Object
61 62 63 64 |
# File 'lib/split/experiment.rb', line 61 def increment_version @version += 1 Split.redis.set("#{name}:version", @version) end |
#key ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/split/experiment.rb', line 66 def key if version.to_i > 0 "#{name}:#{version}" else name end end |
#new_record? ⇒ Boolean
88 89 90 |
# File 'lib/split/experiment.rb', line 88 def new_record? !Split.redis.exists(name) end |
#next_alternative ⇒ Object
41 42 43 |
# File 'lib/split/experiment.rb', line 41 def next_alternative winner || random_alternative end |
#random_alternative ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/split/experiment.rb', line 45 def random_alternative weights = alternatives.map(&:weight) total = weights.inject(0.0) {|t,w| t+w} point = rand * total alternatives.zip(weights).each do |n,w| return n if w >= point point -= w end end |
#reset ⇒ Object
74 75 76 77 78 |
# File 'lib/split/experiment.rb', line 74 def reset alternatives.each(&:reset) reset_winner increment_version end |