chanko_ab 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b97fbd1349d68e6c318736e7fd0ad0ec0f86cfe8a731cb875ade20d6c785c72b
4
+ data.tar.gz: 92029f92c969fb816749987326dbb7be33d2e8813434193ebb3b611712eef6ce
5
+ SHA512:
6
+ metadata.gz: 82b5a4eb5b35d016c6e0af2a0c13fff7f10a7c4dad52fb1379179e24113604b2c5d17d30701ab5b9f55b3008ed6f61551d82283c034d3b7a3e59f71433766938
7
+ data.tar.gz: a3615454afd6e4262931acf47f2ad08c33cced96f36293f27b8b03cf473e1d4732de30a8ad17df3f7adbc887afa155897109dd0f7a031af11ae45812f5ddcc07
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in chanko_ab.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,177 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ chanko_ab (0.1.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ actioncable (7.0.3.1)
10
+ actionpack (= 7.0.3.1)
11
+ activesupport (= 7.0.3.1)
12
+ nio4r (~> 2.0)
13
+ websocket-driver (>= 0.6.1)
14
+ actionmailbox (7.0.3.1)
15
+ actionpack (= 7.0.3.1)
16
+ activejob (= 7.0.3.1)
17
+ activerecord (= 7.0.3.1)
18
+ activestorage (= 7.0.3.1)
19
+ activesupport (= 7.0.3.1)
20
+ mail (>= 2.7.1)
21
+ net-imap
22
+ net-pop
23
+ net-smtp
24
+ actionmailer (7.0.3.1)
25
+ actionpack (= 7.0.3.1)
26
+ actionview (= 7.0.3.1)
27
+ activejob (= 7.0.3.1)
28
+ activesupport (= 7.0.3.1)
29
+ mail (~> 2.5, >= 2.5.4)
30
+ net-imap
31
+ net-pop
32
+ net-smtp
33
+ rails-dom-testing (~> 2.0)
34
+ actionpack (7.0.3.1)
35
+ actionview (= 7.0.3.1)
36
+ activesupport (= 7.0.3.1)
37
+ rack (~> 2.0, >= 2.2.0)
38
+ rack-test (>= 0.6.3)
39
+ rails-dom-testing (~> 2.0)
40
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
41
+ actiontext (7.0.3.1)
42
+ actionpack (= 7.0.3.1)
43
+ activerecord (= 7.0.3.1)
44
+ activestorage (= 7.0.3.1)
45
+ activesupport (= 7.0.3.1)
46
+ globalid (>= 0.6.0)
47
+ nokogiri (>= 1.8.5)
48
+ actionview (7.0.3.1)
49
+ activesupport (= 7.0.3.1)
50
+ builder (~> 3.1)
51
+ erubi (~> 1.4)
52
+ rails-dom-testing (~> 2.0)
53
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
54
+ activejob (7.0.3.1)
55
+ activesupport (= 7.0.3.1)
56
+ globalid (>= 0.3.6)
57
+ activemodel (7.0.3.1)
58
+ activesupport (= 7.0.3.1)
59
+ activerecord (7.0.3.1)
60
+ activemodel (= 7.0.3.1)
61
+ activesupport (= 7.0.3.1)
62
+ activestorage (7.0.3.1)
63
+ actionpack (= 7.0.3.1)
64
+ activejob (= 7.0.3.1)
65
+ activerecord (= 7.0.3.1)
66
+ activesupport (= 7.0.3.1)
67
+ marcel (~> 1.0)
68
+ mini_mime (>= 1.1.0)
69
+ activesupport (7.0.3.1)
70
+ concurrent-ruby (~> 1.0, >= 1.0.2)
71
+ i18n (>= 1.6, < 2)
72
+ minitest (>= 5.1)
73
+ tzinfo (~> 2.0)
74
+ builder (3.2.4)
75
+ chanko (2.2.1)
76
+ rails (>= 4.0.0)
77
+ concurrent-ruby (1.1.10)
78
+ crass (1.0.6)
79
+ diff-lcs (1.5.0)
80
+ digest (3.1.0)
81
+ erubi (1.11.0)
82
+ globalid (1.0.0)
83
+ activesupport (>= 5.0)
84
+ i18n (1.12.0)
85
+ concurrent-ruby (~> 1.0)
86
+ loofah (2.18.0)
87
+ crass (~> 1.0.2)
88
+ nokogiri (>= 1.5.9)
89
+ mail (2.7.1)
90
+ mini_mime (>= 0.1.1)
91
+ marcel (1.0.2)
92
+ method_source (1.0.0)
93
+ mini_mime (1.1.2)
94
+ mini_portile2 (2.8.0)
95
+ minitest (5.16.3)
96
+ net-imap (0.2.3)
97
+ digest
98
+ net-protocol
99
+ strscan
100
+ net-pop (0.1.1)
101
+ digest
102
+ net-protocol
103
+ timeout
104
+ net-protocol (0.1.3)
105
+ timeout
106
+ net-smtp (0.3.1)
107
+ digest
108
+ net-protocol
109
+ timeout
110
+ nio4r (2.5.8)
111
+ nokogiri (1.13.8)
112
+ mini_portile2 (~> 2.8.0)
113
+ racc (~> 1.4)
114
+ racc (1.6.0)
115
+ rack (2.2.4)
116
+ rack-test (2.0.2)
117
+ rack (>= 1.3)
118
+ rails (7.0.3.1)
119
+ actioncable (= 7.0.3.1)
120
+ actionmailbox (= 7.0.3.1)
121
+ actionmailer (= 7.0.3.1)
122
+ actionpack (= 7.0.3.1)
123
+ actiontext (= 7.0.3.1)
124
+ actionview (= 7.0.3.1)
125
+ activejob (= 7.0.3.1)
126
+ activemodel (= 7.0.3.1)
127
+ activerecord (= 7.0.3.1)
128
+ activestorage (= 7.0.3.1)
129
+ activesupport (= 7.0.3.1)
130
+ bundler (>= 1.15.0)
131
+ railties (= 7.0.3.1)
132
+ rails-dom-testing (2.0.3)
133
+ activesupport (>= 4.2.0)
134
+ nokogiri (>= 1.6)
135
+ rails-html-sanitizer (1.4.3)
136
+ loofah (~> 2.3)
137
+ railties (7.0.3.1)
138
+ actionpack (= 7.0.3.1)
139
+ activesupport (= 7.0.3.1)
140
+ method_source
141
+ rake (>= 12.2)
142
+ thor (~> 1.0)
143
+ zeitwerk (~> 2.5)
144
+ rake (13.0.6)
145
+ rspec (3.11.0)
146
+ rspec-core (~> 3.11.0)
147
+ rspec-expectations (~> 3.11.0)
148
+ rspec-mocks (~> 3.11.0)
149
+ rspec-core (3.11.0)
150
+ rspec-support (~> 3.11.0)
151
+ rspec-expectations (3.11.0)
152
+ diff-lcs (>= 1.2.0, < 2.0)
153
+ rspec-support (~> 3.11.0)
154
+ rspec-mocks (3.11.1)
155
+ diff-lcs (>= 1.2.0, < 2.0)
156
+ rspec-support (~> 3.11.0)
157
+ rspec-support (3.11.0)
158
+ strscan (3.0.4)
159
+ thor (1.2.1)
160
+ timeout (0.3.0)
161
+ tzinfo (2.0.5)
162
+ concurrent-ruby (~> 1.0)
163
+ websocket-driver (0.7.5)
164
+ websocket-extensions (>= 0.1.0)
165
+ websocket-extensions (0.1.5)
166
+ zeitwerk (2.6.0)
167
+
168
+ PLATFORMS
169
+ ruby
170
+
171
+ DEPENDENCIES
172
+ chanko (>= 2.1.0)
173
+ chanko_ab!
174
+ rspec
175
+
176
+ BUNDLED WITH
177
+ 1.17.2
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2018 shingo morita
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # ChankoAb
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'chanko_ab'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install chanko_ab
18
+
19
+ ## Usage
20
+ ```ruby
21
+ # Define logging code and identifier
22
+ # e.g.
23
+ ChankoAb.set_logging do |name|
24
+ Rails.logger.debug(name)
25
+ end
26
+
27
+ ChankoAb.set_identifier do
28
+ cookies[:identifier]
29
+ end
30
+ ```
31
+
32
+ ```ruby
33
+ # And then, prepare ab test chanko unit.
34
+ module MySplitTest
35
+ include Chanko::Unit
36
+ include ChankoAb
37
+
38
+ split_test.add(:default, {})
39
+ split_test.add(:pattern1, { partial: 'partial1'} )
40
+
41
+ split_test.log_tesmplate('show' ,'my_split_test.[name]')
42
+
43
+ split_test.define(:new_text, scope: :view) do
44
+ ab.log('show')
45
+
46
+ case ab.name
47
+ when 'default'
48
+ run_default
49
+ else
50
+ render ab.fetch(:partial)
51
+ end
52
+ end
53
+ end
54
+ ```
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/eudoxa/chanko_ab.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "chanko_ab"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/chanko_ab.gemspec ADDED
@@ -0,0 +1,24 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "chanko_ab/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "chanko_ab"
8
+ spec.version = ChankoAb::VERSION
9
+ spec.authors = ["morita shingo"]
10
+ spec.email = ["morita.shingo@gmail.com"]
11
+ spec.summary = %q{ab test extension for chanko}
12
+ spec.description = %q{ab test extension for chanko}
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
15
+ f.match(%r{^(test|spec|features)/})
16
+ end
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+
22
+ spec.add_development_dependency "chanko", ">= 2.1.0"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,3 @@
1
+ module ChankoAb
2
+ VERSION = "0.1.1"
3
+ end
data/lib/chanko_ab.rb ADDED
@@ -0,0 +1,235 @@
1
+ require "chanko_ab/version"
2
+
3
+ module ChankoAb
4
+ def self.set_logging(&block)
5
+ @logging_proc = block
6
+ end
7
+
8
+ def self.set_identifier(&block)
9
+ @identifier_proc = block
10
+ end
11
+
12
+ def self.identifier_proc
13
+ @identifier_proc
14
+ end
15
+
16
+ def self.log(caller_scope, name, options)
17
+ raise NotImplementedError unless @logging_proc
18
+ caller_scope.instance_exec(name, options, &@logging_proc)
19
+ end
20
+
21
+ module Process
22
+ class Base
23
+ def initialize(split_test, caller_scope, request, identifier, using_index)
24
+ @split_test = split_test
25
+ @caller_scope = caller_scope
26
+ @request = request
27
+ @identifier = identifier
28
+ @using_index = using_index || 0
29
+ @data = {}
30
+ end
31
+
32
+ def data(name, &block)
33
+ @data[name] ||= block.call
34
+ end
35
+
36
+ def should_run_default?
37
+ raise NotImplementedError
38
+ end
39
+ private :should_run_default?
40
+
41
+ def pattern_by_overwritten
42
+ overwritten_name = ChankoAb::Test.overwritten_name(@split_test.unit)
43
+ pattern = patterns.detect { |pt| pt[0] == overwritten_name }
44
+ pattern || raise("ChankoAb: specified no defined name '#{overwritten_name}'")
45
+ end
46
+ private :pattern_by_overwritten
47
+
48
+ def name
49
+ item[0]
50
+ end
51
+
52
+ def fetch(key)
53
+ item[1][key]
54
+ end
55
+
56
+ def log_key(template_name)
57
+ template = @split_test.fetch_template(template_name)
58
+ template.gsub('[name]', name)
59
+ end
60
+
61
+ def log(template_name)
62
+ ChankoAb.log(@caller_scope, log_key(template_name), request: @request, identifier: @identifier)
63
+ end
64
+
65
+ def item
66
+ if Rails.env.test? && ChankoAb::Test.overwritten?(@split_test.unit)
67
+ return pattern_by_overwritten
68
+ else
69
+ index = decide_pattern_index
70
+ return nil if index > patterns.size
71
+ return patterns[index]
72
+ end
73
+ end
74
+
75
+ def patterns
76
+ @split_test.patterns
77
+ end
78
+ private :patterns
79
+
80
+ def decide_pattern_index
81
+ #e.g
82
+ raise NotImplementedError
83
+ end
84
+ private :decide_pattern_index
85
+ end
86
+
87
+ class NumberIdentifier < Base
88
+ DEFAULT_JUST_PATTERN_SIZES = [1, 2, 5, 10]
89
+
90
+ def just_pattern_sizes
91
+ DEFAULT_JUST_PATTERN_SIZES
92
+ end
93
+
94
+ def should_run_default?
95
+ return true if !item
96
+ @identifier.blank?
97
+ end
98
+
99
+ def ab_index(identifier, size, using_index)
100
+ unless Rails.env.production?
101
+ raise "argument should be a divisor of 10 but #{size}" if !just_pattern_sizes.member?(size)
102
+ raise "identifier is blank" if identifier.blank?
103
+ raise "using_index error" if identifier.size < (using_index + 1)
104
+ end
105
+ using_number = identifier.slice(-(using_index + 1)).to_i
106
+ using_number % size
107
+ end
108
+
109
+ def decide_pattern_index
110
+ return 0 if patterns.size == 0
111
+ raise 'too large size' if patterns.size >= just_pattern_sizes.max
112
+ size = just_pattern_sizes.select { |size| size >= patterns.size }.min
113
+ ab_index(@identifier, size, @using_index)
114
+ end
115
+ end
116
+ end
117
+
118
+ class SplitTest
119
+ def initialize(unit)
120
+ @unit = unit
121
+ @patterns = []
122
+ @log_templates = {}
123
+
124
+ initialize_shared_methods
125
+ end
126
+
127
+ def process(caller_scope, request, identifier, &block)
128
+ process = ChankoAb::Process::NumberIdentifier.new(self, caller_scope, request, identifier, @using_index)
129
+
130
+ processes.push(process)
131
+ begin
132
+ block.call(process)
133
+ ensure
134
+ processes.pop
135
+ end
136
+ end
137
+
138
+ def unit
139
+ @unit
140
+ end
141
+
142
+ def add(name, attributes)
143
+ @patterns << [name, attributes]
144
+ end
145
+
146
+ def log_template(name, template)
147
+ @log_templates[name] = template
148
+ end
149
+
150
+ def patterns
151
+ @patterns
152
+ end
153
+
154
+ def fetch_template(name)
155
+ @log_templates[name]
156
+ end
157
+
158
+ def processes
159
+ @processes ||= []
160
+ end
161
+
162
+ def using_index(index)
163
+ @using_index = index
164
+ end
165
+
166
+ def use_user_via(&user_proc)
167
+ @user_proc = user_proc
168
+ end
169
+
170
+ def user_ab?
171
+ !!@user_proc
172
+ end
173
+
174
+ def define(key, options = {}, &block)
175
+ __split_test__ = self
176
+ unit.class_eval do
177
+ scope(options[:scope] || :view) do
178
+ function(key) do
179
+ __split_test__.process(self, request, identifier) do |process|
180
+ next run_default if process.should_run_default?
181
+ self.instance_eval(&block)
182
+ end
183
+ end
184
+ end
185
+ end
186
+ end
187
+
188
+ def initialize_shared_methods
189
+ __split_test__ = self
190
+ @unit.class_eval do
191
+ shared(:unit_name) do
192
+ @units.last.name
193
+ end
194
+
195
+ shared(:ab) do
196
+ __split_test__.processes.last
197
+ end
198
+
199
+ shared(:identifier) do
200
+ self.instance_eval(&ChankoAb.identifier_proc)
201
+ end
202
+ end
203
+ end
204
+ private :initialize_shared_methods
205
+ end
206
+
207
+ module Test
208
+ def self.overwritten_name(klass)
209
+ @overwritten_names[klass]
210
+ end
211
+
212
+ def self.overwrite(klass, name)
213
+ @overwritten_names ||= {}
214
+ @overwritten_names[klass] = name
215
+ end
216
+
217
+ def self.overwritten?(klass)
218
+ !!@overwritten_names[klass]
219
+ end
220
+
221
+ def self.reset!
222
+ @overwritten_names = {}
223
+ end
224
+ end
225
+
226
+ module ClassMethods
227
+ def split_test
228
+ @split_test ||= SplitTest.new(self)
229
+ end
230
+ end
231
+
232
+ def self.included(klass)
233
+ klass.extend(ClassMethods)
234
+ end
235
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chanko_ab
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - morita shingo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-09-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: chanko
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: ab test extension for chanko
42
+ email:
43
+ - morita.shingo@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - Gemfile
49
+ - Gemfile.lock
50
+ - LICENSE
51
+ - README.md
52
+ - Rakefile
53
+ - bin/console
54
+ - bin/setup
55
+ - chanko_ab.gemspec
56
+ - lib/chanko_ab.rb
57
+ - lib/chanko_ab/version.rb
58
+ homepage:
59
+ licenses: []
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubygems_version: 3.2.32
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: ab test extension for chanko
80
+ test_files: []