Class: Absurdity::Experiment

Inherits:
Object
  • Object
show all
Defined in:
lib/absurdity/experiment.rb

Defined Under Namespace

Classes: FoundError, NotFoundError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(slug, attributes = {}) ⇒ Experiment

Returns a new instance of Experiment.



27
28
29
30
# File 'lib/absurdity/experiment.rb', line 27

def initialize(slug, attributes = {})
  @slug       = slug
  @attributes = attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



26
27
28
# File 'lib/absurdity/experiment.rb', line 26

def attributes
  @attributes
end

#slugObject (readonly)

Returns the value of attribute slug.



26
27
28
# File 'lib/absurdity/experiment.rb', line 26

def slug
  @slug
end

Class Method Details

.allObject



22
23
24
# File 'lib/absurdity/experiment.rb', line 22

def self.all
  Datastore.all_experiments
end

.create(slug, metrics_list, variants_list = nil) ⇒ Object

Raises:



6
7
8
9
10
11
# File 'lib/absurdity/experiment.rb', line 6

def self.create(slug, metrics_list, variants_list=nil)
  raise FoundError if Datastore.find(self, slug: slug)
  experiment = new(slug, metrics_list: metrics_list, variants_list: variants_list)
  experiment.save
  experiment
end

.find(slug) ⇒ Object

Raises:



13
14
15
16
# File 'lib/absurdity/experiment.rb', line 13

def self.find(slug)
  raise NotFoundError unless experiment = Datastore.find(self, slug: slug)
  experiment
end

.reportsObject



18
19
20
# File 'lib/absurdity/experiment.rb', line 18

def self.reports
  all.map { |exp| exp.report }
end

Instance Method Details

#==(other_experiment) ⇒ Object



84
85
86
87
88
# File 'lib/absurdity/experiment.rb', line 84

def ==(other_experiment)
  slug            == other_experiment.slug         &&
  metrics_list    == other_experiment.metrics_list &&
  variants_list   == other_experiment.variants_list
end

#complete(variant_slug) ⇒ Object



59
60
61
62
# File 'lib/absurdity/experiment.rb', line 59

def complete(variant_slug)
  @attributes[:completed] = variant_slug
  save
end

#completedObject



64
65
66
# File 'lib/absurdity/experiment.rb', line 64

def completed
  @attributes[:completed]
end

#count(metric_slug) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/absurdity/experiment.rb', line 68

def count(metric_slug)
  if variants?
    count = {}
    variants_list.each do |variant|
      count[variant] = metric(metric_slug, variant).count
    end
  else
    count = metric(metric_slug).count
  end
  count
end

#metric(metric_slug, variant = nil) ⇒ Object



80
81
82
# File 'lib/absurdity/experiment.rb', line 80

def metric(metric_slug, variant=nil)
  Metric.find(metric_slug, slug, variant)
end

#metricsObject



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/absurdity/experiment.rb', line 90

def metrics
  return @metrics unless @metrics.nil?
  @metrics = []
  metrics_list.each do |metric_slug|
    if variants?
      variants_list.each { |variant| @metrics << metric(metric_slug, variant) }
    else
      @metrics << metric(metric_slug)
    end
  end
  @metrics
end

#metrics_listObject



116
117
118
# File 'lib/absurdity/experiment.rb', line 116

def metrics_list
  attributes[:metrics_list]
end

#reportObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/absurdity/experiment.rb', line 32

def report
  report = { name: slug, completed: completed, data: {} }
  if variants?
    variants_list.each do |variant|
      report[:data][variant] = {}
      metrics_list.each do |metric_slug|
        report[:data][variant][metric_slug] = metric(metric_slug, variant).count
      end
    end
  else
    metrics_list.each do |metric_slug|
      report[:data][metric_slug] = metric(metric_slug).count
    end
  end
  report
end

#saveObject



49
50
51
# File 'lib/absurdity/experiment.rb', line 49

def save
  Datastore.save_experiment(self)
end

#track!(metric_slug, identity_id = nil) ⇒ Object



53
54
55
56
57
# File 'lib/absurdity/experiment.rb', line 53

def track!(metric_slug, identity_id=nil)
  raise Absurdity::MissingIdentityIDError if variants? && identity_id.nil?
  variant = variants? ? variant_for(identity_id) : nil
  metric(metric_slug, variant).track! unless completed
end

#variant_for(identity_id) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/absurdity/experiment.rb', line 103

def variant_for(identity_id)
  if completed
    variant = Variant.new(completed, slug, identity_id)
  else
    variant = Variant.find(identity_id, slug)
    if variant.nil?
      variant = Variant.new(random_variant, slug, identity_id)
      variant.save
    end
  end
  variant.slug
end

#variants?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/absurdity/experiment.rb', line 124

def variants?
  variants_list
end

#variants_listObject



120
121
122
# File 'lib/absurdity/experiment.rb', line 120

def variants_list
  attributes[:variants_list]
end