ndb-rails 1.23.0 → 1.23.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: 5b783c6fcc4eaf7e2a923fa0f2a4364dbbccfb7125fbede8e4ec15313f97a347
4
- data.tar.gz: 48ebd0dd32cd550e2cc666a89dfd291a52d80dd21883cfb785f0294acda12657
3
+ metadata.gz: 0ad5aca739eddaf19c833931a7672bd30d0ed6476d0637ce9cd73f4e722bd20f
4
+ data.tar.gz: 01cdfb078dc4189b475fe9931fffb4821fb7ca7cdcc7f98a82d3c6cef6fa672d
5
5
  SHA512:
6
- metadata.gz: 31ff52136c6848b5b8954b91b3fa4cb46f75fa172a05759129b6783b13b36803e55d481ea426a758e388655531ff5933d351da60dcb44625e59be04909752047
7
- data.tar.gz: b01863b74835d4c79cf8c011ed7973c03b629a33dc15eee62fc8d20fadc7547de2f6ed4af07c4ed20ebce8c0739c6bdf6ab11a1225a13828cfcb48ec4a4ae022
6
+ metadata.gz: 2dc449bf0022b4b14a307e7e525f6642af966dc46cdf9d0d55d97b0986545ddb75befb92417140e9580d6ed5e4dd530d5ac6868d33627c6ae74d99547d60dad5
7
+ data.tar.gz: 58a8fc3bde98cde180ea34b7f26b96d0136291df36d1112b8a6e395c88d7f0aaa40a7dc9b416bae2dd8b74b02fcef0b485afe3d8f9865796eaf158643b851d07
@@ -139,14 +139,19 @@ module Ndb
139
139
 
140
140
  # Atomic writes. The daemon matches the where and writes in one step on
141
141
  # every match, so the where needs the id or an indexed field
142
- # (Ndb::UnindexedMany otherwise). ops is the operator Hash of
143
- # Ndb::Client#update ({ "$inc" => { "n" => 1 } }); returns the number of
144
- # modified documents. The braces are optional: update("$inc" => {...}).
142
+ # (Ndb::UnindexedMany otherwise). Returns the number of modified
143
+ # documents. It takes attributes, as Active Record (update(status: "x")
144
+ # is a $set, cast by the attribute types), the operators of
145
+ # Ndb::Client#update ("$inc" => { "n" => 1 }), or both: the attributes
146
+ # join the $set. Unlike Active Record, it runs no validation and no
147
+ # callback (as update_all).
145
148
  def update(ops = nil, expire_in: nil, **ops_kw)
146
- ops = (ops || {}).merge(ops_kw)
147
- plain = ops.keys.reject { |key| key.to_s.start_with?("$") }
149
+ ops = (ops || {}).merge(ops_kw).transform_keys(&:to_s)
150
+ attrs, ops = ops.partition { |key, _| !key.start_with?("$") }.map(&:to_h)
148
151
 
149
- raise ArgumentError, "update takes operators such as \"$set\" => { status: ... }, got #{ops.keys.inspect}" if ops.empty? || plain.any?
152
+ raise ArgumentError, "update takes attributes (status: \"x\") or operators (\"$inc\" => { n: 1 })" if attrs.empty? && ops.empty?
153
+
154
+ ops = with_attributes(ops, attrs) unless attrs.empty?
150
155
 
151
156
  klass.ndb_update(Conditions.combine(values[:where]), with_updated_at(ops), many: true, expire_in: expire_in).fetch("modified")
152
157
  end
@@ -384,6 +389,18 @@ module Ndb
384
389
  values[:keep_encrypted] ? Rails.client.keep_encrypted(&) : yield
385
390
  end
386
391
 
392
+ # ops with attrs, cast by their types, in its $set. A field in both is
393
+ # refused: it is not clear which value wins.
394
+ def with_attributes(ops, attrs)
395
+ set = (ops["$set"] || {}).transform_keys(&:to_s)
396
+ doc = klass.send(:upsert_document, attrs)
397
+ both = set.keys & doc.keys
398
+
399
+ raise ArgumentError, "update sets #{both.join(", ")} twice: as an attribute and in $set" if both.any?
400
+
401
+ ops.merge("$set" => set.merge(doc))
402
+ end
403
+
387
404
  # The operators with $set of updated_at, unless they write it already.
388
405
  def with_updated_at(ops)
389
406
  ops = ops.transform_keys(&:to_s)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Ndb
4
4
  module Rails
5
- VERSION = "1.23.0"
5
+ VERSION = "1.23.3"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ndb-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.23.0
4
+ version: 1.23.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - thadeu
@@ -43,7 +43,7 @@ dependencies:
43
43
  requirements:
44
44
  - - ">="
45
45
  - !ruby/object:Gem::Version
46
- version: 1.23.0
46
+ version: 1.23.3
47
47
  - - "<"
48
48
  - !ruby/object:Gem::Version
49
49
  version: '2'
@@ -53,7 +53,7 @@ dependencies:
53
53
  requirements:
54
54
  - - ">="
55
55
  - !ruby/object:Gem::Version
56
- version: 1.23.0
56
+ version: 1.23.3
57
57
  - - "<"
58
58
  - !ruby/object:Gem::Version
59
59
  version: '2'