Class: TrailGuide::Helper::ExperimentProxy
Instance Attribute Summary collapse
Attributes inherited from HelperProxy
#context
Instance Method Summary
collapse
-
#choose(**opts, &block) ⇒ Object
(also: #enroll)
-
#choose!(**opts, &block) ⇒ Object
(also: #enroll!)
-
#convert(checkpoint = nil, **opts, &block) ⇒ Object
-
#convert!(checkpoint = nil, **opts, &block) ⇒ Object
-
#exclude_visitor? ⇒ Boolean
-
#experiment ⇒ Object
-
#experiments ⇒ Object
-
#initialize(context, key, **opts) ⇒ ExperimentProxy
constructor
A new instance of ExperimentProxy.
-
#override_variant ⇒ Object
-
#render(prefix: nil, templates: nil, locals: {}, **opts) ⇒ Object
-
#render!(prefix: nil, templates: nil, locals: {}, **opts) ⇒ Object
-
#run(methods: nil, **opts) ⇒ Object
-
#run!(methods: nil, **opts) ⇒ Object
Methods inherited from HelperProxy
#context_type, #new, #participant
Constructor Details
#initialize(context, key, **opts) ⇒ ExperimentProxy
Returns a new instance of ExperimentProxy.
127
128
129
130
|
# File 'lib/trail_guide/helper.rb', line 127
def initialize(context, key, **opts)
super(context, **opts)
@key = key.to_s.underscore.to_sym
end
|
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key.
125
126
127
|
# File 'lib/trail_guide/helper.rb', line 125
def key
@key
end
|
Instance Method Details
#choose(**opts, &block) ⇒ Object
Also known as:
enroll
146
147
148
149
150
151
152
153
|
# File 'lib/trail_guide/helper.rb', line 146
def choose(**opts, &block)
choose!(**opts, &block)
rescue NoExperimentsError => e
raise e
rescue => e
TrailGuide.logger.error e
experiment.control
end
|
#choose!(**opts, &block) ⇒ Object
Also known as:
enroll!
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/trail_guide/helper.rb', line 132
def choose!(**opts, &block)
raise NoExperimentsError, key if experiments.empty?
raise TooManyExperimentsError, "Selecting a variant requires a single experiment, but `#{key}` matches more than one experiment." if experiments.length > 1
raise TooManyExperimentsError, "Selecting a variant requires a single experiment, but `#{key}` refers to a combined experiment." if experiment.combined?
opts = {override: override_variant, excluded: exclude_visitor?}.merge(opts)
variant = experiment.choose!(**opts)
if block_given?
yield variant, opts[:metadata]
else
variant
end
end
|
#convert(checkpoint = nil, **opts, &block) ⇒ Object
247
248
249
250
251
252
|
# File 'lib/trail_guide/helper.rb', line 247
def convert(checkpoint=nil, **opts, &block)
convert!(checkpoint, **opts, &block)
rescue => e
TrailGuide.logger.error e
false
end
|
#convert!(checkpoint = nil, **opts, &block) ⇒ Object
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
# File 'lib/trail_guide/helper.rb', line 217
def convert!(checkpoint=nil, **opts, &block)
raise NoExperimentsError, key if experiments.empty?
checkpoints = experiments.map do |experiment|
ckpt = checkpoint || experiment.goals.find { |g| g == key }
if experiment.combined?
experiment.combined_experiments.map do |combo|
combo.convert!(ckpt, **opts)
end
else
experiment.convert!(ckpt, **opts)
end
end.flatten
return false unless checkpoints.any?
if block_given?
yield checkpoints, opts[:metadata]
else
checkpoints
end
rescue NoExperimentsError => e
unless TrailGuide.configuration.ignore_orphaned_groups?
trace = e.backtrace.find { |t| !t.match?(Regexp.new(__FILE__)) }
.to_s.split(Rails.root.to_s).last
.split(':').first(2).join(':')
TrailGuide.catalog.orphaned(key, trace)
end
false
end
|
#exclude_visitor? ⇒ Boolean
275
276
277
278
|
# File 'lib/trail_guide/helper.rb', line 275
def exclude_visitor?
return false if experiment.configuration.skip_request_filter?
context.send(:trailguide_excluded_request?)
end
|
#experiment ⇒ Object
260
261
262
|
# File 'lib/trail_guide/helper.rb', line 260
def experiment
@experiment ||= experiments.first
end
|
#experiments ⇒ Object
254
255
256
257
258
|
# File 'lib/trail_guide/helper.rb', line 254
def experiments
@experiments ||= TrailGuide.catalog.select(key).map do |experiment|
experiment.new(participant)
end
end
|
#override_variant ⇒ Object
264
265
266
267
268
269
270
271
272
273
|
# File 'lib/trail_guide/helper.rb', line 264
def override_variant
return unless context.respond_to?(:params, true)
params = context.send(:params)
return unless params.key?(TrailGuide.configuration.override_parameter)
experiment_params = params[TrailGuide.configuration.override_parameter]
return unless experiment_params.key?(experiment.experiment_name.to_s)
varname = experiment_params[experiment.experiment_name.to_s]
variant = experiment.variants.find { |var| var == varname }
variant.try(:name)
end
|
#render(prefix: nil, templates: nil, locals: {}, **opts) ⇒ Object
208
209
210
211
212
213
214
215
|
# File 'lib/trail_guide/helper.rb', line 208
def render(prefix: nil, templates: nil, locals: {}, **opts)
render!(prefix: prefix, templates: templates, locals: locals, **opts)
rescue NoExperimentsError => e
raise e
rescue => e
TrailGuide.logger.error e
false
end
|
#render!(prefix: nil, templates: nil, locals: {}, **opts) ⇒ Object
194
195
196
197
198
199
200
201
202
203
204
205
206
|
# File 'lib/trail_guide/helper.rb', line 194
def render!(prefix: nil, templates: nil, locals: {}, **opts)
raise UnsupportedContextError, "The current context (#{context}) does not support rendering. Rendering is only available in controllers and views." unless context.respond_to?(:render, true)
choose!(**opts) do |variant, metadata|
locals = { variant: variant, metadata: variant.metadata }.merge(locals)
locals = { locals: locals } if context_type == :controller
template = templates[variant.name] if templates
prefix ||= (context.try(:view_context) || context).lookup_context.prefixes.first + '/'
template ||= "#{prefix.to_s}#{variant.experiment.experiment_name.to_s.underscore}/#{variant.name.to_s.underscore}"
context.send(:render, template.to_s, **locals)
end
end
|
#run(methods: nil, **opts) ⇒ Object
185
186
187
188
189
190
191
192
|
# File 'lib/trail_guide/helper.rb', line 185
def run(methods: nil, **opts)
run!(methods: methods, **opts)
rescue NoExperimentsError => e
raise e
rescue => e
TrailGuide.logger.error e
false
end
|
#run!(methods: nil, **opts) ⇒ Object
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
# File 'lib/trail_guide/helper.rb', line 156
def run!(methods: nil, **opts)
choose!(**opts) do |variant, metadata|
varmeth = methods[variant.name] if methods
varmeth ||= variant.name
unless context.respond_to?(varmeth, true)
if context_type == :controller
raise NoVariantMethodError,
"Undefined local method `#{varmeth}`. You must define a controller method matching the variant `#{variant.name}` in your experiment `#{key}`. In this case it looks like you need to define #{context.class.name}##{varmeth}(metadata={})"
elsif context_type == :template
raise NoVariantMethodError,
"Undefined local method `#{varmeth}`. You must define a helper method matching the variant `#{variant.name}` in your experiment `#{key}`. In this case it looks like you need to define ApplicationHelper##{varmeth}(metadata={})"
else
raise NoVariantMethodError,
"Undefined local method `#{varmeth}`. You must define a method matching the variant `#{variant.name}` in your experiment `#{key}`. In this case it looks like you need to define #{context.class.name}##{varmeth}(metadata={})"
end
end
arguments = context.method(varmeth).parameters
if arguments.empty?
context.send(varmeth)
elsif arguments.length > 1 || arguments[0][0] == :rest
context.send(varmeth, variant, **variant.metadata)
elsif arguments.length == 1
context.send(varmeth, **variant.metadata)
end
end
end
|