Class: TrailGuide::Catalog

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/trail_guide/catalog.rb

Defined Under Namespace

Classes: DSL

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(experiments = []) ⇒ Catalog

Returns a new instance of Catalog.



74
75
76
# File 'lib/trail_guide/catalog.rb', line 74

def initialize(experiments=[])
  @experiments = experiments
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



188
189
190
191
# File 'lib/trail_guide/catalog.rb', line 188

def method_missing(meth, *args, &block)
  return experiments.send(meth, *args, &block) if experiments.respond_to?(meth, true)
  super
end

Instance Attribute Details

#experimentsObject (readonly)

Returns the value of attribute experiments.



72
73
74
# File 'lib/trail_guide/catalog.rb', line 72

def experiments
  @experiments
end

Class Method Details

.catalogObject



6
7
8
# File 'lib/trail_guide/catalog.rb', line 6

def catalog
  @catalog ||= new
end

.combined_experiment(combined, name) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/trail_guide/catalog.rb', line 58

def combined_experiment(combined, name)
  experiment = Class.new(TrailGuide::CombinedExperiment)
  experiment.configure combined.configuration.to_h.merge({
    name: name.to_s.underscore.to_sym,
    parent: combined,
    combined: [],
    variants: combined.configuration.variants.map { |var| var.dup(experiment) }
    # TODO also map goals once they're separate classes
  })
  experiment
end

.load_experiments!Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/trail_guide/catalog.rb', line 10

def load_experiments!
  @catalog = nil

  # Load experiments from YAML configs if any exists
  load_yaml_experiments(Rails.root.join("config/experiments.yml"))
  Dir[Rails.root.join("config/experiments/**/*.yml")].each { |f| load_yaml_experiments(f) }

  # Load experiments from ruby configs if any exist
  DSL.instance_eval(File.read(Rails.root.join("config/experiments.rb"))) if File.exists?(Rails.root.join("config/experiments.rb"))
  Dir[Rails.root.join("config/experiments/**/*.rb")].each { |f| DSL.instance_eval(File.read(f)) }

  # Load any experiment classes defined in the app
  Dir[Rails.root.join("app/experiments/**/*.rb")].each { |f| load f }
end

.load_yaml_experiments(file) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/trail_guide/catalog.rb', line 25

def load_yaml_experiments(file)
  experiments = (YAML.load_file(file) || {} rescue {})
    .symbolize_keys.map { |k,v| [k, v.symbolize_keys] }.to_h

  experiments.each do |name, options|
    expvars = options[:variants].map do |var|
      if var.is_a?(Array)
        [var[0], var[1].symbolize_keys]
      else
        [var]
      end
    end

    DSL.experiment(name) do |config|
      expvars.each do |expvar|
        variant *expvar
      end
      # TODO also map goals once they're real classes
      config.control                    = options[:control] if options[:control]
      config.metric                     = options[:metric] if options[:metric]
      config.algorithm                  = options[:algorithm] if options[:algorithm]
      config.goals                      = options[:goals] if options[:goals]
      config.combined                   = options[:combined] if options[:combined]
      config.reset_manually             = options[:reset_manually] if options.key?(:reset_manually)
      config.start_manually             = options[:start_manually] if options.key?(:start_manually)
      config.store_override             = options[:store_override] if options.key?(:store_override)
      config.track_override             = options[:track_override] if options.key?(:track_override)
      config.allow_multiple_conversions = options[:allow_multiple_conversions] if options.key?(:allow_multiple_conversions)
      config.allow_multiple_goals       = options[:allow_multiple_goals] if options.key?(:allow_multiple_goals)
    end
  end
end

Instance Method Details

#allObject



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/trail_guide/catalog.rb', line 82

def all
  exploded = experiments.map do |exp|
    if exp.combined?
      exp.combined.map { |name| combined_experiment(exp, name) }
    else
      exp
    end
  end.flatten

  self.class.new(exploded)
end

#by_startedObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/trail_guide/catalog.rb', line 118

def by_started
  scoped = to_a.sort do |a,b|
    if a.running? && !b.running?
      1
    elsif !a.running? && b.running?
      -1
    else
      if a.started? && !b.started?
        1
      elsif !a.started? && b.started?
        -1
      elsif a.started? && b.started?
        a.started_at <=> b.started_at
      else
        b.experiment_name.to_s <=> a.experiment_name.to_s
      end
    end
  end.reverse

  self.class.new(scoped)
end

#each(&block) ⇒ Object



78
79
80
# File 'lib/trail_guide/catalog.rb', line 78

def each(&block)
  experiments.each(&block)
end

#endedObject



110
111
112
# File 'lib/trail_guide/catalog.rb', line 110

def ended
  self.class.new(to_a.select(&:winner?))
end

#find(name) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/trail_guide/catalog.rb', line 140

def find(name)
  if name.is_a?(Class)
    experiments.find { |exp| exp == name }
  else
    experiment = experiments.find do |exp|
      exp.experiment_name == name.to_s.underscore.to_sym ||
        exp.metric == name.to_s.underscore.to_sym ||
        exp.name == name.to_s.classify
    end
    return experiment if experiment.present?

    combined = experiments.find do |exp|
      next unless exp.combined?
      exp.combined.any? { |combo| combo.to_s.underscore.to_sym == name.to_s.underscore.to_sym }
    end
    return nil unless combined.present?

    return combined_experiment(combined, name)
  end
end

#not_runningObject



114
115
116
# File 'lib/trail_guide/catalog.rb', line 114

def not_running
  self.class.new(to_a.select { |e| !e.running? })
end

#pausedObject



102
103
104
# File 'lib/trail_guide/catalog.rb', line 102

def paused
  self.class.new(to_a.select(&:paused?))
end

#register(klass) ⇒ Object



183
184
185
186
# File 'lib/trail_guide/catalog.rb', line 183

def register(klass)
  experiments << klass unless experiments.any? { |exp| exp == klass }
  klass
end

#respond_to_missing?(meth, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


193
194
195
# File 'lib/trail_guide/catalog.rb', line 193

def respond_to_missing?(meth, include_private=false)
  experiments.respond_to?(meth, include_private)
end

#runningObject



98
99
100
# File 'lib/trail_guide/catalog.rb', line 98

def running
  self.class.new(to_a.select(&:running?))
end

#select(name) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/trail_guide/catalog.rb', line 161

def select(name)
  if name.is_a?(Class)
    selected = experiments.select { |exp| exp == name }
  else
    # TODO we can be more efficient than mapping twice here
    selected = experiments.select do |exp|
      exp.experiment_name == name.to_s.underscore.to_sym ||
        exp.metric == name.to_s.underscore.to_sym ||
        exp.name == name.to_s.classify ||
        (exp.combined? && exp.combined.any? { |combo| combo.to_s.underscore.to_sym == name.to_s.underscore.to_sym })
    end.map do |exp|
      if exp.combined? && exp.combined.any? { |combo| combo.to_s.underscore.to_sym == name.to_s.underscore.to_sym }
        combined_experiment(exp, name)
      else
        exp
      end
    end
  end

  self.class.new(selected)
end

#startedObject



94
95
96
# File 'lib/trail_guide/catalog.rb', line 94

def started
  self.class.new(to_a.select(&:started?))
end

#stoppedObject



106
107
108
# File 'lib/trail_guide/catalog.rb', line 106

def stopped
  self.class.new(to_a.select(&:stopped?))
end