batching 0.2.0

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: fc2a55db2140a48860bdd5ecb8d01f914cc2e2ee7f6e80552398f2f417e74017
4
+ data.tar.gz: 8d87c88fd3cef9ab7be40d2260c7eaa6c91220ee41ac43c156e38e0fe8ca6164
5
+ SHA512:
6
+ metadata.gz: 2fdcb1e9646fb2cb0a418237a45d27414e98452ea61d684655864e1081ee04c0ea16a0b830bb0c8b1412903e18d437bb9d6714ff83bd69899bb273a5efa0a538
7
+ data.tar.gz: 2a2cc53aafd8da857fb394f7cd93be0d7e8d4e4877a0f2e4dbe8ac7b1068b6ca63d2e2dfce4cd89a487791f373e8e8e9e851ad1a159e59fbcf68172811831a1d
data/.gitignore ADDED
@@ -0,0 +1,59 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ # Ignore Byebug command history file.
17
+ .byebug_history
18
+
19
+ ## Specific to RubyMotion:
20
+ .dat*
21
+ .repl_history
22
+ build/
23
+ *.bridgesupport
24
+ build-iPhoneOS/
25
+ build-iPhoneSimulator/
26
+
27
+ ## Specific to RubyMotion (use of CocoaPods):
28
+ #
29
+ # We recommend against adding the Pods directory to your .gitignore. However
30
+ # you should judge for yourself, the pros and cons are mentioned at:
31
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
32
+ #
33
+ # vendor/Pods/
34
+
35
+ ## Documentation cache and generated files:
36
+ /.yardoc/
37
+ /_yardoc/
38
+ /doc/
39
+ /rdoc/
40
+
41
+ ## Environment normalization:
42
+ /.bundle/
43
+ /vendor/bundle
44
+ /lib/bundler/man/
45
+
46
+ # for a library or gem, you might want to ignore these files since the code is
47
+ # intended to run in multiple environments; otherwise, check them in:
48
+ # Gemfile.lock
49
+ # .ruby-version
50
+ # .ruby-gemset
51
+
52
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
53
+ .rvmrc
54
+
55
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
56
+ # .rubocop-https?--*
57
+
58
+ # Emacs
59
+ *~
data/.rubocop.yml ADDED
@@ -0,0 +1,11 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ TargetRubyVersion: 2.0
4
+ Exclude:
5
+ - 'spec/**/*_spec.rb'
6
+ Metrics/LineLength:
7
+ Max: 120
8
+ Metrics/ModuleLength:
9
+ Max: 120
10
+ Metrics/MethodLength:
11
+ Max: 20
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ batching
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.2.2
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'http://rubygems.org'
4
+
5
+ group :development do
6
+ gem 'rubocop'
7
+ end
8
+
9
+ group :spec do
10
+ gem 'rspec'
11
+ gem 'simplecov'
12
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,61 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ ast (2.4.2)
5
+ diff-lcs (1.5.0)
6
+ docile (1.4.0)
7
+ json (2.7.0)
8
+ language_server-protocol (3.17.0.3)
9
+ parallel (1.23.0)
10
+ parser (3.2.2.4)
11
+ ast (~> 2.4.1)
12
+ racc
13
+ racc (1.7.3)
14
+ rainbow (3.1.1)
15
+ regexp_parser (2.8.2)
16
+ rexml (3.2.6)
17
+ rspec (3.12.0)
18
+ rspec-core (~> 3.12.0)
19
+ rspec-expectations (~> 3.12.0)
20
+ rspec-mocks (~> 3.12.0)
21
+ rspec-core (3.12.2)
22
+ rspec-support (~> 3.12.0)
23
+ rspec-expectations (3.12.3)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.12.0)
26
+ rspec-mocks (3.12.6)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.12.0)
29
+ rspec-support (3.12.1)
30
+ rubocop (1.58.0)
31
+ json (~> 2.3)
32
+ language_server-protocol (>= 3.17.0)
33
+ parallel (~> 1.10)
34
+ parser (>= 3.2.2.4)
35
+ rainbow (>= 2.2.2, < 4.0)
36
+ regexp_parser (>= 1.8, < 3.0)
37
+ rexml (>= 3.2.5, < 4.0)
38
+ rubocop-ast (>= 1.30.0, < 2.0)
39
+ ruby-progressbar (~> 1.7)
40
+ unicode-display_width (>= 2.4.0, < 3.0)
41
+ rubocop-ast (1.30.0)
42
+ parser (>= 3.2.1.0)
43
+ ruby-progressbar (1.13.0)
44
+ simplecov (0.22.0)
45
+ docile (~> 1.1)
46
+ simplecov-html (~> 0.11)
47
+ simplecov_json_formatter (~> 0.1)
48
+ simplecov-html (0.12.3)
49
+ simplecov_json_formatter (0.1.4)
50
+ unicode-display_width (2.5.0)
51
+
52
+ PLATFORMS
53
+ x86_64-darwin-23
54
+
55
+ DEPENDENCIES
56
+ rspec
57
+ rubocop
58
+ simplecov
59
+
60
+ BUNDLED WITH
61
+ 2.4.10
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 dougyouch
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # batching
2
+ Adds in_batches to enumerable objects
3
+
4
+ **requirement**: enumerable objects must have the each\_with\_index method
5
+
6
+ Example:
7
+
8
+ ```
9
+ ids = get_all_ids()
10
+
11
+ # adds the in_batches method to this object
12
+ ids.extend(Batching)
13
+
14
+ # use the in_batches method to fetch 100 ids at a time
15
+ ids.in_batches(batch_size: 100) do |batch|
16
+ response = bulk_lookup(batch)
17
+
18
+ JSON.parse(response.body).each do |result|
19
+ # now process each result
20
+ end
21
+ end
22
+ ```
data/batching.gemspec ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'batching'
5
+ s.version = '0.2.0'
6
+ s.licenses = ['MIT']
7
+ s.summary = 'Batch processing'
8
+ s.description = 'Adds an in_batches to Enumerable'
9
+ s.authors = ['Doug Youch']
10
+ s.email = 'dougyouch@gmail.com'
11
+ s.homepage = 'https://github.com/dougyouch/batching'
12
+ s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
13
+ s.metadata = { 'rubygems_mfa_required' => 'true' }
14
+
15
+ s.required_ruby_version = '>= 2.0'
16
+ end
data/lib/batching.rb ADDED
@@ -0,0 +1,32 @@
1
+ # Purpose to provide a in_batches interface around each
2
+ module Batching
3
+ # Purpose adds start_index to Array
4
+ class Batch < Array
5
+ attr_accessor :start_index
6
+
7
+ def end_index
8
+ return nil if size.zero?
9
+
10
+ start_index + size - 1
11
+ end
12
+ end
13
+
14
+ def in_batches(batch_size: 1_000)
15
+ batch = Batch.new
16
+
17
+ process_batch_proc = proc do
18
+ yield batch
19
+
20
+ batch = Batch.new
21
+ end
22
+
23
+ each_with_index do |item, index|
24
+ batch.start_index ||= index
25
+ batch << item
26
+
27
+ process_batch_proc.call if batch.size >= batch_size
28
+ end
29
+
30
+ process_batch_proc.call unless batch.empty?
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: batching
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Doug Youch
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-12-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Adds an in_batches to Enumerable
14
+ email: dougyouch@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - ".gitignore"
20
+ - ".rubocop.yml"
21
+ - ".ruby-gemset"
22
+ - ".ruby-version"
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE
26
+ - README.md
27
+ - batching.gemspec
28
+ - lib/batching.rb
29
+ homepage: https://github.com/dougyouch/batching
30
+ licenses:
31
+ - MIT
32
+ metadata:
33
+ rubygems_mfa_required: 'true'
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '2.0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubygems_version: 3.4.10
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: Batch processing
53
+ test_files: []