Class: Vanity::Adapters::MockAdapter

Inherits:
AbstractAdapter show all
Defined in:
lib/vanity/adapters/mock_adapter.rb

Overview

Mock adapter. You can use this when running in test environment, staging, wherever you don't care for collecting metrics. Doesn't store anything.

Since:

Instance Method Summary (collapse)

Methods inherited from AbstractAdapter

#ab_assigned, #ab_seen

Constructor Details

- (MockAdapter) initialize(options)

Returns a new instance of MockAdapter

Since:

  • 1.4.0



17
18
19
20
# File 'lib/vanity/adapters/mock_adapter.rb', line 17

def initialize(options)
  @metrics = @@metrics ||= {}
  @experiments = @@experiments ||= {}
end

Instance Method Details

- (Object) ab_add_conversion(experiment, alternative, identity, count = 1, implicit = false)

Since:

  • 1.4.0



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/vanity/adapters/mock_adapter.rb', line 130

def ab_add_conversion(experiment, alternative, identity, count = 1, implicit = false)
  @experiments[experiment] ||= {}
  @experiments[experiment][:alternatives] ||= {}
  alt = @experiments[experiment][:alternatives][alternative] ||= {}
  alt[:participants] ||= Set.new
  alt[:converted] ||= Set.new
  alt[:conversions] ||= 0
  if implicit
    alt[:participants] << identity
  else
    participating = alt[:participants].include?(identity)
  end
  alt[:converted] << identity if implicit || participating
  alt[:conversions] += count
end

- (Object) ab_add_participant(experiment, alternative, identity)

Since:

  • 1.4.0



122
123
124
125
126
127
128
# File 'lib/vanity/adapters/mock_adapter.rb', line 122

def ab_add_participant(experiment, alternative, identity)
  @experiments[experiment] ||= {}
  @experiments[experiment][:alternatives] ||= {}
  alt = @experiments[experiment][:alternatives][alternative] ||= {}
  alt[:participants] ||= Set.new
  alt[:participants] << identity
end

- (Object) ab_counts(experiment, alternative)

Since:

  • 1.4.0



99
100
101
102
103
104
105
106
# File 'lib/vanity/adapters/mock_adapter.rb', line 99

def ab_counts(experiment, alternative)
  @experiments[experiment] ||= {}
  @experiments[experiment][:alternatives] ||= {}
  alt = @experiments[experiment][:alternatives][alternative] ||= {}
  { :participants => alt[:participants] ? alt[:participants].size : 0,
    :converted    => alt[:converted] ? alt[:converted].size : 0,
    :conversions  => alt[:conversions] || 0 }
end

- (Object) ab_get_outcome(experiment)

Since:

  • 1.4.0



146
147
148
149
# File 'lib/vanity/adapters/mock_adapter.rb', line 146

def ab_get_outcome(experiment)
  @experiments[experiment] ||= {}
  @experiments[experiment][:outcome]
end

- (Object) ab_not_showing(experiment, identity)

Since:

  • 1.4.0



118
119
120
# File 'lib/vanity/adapters/mock_adapter.rb', line 118

def ab_not_showing(experiment, identity)
  @experiments[experiment][:showing].delete identity if @experiments[experiment] && @experiments[experiment][:showing]
end

- (Object) ab_set_outcome(experiment, alternative = 0)

Since:

  • 1.4.0



151
152
153
154
# File 'lib/vanity/adapters/mock_adapter.rb', line 151

def ab_set_outcome(experiment, alternative = 0)
  @experiments[experiment] ||= {}
  @experiments[experiment][:outcome] = alternative
end

- (Object) ab_show(experiment, identity, alternative)

Since:

  • 1.4.0



108
109
110
111
112
# File 'lib/vanity/adapters/mock_adapter.rb', line 108

def ab_show(experiment, identity, alternative)
  @experiments[experiment] ||= {}
  @experiments[experiment][:showing] ||= {}
  @experiments[experiment][:showing][identity] = alternative
end

- (Object) ab_showing(experiment, identity)

Since:

  • 1.4.0



114
115
116
# File 'lib/vanity/adapters/mock_adapter.rb', line 114

def ab_showing(experiment, identity)
  @experiments[experiment] && @experiments[experiment][:showing] && @experiments[experiment][:showing][identity]
end

- (Boolean) active?

Returns:

  • (Boolean)

Since:

  • 1.4.0



22
23
24
# File 'lib/vanity/adapters/mock_adapter.rb', line 22

def active?
  !!@metrics
end

- (Object) destroy_experiment(experiment)

Since:

  • 1.4.0



156
157
158
# File 'lib/vanity/adapters/mock_adapter.rb', line 156

def destroy_experiment(experiment)
  @experiments.delete experiment
end

- (Object) destroy_metric(metric)

Since:

  • 1.4.0



66
67
68
# File 'lib/vanity/adapters/mock_adapter.rb', line 66

def destroy_metric(metric)
  @metrics.delete metric
end

- (Object) disconnect!

Since:

  • 1.4.0



26
27
28
29
# File 'lib/vanity/adapters/mock_adapter.rb', line 26

def disconnect!
  @metrics = nil
  @experiments = nil
end

- (Boolean) experiment_persisted?(experiment)

– Experiments –

Returns:

  • (Boolean)

Since:

  • 1.4.0



73
74
75
# File 'lib/vanity/adapters/mock_adapter.rb', line 73

def experiment_persisted?(experiment)
  !!@experiments[experiment]
end

- (Object) flushdb

Since:

  • 1.4.0



40
41
42
43
# File 'lib/vanity/adapters/mock_adapter.rb', line 40

def flushdb
  @metrics.clear
  @experiments.clear
end

- (Object) get_experiment_completed_at(experiment)

Since:

  • 1.4.0



91
92
93
# File 'lib/vanity/adapters/mock_adapter.rb', line 91

def get_experiment_completed_at(experiment)
  @experiments[experiment] && @experiments[experiment][:completed_at]
end

- (Object) get_experiment_created_at(experiment)

Since:

  • 1.4.0



82
83
84
# File 'lib/vanity/adapters/mock_adapter.rb', line 82

def get_experiment_created_at(experiment)
  @experiments[experiment] && @experiments[experiment][:created_at]
end

- (Object) get_metric_last_update_at(metric)

– Metrics –

Since:

  • 1.4.0



48
49
50
# File 'lib/vanity/adapters/mock_adapter.rb', line 48

def get_metric_last_update_at(metric)
  @metrics[metric] && @metrics[metric][:last_update_at]
end

- (Boolean) is_experiment_completed?(experiment)

Returns:

  • (Boolean)

Since:

  • 1.4.0



95
96
97
# File 'lib/vanity/adapters/mock_adapter.rb', line 95

def is_experiment_completed?(experiment)
  @experiments[experiment] && @experiments[experiment][:completed_at]
end

- (Object) metric_track(metric, timestamp, identity, values)

Since:

  • 1.4.0



52
53
54
55
56
57
58
59
# File 'lib/vanity/adapters/mock_adapter.rb', line 52

def metric_track(metric, timestamp, identity, values)
  @metrics[metric] ||= {}
  current = @metrics[metric][timestamp.to_date] ||= []
  values.each_with_index do |v,i|
    current[i] = (current[i] || 0) + v || 0
  end
  @metrics[metric][:last_update_at] = Time.now
end

- (Object) metric_values(metric, from, to)

Since:

  • 1.4.0



61
62
63
64
# File 'lib/vanity/adapters/mock_adapter.rb', line 61

def metric_values(metric, from, to)
  hash = @metrics[metric] || {}
  (from.to_date..to.to_date).map { |date| hash[date] || [] }
end

- (Object) reconnect!

Since:

  • 1.4.0



31
32
33
34
# File 'lib/vanity/adapters/mock_adapter.rb', line 31

def reconnect!
  @metrics = @@metrics
  @experiments = @@experiments
end

- (Object) set_experiment_completed_at(experiment, time)

Since:

  • 1.4.0



86
87
88
89
# File 'lib/vanity/adapters/mock_adapter.rb', line 86

def set_experiment_completed_at(experiment, time)
  @experiments[experiment] ||= {}
  @experiments[experiment][:completed_at] ||= time
end

- (Object) set_experiment_created_at(experiment, time)

Since:

  • 1.4.0



77
78
79
80
# File 'lib/vanity/adapters/mock_adapter.rb', line 77

def set_experiment_created_at(experiment, time)
  @experiments[experiment] ||= {}
  @experiments[experiment][:created_at] ||= time
end

- (Object) to_s

Since:

  • 1.4.0



36
37
38
# File 'lib/vanity/adapters/mock_adapter.rb', line 36

def to_s
  "mock:/"
end