Class: TrailGuide::Helper::MetricProxy

Inherits:
HelperProxy show all
Defined in:
lib/trail_guide/helper.rb

Instance Attribute Summary collapse

Attributes inherited from HelperProxy

#context

Instance Method Summary collapse

Methods inherited from HelperProxy

#context_type, #new, #participant

Constructor Details

#initialize(context, metric, **opts) ⇒ MetricProxy

Returns a new instance of MetricProxy.

Raises:



127
128
129
130
131
# File 'lib/trail_guide/helper.rb', line 127

def initialize(context, metric, **opts)
  super(context, **opts)
  @metric = metric
  raise NoExperimentsError, "Could not find any experiments matching `#{metric}`." if experiments.empty?
end

Instance Attribute Details

#metricObject (readonly)

Returns the value of attribute metric.



125
126
127
# File 'lib/trail_guide/helper.rb', line 125

def metric
  @metric
end

Instance Method Details

#choose(**opts, &block) ⇒ Object Also known as: enroll



145
146
147
148
149
150
# File 'lib/trail_guide/helper.rb', line 145

def choose(**opts, &block)
  choose!(**opts, &block)
rescue => e
  TrailGuide.logger.error e
  experiment.control
end

#choose!(**opts, &block) ⇒ Object Also known as: enroll!



133
134
135
136
137
138
139
140
141
142
# File 'lib/trail_guide/helper.rb', line 133

def choose!(**opts, &block)
  raise TooManyExperimentsError, "Selecting a variant requires a single experiment, but the metric `#{metric}` matches more than one experiment." if experiments.length > 1
  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



220
221
222
223
224
225
# File 'lib/trail_guide/helper.rb', line 220

def convert(checkpoint=nil, **opts, &block)
  convert!(checkpoint, **opts, &block)
rescue => e
  TrailGuide.logger.error e
  false
end

#convert!(checkpoint = nil, **opts, &block) ⇒ Object



210
211
212
213
214
215
216
217
218
# File 'lib/trail_guide/helper.rb', line 210

def convert!(checkpoint=nil, **opts, &block)
  checkpoints = experiments.map { |experiment| experiment.convert!(checkpoint, **opts) }
  return false unless checkpoints.any?
  if block_given?
    yield checkpoints, opts[:metadata]
  else
    checkpoints
  end
end

#exclude_visitor?Boolean

Returns:

  • (Boolean)


248
249
250
251
# File 'lib/trail_guide/helper.rb', line 248

def exclude_visitor?
  return false if experiment.configuration.skip_request_filter?
  context.send(:trailguide_excluded_request?)
end

#experimentObject



233
234
235
# File 'lib/trail_guide/helper.rb', line 233

def experiment
  @experiment ||= experiments.first
end

#experimentsObject



227
228
229
230
231
# File 'lib/trail_guide/helper.rb', line 227

def experiments
  @experiments ||= TrailGuide.catalog.select(metric).map do |experiment|
    experiment.new(participant)
  end
end

#override_variantObject



237
238
239
240
241
242
243
244
245
246
# File 'lib/trail_guide/helper.rb', line 237

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



203
204
205
206
207
208
# File 'lib/trail_guide/helper.rb', line 203

def render(prefix: nil, templates: nil, locals: {}, **opts)
  render!(prefix: prefix, templates: templates, locals: locals, **opts)
rescue => e
  TrailGuide.logger.error e
  false
end

#render!(prefix: nil, templates: nil, locals: {}, **opts) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/trail_guide/helper.rb', line 189

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, |
    locals = { variant: variant, metadata: variant. }.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



182
183
184
185
186
187
# File 'lib/trail_guide/helper.rb', line 182

def run(methods: nil, **opts)
  run!(methods: methods, **opts)
rescue => e
  TrailGuide.logger.error e
  false
end

#run!(methods: nil, **opts) ⇒ Object



153
154
155
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
# File 'lib/trail_guide/helper.rb', line 153

def run!(methods: nil, **opts)
  choose!(**opts) do |variant, |
    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 `#{metric}`. 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 `#{metric}`. 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 `#{metric}`. 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.)
    elsif arguments.length == 1
      context.send(varmeth, **variant.)
    end
  end
end