update_cardinals_by 0.1.0 → 0.3.0
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 +4 -4
- data/README.md +4 -0
- data/lib/update_cardinals_by/version.rb +1 -1
- data/lib/update_cardinals_by.rb +6 -3
- data/spec/concerns/update_cardinals_by_spec.rb +12 -9
- metadata +11 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e9273872a9585f3f525899cdb65003d32d67cf3657802b572a33d4075d7b32d0
|
4
|
+
data.tar.gz: e53d5fcc13195c30497c4e640a616adc49ab6dab0d72a76c2dffc6cfed0edada
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ecd5355aa0a352beef84e88fd7a68d96a6d32688f2543bd9c56324d63b686e983a583a1e0f5f319a133278262bccd768b47df40ffd4cfb8d30b3f9b0e7396748
|
7
|
+
data.tar.gz: b270748217ecd8f60d3374ce9a9afbba14a5862dd1cb4b6f3f93bd96773cc79f04aff1206ff4521a21a03c7380957a68b876334f7778b0f43a03c8295d92db35
|
data/README.md
CHANGED
data/lib/update_cardinals_by.rb
CHANGED
@@ -11,7 +11,7 @@ module UpdateCardinalsBy
|
|
11
11
|
def update_cardinals_by!(attributes, &block)
|
12
12
|
db_attributes = attributes.map { |k, v| [self.class.connection.quote_column_name(k), v] }
|
13
13
|
updates = db_attributes.map.with_index { |(k, v), i| "#{k} = #{k} + $#{i+1}" }
|
14
|
-
binds = attributes.map { |k, v| [
|
14
|
+
binds = attributes.map { |k, v| self.class.attribute_types[k].serialize(v) }
|
15
15
|
|
16
16
|
transaction do
|
17
17
|
res = self.class.connection.exec_query(
|
@@ -21,12 +21,15 @@ module UpdateCardinalsBy
|
|
21
21
|
"returning #{db_attributes.map(&:first).join(', ')}",
|
22
22
|
'SQL', binds
|
23
23
|
).first
|
24
|
-
.map { |k, v| [k,
|
24
|
+
.map { |k, v| [k, self.class.attribute_types[k].deserialize(v)] }
|
25
25
|
res = HashWithIndifferentAccess[ res ]
|
26
26
|
|
27
27
|
yield res if block_given?
|
28
28
|
|
29
|
-
res.each
|
29
|
+
res.each do |attr, v|
|
30
|
+
write_attribute attr, v
|
31
|
+
clear_attribute_change attr
|
32
|
+
end
|
30
33
|
end
|
31
34
|
end
|
32
35
|
end
|
@@ -15,23 +15,26 @@ class Account < ActiveRecord::Base
|
|
15
15
|
end
|
16
16
|
|
17
17
|
describe UpdateCardinalsBy do
|
18
|
-
let(:account) { Account.create credits: 100, money: BigDecimal(12
|
18
|
+
let(:account) { Account.create credits: 100, money: BigDecimal("12.34") }
|
19
19
|
|
20
20
|
describe 'update_cardinals_by!' do
|
21
21
|
it 'accepts positive change' do
|
22
|
-
account.update_by!(credits: 20, money: BigDecimal(3
|
22
|
+
account.update_by!(credits: 20, money: BigDecimal("3.16"))
|
23
23
|
expect(account.credits).to eq 120
|
24
|
-
expect(account.money).to eq BigDecimal(15
|
24
|
+
expect(account.money).to eq BigDecimal("15.50")
|
25
|
+
account.reload
|
26
|
+
expect(account.credits).to eq 120
|
27
|
+
expect(account.money).to eq BigDecimal("15.50")
|
25
28
|
end
|
26
29
|
|
27
30
|
it 'accepts negative change' do
|
28
|
-
account.update_by!(credits: -20, money: -BigDecimal(1
|
31
|
+
account.update_by!(credits: -20, money: -BigDecimal("1.24"))
|
29
32
|
expect(account.credits).to eq 80
|
30
|
-
expect(account.money).to eq BigDecimal(11
|
33
|
+
expect(account.money).to eq BigDecimal("11.10")
|
31
34
|
end
|
32
35
|
|
33
|
-
it 'leaves
|
34
|
-
account.update_by!(credits: 20, money: BigDecimal(3
|
36
|
+
it 'leaves the instance in a saved state' do
|
37
|
+
account.update_by!(credits: 20, money: BigDecimal("3.16"))
|
35
38
|
expect(account.changed?).to be_falsy
|
36
39
|
end
|
37
40
|
|
@@ -42,10 +45,10 @@ describe UpdateCardinalsBy do
|
|
42
45
|
end
|
43
46
|
|
44
47
|
it 'passes the right types to the block' do
|
45
|
-
account.update_cardinals_by!(credits: 20, money: BigDecimal(3
|
48
|
+
account.update_cardinals_by!(credits: 20, money: BigDecimal("3.16")) do |res|
|
46
49
|
expect(res[:credits]).to be_a Integer
|
47
50
|
expect(res[:money]).to be_a BigDecimal
|
48
51
|
end
|
49
52
|
end
|
50
53
|
end
|
51
|
-
end
|
54
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: update_cardinals_by
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hampei
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-02-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,20 +16,20 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '6.1'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '
|
22
|
+
version: '7.1'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
27
|
- - ">"
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: '
|
29
|
+
version: '6.1'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
32
|
+
version: '7.1'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: pg
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -114,7 +114,7 @@ dependencies:
|
|
114
114
|
- - ">="
|
115
115
|
- !ruby/object:Gem::Version
|
116
116
|
version: '0'
|
117
|
-
description:
|
117
|
+
description:
|
118
118
|
email:
|
119
119
|
- henk.van.der.veen@gmail.com
|
120
120
|
executables: []
|
@@ -132,7 +132,7 @@ homepage: https://github.com/hampei/update_cardinals_by
|
|
132
132
|
licenses:
|
133
133
|
- MIT
|
134
134
|
metadata: {}
|
135
|
-
post_install_message:
|
135
|
+
post_install_message:
|
136
136
|
rdoc_options: []
|
137
137
|
require_paths:
|
138
138
|
- lib
|
@@ -147,8 +147,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
147
147
|
- !ruby/object:Gem::Version
|
148
148
|
version: '0'
|
149
149
|
requirements: []
|
150
|
-
rubygems_version: 3.
|
151
|
-
signing_key:
|
150
|
+
rubygems_version: 3.4.6
|
151
|
+
signing_key:
|
152
152
|
specification_version: 4
|
153
153
|
summary: postgres database extension to increment/decrement values safely and get
|
154
154
|
back result.
|