dino_utils 0.1.2 → 0.1.3

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: 4c4051bd9fc8ebdc4dcfc38cda484f058de45c87
4
- data.tar.gz: 76087d0036a6e068ba939623605481016f9270eb
3
+ metadata.gz: 4fe82553f09d16773a01d4b2541dac1066447df4
4
+ data.tar.gz: 5c40e59e63341f818a9401fb16b5515ea918428e
5
5
  SHA512:
6
- metadata.gz: c0fb37df0c07a8ebb543cdc5bfc1e32cacedcc3c3eb64ff3af09be5f1c10c5c0c810eac4353c995a4b97fc85d0f5de0c1b00fd2008e3f9c42d07424c2fb2fa0b
7
- data.tar.gz: d2932bbe1bd8f7478f2c042c4b4a89c4f43a0150daa7116e081cfe30548d0e0b7003298111c95866ab8c161cddd0d6767281397c892a21389da26f5b77fafd78
6
+ metadata.gz: 976a2baff2e86bfc86cc85f272fd1ade2b69bd688bdbf1b5e595d17b324a2793bdef6524dda3b72aac8882889cadf9cf54aeed9581d8ef88c682959b0245eba3
7
+ data.tar.gz: 3a1ffceb478d8a018262d8039b197647469c58083137fe8037abcd45c7eaa150bcbf2a4e90ca89a70bcbb9c2494be708acb0e395f15973864f9e173aa6a0a831
@@ -0,0 +1,42 @@
1
+ require 'active_record'
2
+ require 'pg'
3
+
4
+ module Dino
5
+ # Provides a simple way to do an create or update of an ActiveRecord object.
6
+ module Upsert
7
+ extend ActiveSupport::Concern
8
+ # User.upsert
9
+ module ClassMethods
10
+ def upsert(*args)
11
+ Dino::Upsert.upsert(self, *args)
12
+ end
13
+ end
14
+
15
+ # klass: The ActiveRecord class we are upserting against.
16
+ # conditions: what should match the upsert
17
+ # options: what we are updating/inserting
18
+ # If provided, the block gets the object before it's saved, in case
19
+ # there's special init options necessary for it.
20
+ # rubocop:disable MethodLength
21
+ def self.upsert(klass, conditions, options = {}, &block)
22
+ retry_count = 0
23
+ begin
24
+ klass.transaction(requires_new: true) do
25
+ klass.where(conditions).first_or_initialize(&block).tap { |t| t.update!(options) }
26
+ end
27
+ rescue PG::UniqueViolation, ActiveRecord::RecordNotUnique
28
+ # If there's a unique violation, retry this. But only a certain amount
29
+ # of times or we'll get into an infinite loop if something's messed up.
30
+ # (like an incorrect unique index or something)
31
+ if retry_count < 10
32
+ retry_count += 1
33
+ retry
34
+ else
35
+ raise
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ ActiveRecord::Base.send(:include, Dino::Upsert)
@@ -1,3 +1,3 @@
1
1
  module DinoUtils
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/dino_utils.rb CHANGED
@@ -1,41 +1,2 @@
1
1
  require "dino_utils/version"
2
- require 'active_record'
3
-
4
- module Dino
5
- # Provides a simple way to do an create or update of an ActiveRecord object.
6
- module Upsert
7
- extend ActiveSupport::Concern
8
- # User.upsert
9
- module ClassMethods
10
- def upsert(*args)
11
- Dino::Upsert.upsert(self, *args)
12
- end
13
- end
14
-
15
- # klass: The ActiveRecord class we are upserting against.
16
- # conditions: what should match the upsert
17
- # options: what we are updating/inserting
18
- # If provided, the block gets the object before it's saved, in case
19
- # there's special init options necessary for it.
20
- # rubocop:disable MethodLength
21
- def self.upsert(klass, conditions, options = {}, &block)
22
- retry_count = 0
23
- begin
24
- klass.transaction(requires_new: true) do
25
- klass.where(conditions).first_or_initialize(&block).tap { |t| t.update!(options) }
26
- end
27
- rescue PG::UniqueViolation, ActiveRecord::RecordNotUnique
28
- # If there's a unique violation, retry this. But only a certain amount
29
- # of times or we'll get into an infinite loop if something's messed up.
30
- # (like an incorrect unique index or something)
31
- if retry_count < 10
32
- retry_count += 1 && retry
33
- else
34
- raise
35
- end
36
- end
37
- end
38
- end
39
- end
40
-
41
- ActiveRecord::Base.send(:include, Dino::Upsert)
2
+ require_relative 'dino/upsert'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dino_utils
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
  - Joe Van Dyk
@@ -86,6 +86,7 @@ files:
86
86
  - bin/rake
87
87
  - bin/setup
88
88
  - dino_utils.gemspec
89
+ - lib/dino/upsert.rb
89
90
  - lib/dino_utils.rb
90
91
  - lib/dino_utils/version.rb
91
92
  homepage: https://github.com/tanga/dino_utils
@@ -113,4 +114,3 @@ signing_key:
113
114
  specification_version: 4
114
115
  summary: Dino stuff
115
116
  test_files: []
116
- has_rdoc: