thread_safe_uniqueness_record 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2bf4c93069a1136251fd084b86325d84e8cfd71ebadea46f47e81e04377fd8a8
4
- data.tar.gz: 5d09a1c56a10682ad1603cd64ad2fcd3adb418de0526f26017840965481a6240
3
+ metadata.gz: d3236dcaebb66767b924337d47251007b0bf96e4c3c63d94d16c1e3793ad680c
4
+ data.tar.gz: cda4475e15d9a6a567f7bb403506d4f32ea81514f42d93b80ce69b3bf011fc87
5
5
  SHA512:
6
- metadata.gz: '08ae52edaf3d1e0d82cb65221be89a1f6b9cd473446fa4bb6f8dbd68f4696f80f442512a0295deeb067f2f99f8ca93597d3d7496d63c18c9dfabc2a52409ddd1'
7
- data.tar.gz: 24c5b6b4fb5e1556e46e385819e47d08352d18c575f09cf36e421b7af159b091dd058d287c581d9522fd13c5b3fde9d8716022bacd785d4318e20bb7f7393da3
6
+ metadata.gz: 0572247260a8393990740911b51e41193d376569f387143968afe5a1ede4faf09a171efa33e338a90e3ff723247ee347032d88f8f5a461d295e7da689c00ac8d
7
+ data.tar.gz: 603678264c7047d99efe39939956605f714aaf5139b5557886aedbd152e0d945a8654d71b18b618f6f8f4c912d61efdf065234c048e1d77a2205f5978359fe8c
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Aleksey Strizhak
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.
@@ -0,0 +1,31 @@
1
+ require 'thread_safe_uniqueness_record/version'
2
+ require 'thread_safe_uniqueness_record/find_or_create_by'
3
+ require 'active_record'
4
+ require 'concurrent'
5
+
6
+ module ThreadSafeUniquenessRecord
7
+ class Error < StandardError; end
8
+
9
+ class UniquenessValidationError
10
+ def self.===(exception)
11
+ exception.is_a?(ActiveRecord::RecordInvalid) &&
12
+ /Validation failed:.+\btaken\b/.match?(exception.message)
13
+ end
14
+ end
15
+
16
+ MAX_TRIES = 3
17
+ ERRORS = [
18
+ UniquenessValidationError,
19
+ ActiveRecord::RecordNotUnique
20
+ ].freeze
21
+
22
+ def self.with_retry(&block)
23
+ attempts ||= Concurrent::ThreadLocalVar.new(0)
24
+ block.call
25
+ rescue *ERRORS => e
26
+ attempts.value += 1
27
+ raise e if attempts.value >= MAX_TRIES
28
+
29
+ retry
30
+ end
31
+ end
@@ -0,0 +1,11 @@
1
+ module ThreadSafeUniquenessRecord
2
+ class FindOrCreateBy
3
+ def self.call(model_klass:, attributes:)
4
+ ThreadSafeUniquenessRecord.with_retry do
5
+ ActiveRecord::Base.transaction(requires_new: true) do
6
+ model_klass.find_or_create_by!(attributes)
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module ThreadSafeUniquenessRecord
2
+ VERSION = '0.1.3'.freeze
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thread_safe_uniqueness_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aleksey Strizhak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-13 00:00:00.000000000 Z
11
+ date: 2021-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -86,7 +86,11 @@ email:
86
86
  executables: []
87
87
  extensions: []
88
88
  extra_rdoc_files: []
89
- files: []
89
+ files:
90
+ - LICENSE.txt
91
+ - lib/thread_safe_uniqueness_record.rb
92
+ - lib/thread_safe_uniqueness_record/find_or_create_by.rb
93
+ - lib/thread_safe_uniqueness_record/version.rb
90
94
  homepage: https://github.com/Mifrill/thread_safe_uniqueness_record.git
91
95
  licenses:
92
96
  - MIT
@@ -101,7 +105,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
101
105
  requirements:
102
106
  - - ">="
103
107
  - !ruby/object:Gem::Version
104
- version: 2.3.0
108
+ version: 2.6.0
105
109
  required_rubygems_version: !ruby/object:Gem::Requirement
106
110
  requirements:
107
111
  - - ">="