update_cardinals_by 0.1.0 → 0.3.0

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
  SHA256:
3
- metadata.gz: 0f0c9aa2f3667c4925560e59e838b79a294b918f48dda0f7092b01e476ad0c40
4
- data.tar.gz: cdd625ca542d8d09e7d4ac17a03e48862f569d42a1694bfb94751780b4ed37c0
3
+ metadata.gz: e9273872a9585f3f525899cdb65003d32d67cf3657802b572a33d4075d7b32d0
4
+ data.tar.gz: e53d5fcc13195c30497c4e640a616adc49ab6dab0d72a76c2dffc6cfed0edada
5
5
  SHA512:
6
- metadata.gz: 6ff4b4a4137503a08368116b1b0f213bb41fe08a0d3ca06c8ba44694ca3d00d62e0ee54072efb418737b15af9f987022e9427a0a418e498f2816c5692b83e258
7
- data.tar.gz: 382d155ff8ddc7b3aae7bc9936ee6d6186ac1f55e27dc444249fb23fda69c52c7cf0aa6c28f4c5fed2ae762cc29d6dc85b9faee7f4d704b08b537b2c28cbcd4f
6
+ metadata.gz: ecd5355aa0a352beef84e88fd7a68d96a6d32688f2543bd9c56324d63b686e983a583a1e0f5f319a133278262bccd768b47df40ffd4cfb8d30b3f9b0e7396748
7
+ data.tar.gz: b270748217ecd8f60d3374ce9a9afbba14a5862dd1cb4b6f3f93bd96773cc79f04aff1206ff4521a21a03c7380957a68b876334f7778b0f43a03c8295d92db35
data/README.md CHANGED
@@ -37,6 +37,10 @@ end
37
37
 
38
38
  ## Changelog
39
39
 
40
+ 0.3.0 changed activerecord support to 6.1 and 7.0
41
+
42
+ 0.2.0 changed activerecord support to 5.2 and 6.0
43
+
40
44
  0.1.0 changed activerecord support from 4.2 to 5.0 and 5.1
41
45
 
42
46
  ## Developing
@@ -1,3 +1,3 @@
1
1
  module UpdateCardinalsBy
2
- VERSION = "0.1.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -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| [column_for_attribute(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, type_for_attribute(k).cast(v)] }
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 { |attr, v| raw_write_attribute attr, v }
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, 34) }
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, 16))
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, 50)
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, 24))
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, 10)
33
+ expect(account.money).to eq BigDecimal("11.10")
31
34
  end
32
35
 
33
- it 'leaves leaves the instance in a saved state' do
34
- account.update_by!(credits: 20, money: BigDecimal(3, 16))
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, 16)) do |res|
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.1.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: 2019-05-02 00:00:00.000000000 Z
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: '5.0'
19
+ version: '6.1'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '5.2'
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: '5.0'
29
+ version: '6.1'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '5.2'
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.0.2
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.