serega 0.34.0 → 0.35.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: bbda67f020296ab580f7cde1b9f867c5ab68b84f3c4cfa9c7f7517d87367640b
4
- data.tar.gz: bfea118712593fc12ea96bdea8dd3d8245bf01239474b6a034d408f1fdf04342
3
+ metadata.gz: a32fcd6506959b5498ef6357c27110bd7623d0bf16e28d0485703016704e6cf4
4
+ data.tar.gz: 4e2bf60aba2c1db746587b1135a3d852e34858606ac30c6947c960d4e2dba726
5
5
  SHA512:
6
- metadata.gz: 29d6e54cd973362751eda5533765cb44d8dac8621b77f335956de25d85e436e8c71889c72d4bb599a2114f98b9e2bc459c66f3a24f1e3e44441230ca32f923e3
7
- data.tar.gz: bf4ea013fbd1a69e8c742964116834ae3bd4d5c4dc8dd62dd864acfcc53268277fb187a3b4f0b9a6cfb077464e6f24e4fd6222d54e6a54c53054775fe2c1707f
6
+ metadata.gz: 8cb7d76c325320e144dd7de3dcfc376a03386a7742f7eea68cae1bbb421b0cbe969e2c858fbae253114bbb3ed86e63d4c689395976ac94463813a8229ae66f9e
7
+ data.tar.gz: e4595313b09bb2fcb7f37c6e26ebe9376a5caf86c66ed2ab4a19091aae854f1c51fafd0adea54cae0ac97eeb69e770e74f7e66d1aee44a8714d50ae225b8683a
data/README.md CHANGED
@@ -761,27 +761,50 @@ end
761
761
 
762
762
  ### Plugin :presenter
763
763
 
764
- Helps to write clean code by using a Presenter class.
764
+ Moves computed attribute logic out of blocks and into a dedicated `Presenter` class,
765
+ keeping serializers readable as schemas rather than bags of lambdas.
766
+
767
+ Without the plugin, computed attributes live as inline blocks:
768
+
769
+ ```ruby
770
+ class UserSerializer < Serega
771
+ attribute :name, value: proc { |u| [u.first_name, u.last_name].compact.join(' ') }
772
+ attribute :role, value: proc { |u, ctx| u == ctx[:current_user] ? :self : :other }
773
+ end
774
+ ```
775
+
776
+ With the plugin, they move into a clean class:
765
777
 
766
778
  ```ruby
767
779
  class UserSerializer < Serega
768
780
  plugin :presenter
769
781
 
770
782
  attribute :name
771
- attribute :address
783
+ attribute :role
772
784
 
773
785
  class Presenter
774
786
  def name
775
- [first_name, last_name].compact_blank.join(' ')
787
+ [first_name, last_name].compact.join(' ')
776
788
  end
777
789
 
778
- def address
779
- [country, city, address].join("\n")
790
+ def role
791
+ id == __ctx__[:current_user_id] ? :self : :other
780
792
  end
781
793
  end
782
794
  end
783
795
  ```
784
796
 
797
+ `Presenter` inherits from `SimpleDelegator`, so every method of the serialized
798
+ object is available directly inside presenter methods. Any method not explicitly
799
+ defined on `Presenter` is resolved via `method_missing` on the first call, which
800
+ also defines a real delegator method — so all subsequent serializations call it
801
+ directly, without going through `method_missing` again.
802
+
803
+ The original wrapped object is accessible via `__getobj__` (standard
804
+ `SimpleDelegator` API) when you need an unambiguous reference to it.
805
+
806
+ The serialization context is accessible via the private method `__ctx__`.
807
+
785
808
  ### Plugin :string_modifiers
786
809
 
787
810
  Allows to specify modifiers as strings.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.34.0
1
+ 0.35.0
@@ -6,16 +6,28 @@ require "forwardable"
6
6
  class Serega
7
7
  module SeregaPlugins
8
8
  #
9
- # Plugin Presenter adds possibility to use declare Presenter for your objects inside serializer
9
+ # Plugin :presenter moves computed attribute logic into a dedicated Presenter class.
10
10
  #
11
- # class User < Serega
11
+ # Presenter inherits from SimpleDelegator:
12
+ # - All methods of the serialized object are available directly inside presenter methods.
13
+ # - Methods not defined on Presenter are resolved via method_missing on the first call
14
+ # and then defined as real delegators, so subsequent calls skip method_missing entirely.
15
+ # - The original object is accessible via __getobj__ (standard SimpleDelegator API).
16
+ # - The serialization context is accessible via the private method __ctx__.
17
+ #
18
+ # class UserSerializer < Serega
12
19
  # plugin :presenter
13
20
  #
14
21
  # attribute :name
22
+ # attribute :role
15
23
  #
16
24
  # class Presenter
17
25
  # def name
18
- # [first_name, last_name].compact_blank.join(' ')
26
+ # [first_name, last_name].compact.join(' ') # first_name/last_name delegated to object
27
+ # end
28
+ #
29
+ # def role
30
+ # id == __ctx__[:current_user_id] ? :self : :other
19
31
  # end
20
32
  # end
21
33
  # end
@@ -56,6 +68,19 @@ class Serega
56
68
  class Presenter < SimpleDelegator
57
69
  # Presenter instance methods
58
70
  module InstanceMethods
71
+ #
72
+ # @param object [Object] Serialized object to wrap
73
+ # @param ctx [Hash, nil] Serialization context
74
+ #
75
+ def initialize(object, ctx = nil)
76
+ super(object)
77
+ @__ctx__ = ctx
78
+ end
79
+
80
+ private
81
+
82
+ attr_reader :__ctx__
83
+
59
84
  #
60
85
  # Delegates all missing methods to serialized object.
61
86
  #
@@ -100,10 +125,10 @@ class Serega
100
125
  private
101
126
 
102
127
  #
103
- # Replaces serialized object with Presenter.new(object)
128
+ # Replaces serialized object with Presenter.new(object, ctx)
104
129
  #
105
130
  def serialize_object(object)
106
- object = self.class.serializer_class::Presenter.new(object)
131
+ object = self.class.serializer_class::Presenter.new(object, context)
107
132
  super
108
133
  end
109
134
  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.34.0
4
+ version: 0.35.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Glushkov
@@ -121,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
121
  - !ruby/object:Gem::Version
122
122
  version: '0'
123
123
  requirements: []
124
- rubygems_version: 4.0.9
124
+ rubygems_version: 4.0.11
125
125
  specification_version: 4
126
126
  summary: JSON Serializer
127
127
  test_files: []