Class: Split::Trial
- Inherits:
-
Object
- Object
- Split::Trial
- Defined in:
- lib/split/trial.rb
Instance Attribute Summary collapse
-
#experiment ⇒ Object
Returns the value of attribute experiment.
Instance Method Summary collapse
- #alternative ⇒ Object
- #alternative=(alternative) ⇒ Object
-
#choose!(context = nil) ⇒ Object
Choose an alternative, add a participant, and save the alternative choice on the user.
- #complete!(goals, context = nil) ⇒ Object
-
#initialize(attrs = {}) ⇒ Trial
constructor
A new instance of Trial.
Constructor Details
#initialize(attrs = {}) ⇒ Trial
Returns a new instance of Trial.
5 6 7 8 9 10 11 12 13 |
# File 'lib/split/trial.rb', line 5 def initialize(attrs = {}) self.experiment = attrs.delete(:experiment) self.alternative = attrs.delete(:alternative) @user = attrs.delete(:user) @options = attrs @alternative_choosen = false end |
Instance Attribute Details
#experiment ⇒ Object
Returns the value of attribute experiment.
3 4 5 |
# File 'lib/split/trial.rb', line 3 def experiment @experiment end |
Instance Method Details
#alternative ⇒ Object
15 16 17 18 19 |
# File 'lib/split/trial.rb', line 15 def alternative @alternative ||= if @experiment.has_winner? @experiment.winner end end |
#alternative=(alternative) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/split/trial.rb', line 21 def alternative=(alternative) @alternative = if alternative.kind_of?(Split::Alternative) alternative else @experiment.alternatives.find{|a| a.name == alternative } end end |
#choose!(context = nil) ⇒ Object
Choose an alternative, add a participant, and save the alternative choice on the user. This method is guaranteed to only run once, and will skip the alternative choosing process if run a second time.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/split/trial.rb', line 47 def choose!(context = nil) # Only run the process once return alternative if @alternative_choosen if @options[:override] self.alternative = @options[:override] elsif @options[:disabled] || !Split.configuration.enabled self.alternative = @experiment.control elsif @experiment.has_winner? self.alternative = @experiment.winner else cleanup_old_versions if exclude_user? self.alternative = @experiment.control elsif @user[@experiment.key] self.alternative = @user[@experiment.key] else self.alternative = @experiment.next_alternative # Increment the number of participants since we are actually choosing a new alternative self.alternative.increment_participation # Run the post-choosing hook on the context context.send(Split.configuration.on_trial_choose, self) \ if Split.configuration.on_trial_choose && context end end @user[@experiment.key] = alternative.name if should_store_alternative? @alternative_choosen = true alternative end |
#complete!(goals, context = nil) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/split/trial.rb', line 29 def complete!(goals, context = nil) goals = goals || [] if alternative if goals.empty? alternative.increment_completion else goals.each {|g| alternative.increment_completion(g) } end context.send(Split.configuration.on_trial_complete, self) \ if Split.configuration.on_trial_complete && context end end |