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 +4 -4
- data/README.md +28 -5
- data/VERSION +1 -1
- data/lib/serega/plugins/presenter/presenter.rb +30 -5
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a32fcd6506959b5498ef6357c27110bd7623d0bf16e28d0485703016704e6cf4
|
|
4
|
+
data.tar.gz: 4e2bf60aba2c1db746587b1135a3d852e34858606ac30c6947c960d4e2dba726
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
-
|
|
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 :
|
|
783
|
+
attribute :role
|
|
772
784
|
|
|
773
785
|
class Presenter
|
|
774
786
|
def name
|
|
775
|
-
[first_name, last_name].
|
|
787
|
+
[first_name, last_name].compact.join(' ')
|
|
776
788
|
end
|
|
777
789
|
|
|
778
|
-
def
|
|
779
|
-
[
|
|
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.
|
|
1
|
+
0.35.0
|
|
@@ -6,16 +6,28 @@ require "forwardable"
|
|
|
6
6
|
class Serega
|
|
7
7
|
module SeregaPlugins
|
|
8
8
|
#
|
|
9
|
-
# Plugin
|
|
9
|
+
# Plugin :presenter — moves computed attribute logic into a dedicated Presenter class.
|
|
10
10
|
#
|
|
11
|
-
#
|
|
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].
|
|
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.
|
|
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.
|
|
124
|
+
rubygems_version: 4.0.11
|
|
125
125
|
specification_version: 4
|
|
126
126
|
summary: JSON Serializer
|
|
127
127
|
test_files: []
|