easy_ab 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a1eeea1cbf4338159b42eb100e86cf1bae045b7a
4
- data.tar.gz: dbdb41940f2da5113a705c30bc290934219666b7
3
+ metadata.gz: 6a56efe571167899ce6a67e013c3815c3ac9bd8b
4
+ data.tar.gz: 8a3b0357ce34f6e420e7aa10276afc3a09eb1266
5
5
  SHA512:
6
- metadata.gz: 4b2465ffa608c615f52c9f2a3c153323848d2a5d5c8871a906e33492ba927da27a6890b6c1baf524f72e02045907d67906f2619d36e78559e3720af7900f939d
7
- data.tar.gz: 00525589553175a0feac4fc7cf4f2b1c7936885792009f08e82393f988638723c9d593b31db63f9ef1ed27bdaecd3ed72266928beddd51ecc7275f280bd74c6a
6
+ metadata.gz: 1315a89fd9ad3cb5e1fede3ab862c6608222cbe5faba02b2604a8eff3cd3b66c875a90d95ca4a89db687f56a3fb408dedbed7ee8b47c66f5915901171b02536b
7
+ data.tar.gz: 5e40c5b8f23e94a782e2a9b9dcb9f66711384be59b734bff18281e5adb1eeb25ca85db7dd0465d7eb36942dbb401e02be3c23eebfe18d519b0546afbca8f9ccf
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ # HEAD
2
+ * Supports scope in config to define whether a user can join an experiment or not.
3
+
1
4
  # 0.2.0 (2017-08-15)
2
5
  * **API change**: If all rules failed, `ab_test` returns nil, instead of the first variant.
3
6
 
@@ -1,18 +1,20 @@
1
1
  module EasyAb
2
2
  class Experiment
3
- attr_reader :name, :variants, :weights, :rules, :winner
3
+ attr_reader :name, :variants, :weights, :rules, :winner, :scope
4
4
 
5
5
  def initialize(name, options = {})
6
6
  @name = name.to_s
7
7
  @variants = options[:variants].map(&:to_s)
8
8
  @weights = options[:weights]
9
9
  @rules = options[:rules]
10
+ @scope = options[:scope]
10
11
  @winner = options[:winner].nil? ? nil : options[:winner].to_s
11
12
 
12
13
  raise ArgumentError, 'Please define variants' if @variants.blank?
13
14
  raise ArgumentError, 'Number of variants and weights should be identical' if @weights.present? && @weights.size != @variants.size
14
15
  raise ArgumentError, 'Number of variants and rules should be identical' if @rules.present? && @rules.size != @variants.size
15
16
  raise ArgumentError, 'All rules should be a Proc' if @rules.present? && @rules.any? { |rule| !rule.is_a?(Proc) }
17
+ raise ArgumentError, 'Scope should be a Proc' if @scope.present? && !@scope.is_a?(Proc)
16
18
  raise ArgumentError, 'winner should be one of variants' if @winner && !@variants.include?(@winner)
17
19
  end
18
20
 
@@ -23,14 +25,24 @@ module EasyAb
23
25
  exp
24
26
  end
25
27
 
28
+ # Priority:
29
+ # 1. winner
30
+ # 2. url parameter or assign variant (ex: ab_test(:experiment, variant: 'variant A'))
31
+ # 3. scope
32
+ # 4. rules/weights
26
33
  def assign_variant(user_recognition, options = {})
34
+ # 1. winner
27
35
  return winner if winner
28
36
 
29
37
  grouping = find_grouping_by_user_recognition(user_recognition) || ::EasyAb::Grouping.new(experiment: name, user_id: user_recognition[:id], cookie: user_recognition[:cookie])
30
38
 
39
+ # 2. url parameter or assign variant
31
40
  if options[:variant] && variants.include?(options[:variant].to_s)
32
41
  grouping.variant = options[:variant].to_s
33
42
  else
43
+ # 3. scope
44
+ return nil if options[:scope] && !options[:scope].call
45
+ # 4. rules/weights
34
46
  grouping.variant ||= flexible_variant(options[:contexted_rules])
35
47
  return nil if grouping.variant.nil?
36
48
  end
@@ -15,11 +15,18 @@ module EasyAb
15
15
 
16
16
  experiment = EasyAb::Experiment.find_by_name!(experiment_name)
17
17
 
18
+ # Obtain context
18
19
  if experiment.rules.present?
19
20
  @rules_with_current_context ||= experiment.rules.map { |rule| Proc.new { instance_exec(&rule)} }
20
21
  options[:contexted_rules] = @rules_with_current_context
21
22
  end
22
23
 
24
+ # Obtain context
25
+ if experiment.scope.present?
26
+ @scope ||= Proc.new { instance_exec(&experiment.scope) }
27
+ options[:scope] = @scope
28
+ end
29
+
23
30
  @variant_cache ||= {}
24
31
  @variant_cache[experiment_name] ||= experiment.assign_variant(user_recognition, options)
25
32
  block_given? ? yield(@variant_cache[experiment_name]) : @variant_cache[experiment_name]
@@ -1,3 +1,3 @@
1
1
  module EasyAb
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_ab
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gary Chu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-15 00:00:00.000000000 Z
11
+ date: 2017-08-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: icarus4.chu@gmail.com