serega 0.37.0 → 0.37.1

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: cf4c5f1a34256084ebedcba18d08dcdc30f9a3090ca3b555a7ee91fc8e0dfc44
4
- data.tar.gz: 05022337ad49d098c50f25c267f33a2db6c210b169f3243b88c010a5f9bc519e
3
+ metadata.gz: ca01b1c5668c5ce5963e5f2b71540dae1d023eed44c6e9e026d43e63fcead4c6
4
+ data.tar.gz: f526d737921efe1789751b51ff15b503b0febe2d5aafbb1a57666625a14b2228
5
5
  SHA512:
6
- metadata.gz: a9b5a2797086500903904617124810a4401f0a223ea75ba88d78c6cf3cba90157e9ff666f54fd76183fbbed4ab54d7be33704fd7c45665537e825f5f0f67222c
7
- data.tar.gz: 150fcf62d92d01076c61d38a4189a31d789904fc643c2ce06c569280099d598582e3e07b7ff32a3482e0754d47045123dcf7f01baf7f7f65afebe2c1465e3c93
6
+ metadata.gz: 643c12f65a16dbce5d05a8262df0f85fc6a5055436a65d2590f0615ccba5a9cb2d1287d4ec1262b9d0f252432262e88799b55d13ca7ce5fed252c58faf9b7a1a
7
+ data.tar.gz: fa8ad847f04617c8ea70cf227aca9bf9ede02c1b1449afb6547b821f09cd8a27638cb5d8904ddc576dfe12c01d5cd6991a99e7cb9fa1cf28d46d2053c7650978
data/README.md CHANGED
@@ -530,12 +530,12 @@ attribute is serialized:
530
530
 
531
531
  ```ruby
532
532
  class UserSerializer < Serega
533
+ attribute :name
534
+ attribute :posts, serializer: PostSerializer, preload: :posts
535
+
533
536
  # `objects` is the gathered users; `preloads` is this attribute's
534
537
  # `:preload` value (`:posts`). `MyORM.preload` does the eager loading.
535
538
  preload_with { |objects, preloads| MyORM.preload(objects, preloads) }
536
-
537
- attribute :name
538
- attribute :posts, serializer: PostSerializer, preload: :posts
539
539
  end
540
540
  ```
541
541
 
@@ -547,6 +547,33 @@ objects can be plain Ruby objects too: the handler decides where the data comes
547
547
  from (another ORM, an HTTP API, a cache) and how to attach it to the objects,
548
548
  by whatever rules you define.
549
549
 
550
+ The handler runs **once per preloaded attribute**, not once per distinct
551
+ `:preload` value. If the same `:preload` is declared on several attributes, the
552
+ handler is invoked once for each of them over the same objects, so it should
553
+ check whether the data is already loaded before loading it again. (The
554
+ `:activerecord_preloads` handler relies on `ActiveRecord::Associations::Preloader`,
555
+ which already skips associations that are loaded.)
556
+
557
+ Because you choose the `:preload` value, it doubles as a **discriminator**: give
558
+ two attributes different `:preload` values and branch on `preloads` inside the
559
+ handler — no need for the value to be a real association name.
560
+
561
+ ```ruby
562
+ class PostSerializer < Serega
563
+ attribute :owner, serializer: UserSerializer, preload: :owner
564
+ attribute :author, serializer: UserSerializer, preload: :author
565
+
566
+ # Both attributes load the same person, but from different sources.
567
+ preload_with do |objects, preloads|
568
+ case preloads
569
+ when :owner then load_owners(objects) # e.g. from the DB
570
+ when :author then load_authors(objects) # e.g. from an external API
571
+ else MyORM.preload(objects, preloads)
572
+ end
573
+ end
574
+ end
575
+ ```
576
+
550
577
  ---
551
578
 
552
579
  ## Plugins
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.37.0
1
+ 0.37.1
@@ -275,8 +275,11 @@ class Serega
275
275
  super
276
276
  end
277
277
 
278
- def attach_final_value(value, point, _container)
279
- return KEY_SKIPPED unless point.satisfy_if_value_conditions?(value, context)
278
+ def attach_final_value(value, point, container)
279
+ unless point.satisfy_if_value_conditions?(value, context)
280
+ container.delete(point.name) # remove reserved placeholder
281
+ return KEY_SKIPPED
282
+ end
280
283
 
281
284
  super
282
285
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serega
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.37.0
4
+ version: 0.37.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Glushkov