ndb-rails 0.3.3 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 36faa77fbcb1e44d523252eb2869a286de0e463f39b20c6f387396b87cffae96
4
- data.tar.gz: 9e5071e77a89c1e76193c6a85e929e32d4613dd184c38fbae8ed510bf90899ab
3
+ metadata.gz: d7fe2bcbea6268a30dae29bfa0457dcd8078ae7b485064871a77b6cf970bf173
4
+ data.tar.gz: cfdde7db03a7c51e58fadf9e4216743aafb8dedfc4dfb7666568c8a9d394cefa
5
5
  SHA512:
6
- metadata.gz: 41b000dbd96455396f3534c7feed13ae49a94295a01a3cdaeb411bbd44004c577e8ebdbe33400fb2f47ee8e69af43dd47b57fa071ec0aa8d4b4b421932661732
7
- data.tar.gz: c3a43e41796660a41d260063d16ab43824ec56e8bef9155eb4d89b65f6cf1b4cd57fb4d0d9f9d26fd03e8225a40638eaf68c6252e1be002ef28dc2079035b8ba
6
+ metadata.gz: cdba61392f9801da6d52a1c73bebfd64306b570a20817281cfa0b21b04689401dfe571b115e6cbddf37ae2279b4dd1bc17a461e296b28de74f18537c41669df9
7
+ data.tar.gz: bc12a571537f11c22fcaabb1346c32bc98bcf4403a7cbb789503d1e91e61f4453d37d6365a0f9c39e17936ee3a3ec29f11691df86cc066a2c842e42c7e138a33
@@ -42,9 +42,10 @@ module Ndb
42
42
  def instantiate(row)
43
43
  row = row.dup
44
44
  version = row.delete("_v")
45
+ source = row.delete("_coll")
45
46
  record = new(row.slice(*attribute_names))
46
47
 
47
- record.send(:loaded!, version)
48
+ record.send(:loaded!, version, source)
48
49
 
49
50
  record
50
51
  end
@@ -52,6 +53,10 @@ module Ndb
52
53
 
53
54
  attr_reader :version
54
55
 
56
+ # The collection a record was read from, when its model reads a
57
+ # collection pattern (self.collection_name = "logs-2026-*"); nil otherwise.
58
+ attr_reader :source_collection
59
+
55
60
  # id is an alias of _id.
56
61
  def id
57
62
  _id
@@ -244,9 +249,10 @@ module Ndb
244
249
  self
245
250
  end
246
251
 
247
- def loaded!(version)
252
+ def loaded!(version, source = nil)
248
253
  @persisted = true
249
254
  @version = version
255
+ @source_collection = source if source
250
256
 
251
257
  changes_applied
252
258
  end
@@ -18,9 +18,12 @@ module Ndb
18
18
 
19
19
  attribute :_id, :string
20
20
 
21
+ # The answer of Model.search.
22
+ SearchResult = Struct.new(:records, :next_cursor, :total)
23
+
21
24
  class << self
22
25
  QUERY_METHODS = %i[
23
- where order limit offset page after group find find_by find_by! first count pluck exists?
26
+ where where_raw order limit offset page after group find find_by find_by! first count pluck exists?
24
27
  find_each in_batches sum average minimum maximum to_a each destroy_all delete_all
25
28
  ].freeze
26
29
 
@@ -36,6 +39,19 @@ module Ndb
36
39
  Relation.new(self)
37
40
  end
38
41
 
42
+ # Runs a find with a whole body built elsewhere, such as from a search
43
+ # form: { "where" => {...}, "sort" => {...}, "limit" => 50, "after" =>
44
+ # token, "count" => true }. The body is sent as is (the daemon
45
+ # validates it). Returns the records, the next cursor (nil on the last
46
+ # page) and the total (with "count" => true).
47
+ def search(body)
48
+ raise ArgumentError, "search takes a Hash, got #{body.class}" unless body.is_a?(Hash)
49
+
50
+ page = ndb_find(body.transform_keys(&:to_s))
51
+
52
+ SearchResult.new(page.rows.map { |row| instantiate(row) }, page.next_cursor, page.total)
53
+ end
54
+
39
55
  # Finds the record with these attributes, or builds an unsaved one.
40
56
  def find_or_initialize_by(attrs, &block)
41
57
  find_by(attrs) || new(attrs).tap { |record| block&.call(record) }
@@ -158,8 +174,17 @@ module Ndb
158
174
  definition
159
175
  end
160
176
 
161
- # Declares the collection once per process (and once per client).
177
+ # A collection pattern ("logs-2026-*") reads many collections and
178
+ # writes none.
179
+ def collection_pattern?
180
+ collection_name.include?("*")
181
+ end
182
+
183
+ # Declares the collection once per process (and once per client). A
184
+ # pattern declares nothing.
162
185
  def ensure_collection!
186
+ return if collection_pattern?
187
+
163
188
  client = Rails.client
164
189
 
165
190
  return if @ensured_client.equal?(client)
@@ -28,6 +28,15 @@ module Ndb
28
28
  add_clause(Conditions.equality(attrs))
29
29
  end
30
30
 
31
+ # A where Hash built elsewhere, such as from a search form, sent as is:
32
+ # no Rails conventions (an Array stays an Array, not $in). The daemon
33
+ # validates it. ANDed with the other clauses.
34
+ def where_raw(where)
35
+ raise ArgumentError, "where_raw takes a Hash, got #{where.class}" unless where.is_a?(Hash)
36
+
37
+ add_clause(Conditions.json_value(where))
38
+ end
39
+
31
40
  def or(other)
32
41
  clauses = [Conditions.combine(values[:where]), Conditions.combine(other.values[:where])]
33
42
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Ndb
4
4
  module Rails
5
- VERSION = "0.3.3"
5
+ VERSION = "0.4.0"
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: 0.3.3
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thadeu