vignette 0.0.15 → 0.0.16

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: 66e0ea38c02b8c4f418c7c33dba373493ffc404b
4
- data.tar.gz: a337a2691497d9f323ab0827a1082025088950bd
3
+ metadata.gz: 9e6cdf3a27251255b9bed7d82c76816ce95edc8c
4
+ data.tar.gz: 6a43fd6f66033556886fc788b21ef56c3b97f66f
5
5
  SHA512:
6
- metadata.gz: 152ca489cb8aa27590be5ebb73851f0480ec0a7d64071a342a846aa814db6e036406f604458e28f472ba599559083ae03dbe6910d82758826b242347436873f1
7
- data.tar.gz: a7803d620dffa98ee0a0cad175dd61e087830a5f9a44454ed2336fa8d6eabc4f4693329a05b73b16d72c4b49fede8fbecfa8509c9ecdd0a12374e5610f62790b
6
+ metadata.gz: 55df73bf300cdc624ebc4728c5fe5fa8213bb882322fe6749059e8bdd31dff343b40aa975f33655cd31b2644ce6ee1c8932a3048c6a656f0a323debd5b15861e
7
+ data.tar.gz: be530c3ba036ac2d02b0f855cb0424c14b6d184fab3ae374dd5bd1013b85d85a399b003f1744f06d4246eb7be0d613b3c7f86e9318b12ff775c0dc5086a029e9
data/README.md CHANGED
@@ -92,6 +92,24 @@ This will lead to `Vignette.tests` to include: `{ "cat_names" => "Chairman Meow"
92
92
 
93
93
  Without a name, Vignettes will try to name themselves based on the name of the falling calling them, e.g. `(app/models/user.rb:25)` or `(app/views/users/new.html.haml:22)`
94
94
 
95
+ ## Force Choice
96
+
97
+ When using in Ruby on Rails, you may set the `force_choice_param` flag, which will allow Vignette to choose a specific split based on a url param. E.g. in an initializer:
98
+
99
+ ```ruby
100
+ Vignette.force_choice_param = :v
101
+ ```
102
+
103
+ and in a controller (e.g. `home_controller.rb`)
104
+
105
+ ```ruby
106
+ def home
107
+ @salutation = ['hello', 'goodbye'].vignette
108
+ end
109
+
110
+ ```
111
+ Then, visit: `http://localhost:3000/?v=goodbye`. This will automatically choose the 'goodbye' split. Note: this will only choose a given split if that is an option in the Vignette array itself (e.g. `v=see+you` would be ignored).
112
+
95
113
  ## Caveats
96
114
 
97
115
  Tests are stored in your store object based on the original input. If you change the input of the test (regardless of the name), a new test will be created. Thus [1,2,3].vignette(:numbers) and [2,3,4].vignette(:numbers) will always be considered unique tests. New tests will overwrite old tests with the same name in `Vignette.tests`. This is by design so that you can update the test, have new results, but keep the same test name in your analytics system.
@@ -1,11 +1,5 @@
1
1
  module ObjectExtensions
2
2
 
3
- module Merge
4
- def merge(store, key, hash)
5
-
6
- end
7
- end
8
-
9
3
  # Extensions to the Array object
10
4
  module ArrayExtensions
11
5
 
@@ -17,9 +11,6 @@ module ObjectExtensions
17
11
  def vignette(name=nil, expect_consistent_name: true)
18
12
  vignette_crc = self.crc().abs.to_s(16)
19
13
 
20
- key = "vignette_#{vignette_crc}"
21
- test_name = nil
22
-
23
14
  vig = Vignette.vig
24
15
 
25
16
  test_name = if expect_consistent_name && name.present?
@@ -35,9 +26,11 @@ module ObjectExtensions
35
26
 
36
27
  result = if vig.has_key?(vignette_crc)
37
28
  vig[vignette_crc]['v']
29
+ elsif Vignette.force_choice && self.include?(Vignette.force_choice)
30
+ Vignette.force_choice
38
31
  else
39
32
  # Store key into storage if not available
40
- new_value = self[Kernel.rand(length)]
33
+ new_value = self[Vignette::rand(length)]
41
34
 
42
35
  Vignette.set_vig( vig.merge(vignette_crc => { n: test_name, v: new_value, t: Time.now.to_i }) )
43
36
 
@@ -68,7 +61,13 @@ module ObjectExtensions
68
61
  Hash.new
69
62
  end
70
63
 
71
- Vignette.with_repo(repo) do
64
+ force_choice = if Vignette.force_choice_param
65
+ params[Vignette.force_choice_param]
66
+ else
67
+ nil
68
+ end
69
+
70
+ Vignette.with_repo(repo, force_choice) do
72
71
  yield
73
72
  end
74
73
  end
@@ -1,3 +1,3 @@
1
1
  module Vignette
2
- VERSION = "0.0.15"
2
+ VERSION = "0.0.16"
3
3
  end
data/lib/vignette.rb CHANGED
@@ -16,12 +16,14 @@ module Vignette
16
16
  # Module Attributes, please set via `init()`
17
17
  mattr_accessor :logging
18
18
  mattr_accessor :store
19
+ mattr_accessor :force_choice_param
19
20
 
20
21
  # Initialization Code
21
22
 
22
23
  # Defaults
23
24
  Vignette.store = :session
24
25
  Vignette.logging = false
26
+ Vignette.force_choice_param = nil
25
27
 
26
28
  # We're going to include ArrayExtensions
27
29
  ActionController::Base.send(:include, ObjectExtensions::ActionControllerExtensions)
@@ -37,19 +39,21 @@ module Vignette
37
39
  end
38
40
 
39
41
  # Sets the current repo to be used to get and store tests for this thread
40
- def self.set_repo(repo)
42
+ def self.set_repo(repo, force_choice=nil)
41
43
  Thread.current[:vignette_repo] = repo
44
+ Thread.current[:vignette_force_choice] = force_choice
42
45
  end
43
46
 
44
47
  # Clears the current repo on this thread
45
48
  def self.clear_repo
46
- set_repo(nil)
49
+ set_repo(nil, nil)
47
50
  end
48
51
 
49
52
  # Performs block with repo set to `repo` for this thread
50
- def self.with_repo(repo)
53
+ # Force choice will be automatically selected if given
54
+ def self.with_repo(repo, force_choice=nil)
51
55
  begin
52
- Vignette.set_repo(repo)
56
+ Vignette.set_repo(repo, force_choice)
53
57
 
54
58
  yield
55
59
  ensure
@@ -69,6 +73,13 @@ module Vignette
69
73
  Thread.current[:vignette_repo]
70
74
  end
71
75
 
76
+ # Get the force_choice for this thread
77
+ def self.force_choice
78
+ raise Errors::ConfigError.new("Repo not active, please call Vignette.set_repo before using Vignette (or use around_filter in Rails)") if !active?
79
+
80
+ Thread.current[:vignette_force_choice]
81
+ end
82
+
72
83
  # From the repo (default whatever is set for the thread), grab Vignettes' repo and unpack
73
84
  def self.vig(repo=nil)
74
85
  repo ||= Vignette.repo # allow using existing
@@ -95,11 +106,14 @@ module Vignette
95
106
  private
96
107
 
97
108
  def self.strip_path(filename)
98
- if defined?(Rails) && Rails
109
+ if defined?(Rails) && Rails && Rails.respond_to?(:root)
99
110
  filename.gsub(Regexp.new("#{Rails.root}[/]?"), '')
100
111
  else
101
112
  filename.split('/')[-1]
102
113
  end
103
114
  end
104
115
 
116
+ def self.rand(length)
117
+ Kernel.rand(length)
118
+ end
105
119
  end
@@ -16,6 +16,14 @@ describe ObjectExtensions do
16
16
 
17
17
  include ObjectExtensions::ActionControllerExtensions
18
18
 
19
+ def set_params(p)
20
+ @params = p
21
+ end
22
+
23
+ def params
24
+ @params || {}
25
+ end
26
+
19
27
  def session
20
28
  { session_id: "whatever" }
21
29
  end
@@ -44,5 +52,28 @@ describe ObjectExtensions do
44
52
  expect(Vignette.tests).to eq(number: 3)
45
53
  end
46
54
  end
55
+
56
+ context "with force choice" do
57
+ around(:each) do |example|
58
+ Vignette.force_choice_param = :v
59
+ example.run
60
+ Vignette.force_choice_param = nil
61
+ end
62
+
63
+ it "should correctly run forced choice" do
64
+ expect(Kernel).to receive(:rand).never
65
+ expect(Vignette.active?).to be(false)
66
+
67
+ tc = TestController.new
68
+ tc.set_params({v: 'dog'})
69
+
70
+ tc.run do
71
+ expect(Vignette.repo).to eq(tc.session)
72
+ expect(Vignette.active?).to be(true)
73
+ expect(%w{cat dog parrot turtle horse}.vignette).to eq('dog')
74
+ expect(Vignette.tests).to eq({})
75
+ end
76
+ end
77
+ end
47
78
  end
48
79
  end
@@ -177,4 +177,28 @@ describe Vignette do
177
177
 
178
178
  end
179
179
 
180
+ context 'for force choice' do
181
+ let!(:session) { Hash.new }
182
+ let(:array) { %w{a b c d e f g h i j k l m n o p q r s t u v w x y z} }
183
+
184
+ it 'should store tests in session' do
185
+ expect(Kernel).to receive(:rand).never
186
+
187
+ Vignette.set_repo(session, 'p')
188
+
189
+ expect(array.vignette).to eq('p')
190
+ expect(Vignette.tests).to eq({})
191
+ end
192
+
193
+ it 'should store tests in session' do
194
+ expect(Kernel).to receive(:rand).and_return(4)
195
+
196
+ Vignette.set_repo(session, 'zz')
197
+
198
+ expect(array.vignette).to eq('e')
199
+ expect(Vignette.tests).to_not eq({})
200
+ end
201
+
202
+ end
203
+
180
204
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vignette
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.15
4
+ version: 0.0.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Geoff Hayes
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-18 00:00:00.000000000 Z
11
+ date: 2015-05-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport