Class: Vanity::Experiment::Base
Overview
Base class that all experiment types are derived from.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#completed_at ⇒ Object
readonly
Time stamp when experiment was completed.
-
#id ⇒ Object
readonly
Unique identifier, derived from name experiment name, e.g.
-
#name ⇒ Object
(also: #to_s)
readonly
Human readable experiment name (first argument you pass when creating a new experiment).
Class Method Summary collapse
-
.load(playground, stack, file) ⇒ Object
Playground uses this to load experiment definitions.
-
.type ⇒ Object
Returns the type of this class as a symbol (e.g. AbTest becomes ab_test).
Instance Method Summary collapse
-
#active? ⇒ Boolean
Returns true if experiment active, false if completed.
-
#complete! ⇒ Object
Force experiment to complete.
-
#complete_if(&block) ⇒ Object
Define experiment completion condition.
-
#created_at ⇒ Object
Time stamp when experiment was created.
-
#description(text = nil) ⇒ Object
Sets or returns description.
-
#destroy ⇒ Object
Get rid of all experiment data.
-
#identify(&block) ⇒ Object
Defines how we obtain an identity for the current experiment.
-
#initialize(playground, id, name, options = nil) ⇒ Base
constructor
A new instance of Base.
-
#save ⇒ Object
Called by Playground to save the experiment definition.
-
#type ⇒ Object
Returns the type of this experiment as a symbol (e.g. :ab_test).
Constructor Details
#initialize(playground, id, name, options = nil) ⇒ Base
Returns a new instance of Base.
67 68 69 70 71 72 |
# File 'lib/vanity/experiment/base.rb', line 67 def initialize(playground, id, name, = nil) @playground = playground @id, @name = id.to_sym, name @options = || {} @identify_block = method(:default_identify) end |
Instance Attribute Details
#completed_at ⇒ Object (readonly)
Time stamp when experiment was completed.
89 90 91 |
# File 'lib/vanity/experiment/base.rb', line 89 def completed_at @completed_at end |
#id ⇒ Object (readonly)
Unique identifier, derived from name experiment name, e.g. “Green Button” becomes :green_button.
81 82 83 |
# File 'lib/vanity/experiment/base.rb', line 81 def id @id end |
#name ⇒ Object (readonly) Also known as: to_s
Human readable experiment name (first argument you pass when creating a new experiment).
76 77 78 |
# File 'lib/vanity/experiment/base.rb', line 76 def name @name end |
Class Method Details
.load(playground, stack, file) ⇒ Object
Playground uses this to load experiment definitions.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/vanity/experiment/base.rb', line 45 def load(playground, stack, file) fail "Circular dependency detected: #{stack.join('=>')}=>#{file}" if stack.include?(file) source = File.read(file) stack.push file id = File.basename(file, ".rb").downcase.gsub(/\W/, "_").to_sym context = Object.new context.instance_eval do extend Definition experiment = eval(source, context.new_binding(playground, id), file) fail NameError.new("Expected #{file} to define experiment #{id}", id) unless playground.experiments[id] experiment end rescue error = NameError.exception($!., id) error.set_backtrace $!.backtrace raise error ensure stack.pop end |
.type ⇒ Object
Returns the type of this class as a symbol (e.g. AbTest becomes ab_test).
40 41 42 |
# File 'lib/vanity/experiment/base.rb', line 40 def type name.split("::").last.gsub(/([a-z])([A-Z])/) { "#{$1}_#{$2}" }.gsub(/([A-Z])([A-Z][a-z])/) { "#{$1}_#{$2}" }.downcase end |
Instance Method Details
#active? ⇒ Boolean
Returns true if experiment active, false if completed.
155 156 157 |
# File 'lib/vanity/experiment/base.rb', line 155 def active? !@playground.collecting? || !connection.is_experiment_completed?(@id) end |
#complete! ⇒ Object
Force experiment to complete.
142 143 144 145 146 147 |
# File 'lib/vanity/experiment/base.rb', line 142 def complete! @playground.logger.info "vanity: completed experiment #{id}" return unless @playground.collecting? connection.set_experiment_completed_at @id, Time.now @completed_at = connection.get_experiment_completed_at(@id) end |
#complete_if(&block) ⇒ Object
Define experiment completion condition. For example:
complete_if do
!score(95).chosen.nil?
end
135 136 137 138 139 |
# File 'lib/vanity/experiment/base.rb', line 135 def complete_if(&block) raise ArgumentError, "Missing block" unless block raise "complete_if already called on this experiment" if @complete_block @complete_block = block end |
#created_at ⇒ Object
Time stamp when experiment was created.
84 85 86 |
# File 'lib/vanity/experiment/base.rb', line 84 def created_at @created_at ||= connection.get_experiment_created_at(@id) end |
#description(text = nil) ⇒ Object
Sets or returns description. For example
ab_test "Simple" do
description "A simple A/B experiment"
end
puts "Just defined: " + experiment(:simple).description
123 124 125 126 |
# File 'lib/vanity/experiment/base.rb', line 123 def description(text = nil) @description = text if text @description end |
#destroy ⇒ Object
Get rid of all experiment data.
162 163 164 165 |
# File 'lib/vanity/experiment/base.rb', line 162 def destroy connection.destroy_experiment @id @created_at = @completed_at = nil end |
#identify(&block) ⇒ Object
Defines how we obtain an identity for the current experiment. Usually Vanity gets the identity form a session object (see use_vanity), but there are cases where you want a particular experiment to use a different identity.
For example, if all your experiments use current_user and you need one experiment to use the current project:
ab_test "Project widget" do
alternatives :small, :medium, :large
identify do |controller|
controller.project.id
end
end
109 110 111 112 |
# File 'lib/vanity/experiment/base.rb', line 109 def identify(&block) fail "Missing block" unless block @identify_block = block end |
#save ⇒ Object
Called by Playground to save the experiment definition.
168 169 170 171 |
# File 'lib/vanity/experiment/base.rb', line 168 def save return unless @playground.collecting? connection.set_experiment_created_at @id, Time.now end |
#type ⇒ Object
Returns the type of this experiment as a symbol (e.g. :ab_test).
92 93 94 |
# File 'lib/vanity/experiment/base.rb', line 92 def type self.class.type end |