bright_serializer 0.2.5 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -2
- data/Gemfile +1 -0
- data/Gemfile.lock +20 -1
- data/README.md +35 -9
- data/lib/bright_serializer/attribute.rb +6 -2
- data/lib/bright_serializer/entity/base.rb +8 -1
- data/lib/bright_serializer/inflector.rb +17 -7
- data/lib/bright_serializer/serializer.rb +1 -1
- data/lib/bright_serializer/version.rb +1 -1
- 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: 14d3fd3a7bdf72b5aa371e64a7e7b9d493b808734a8b00d775acaf86d1e3b559
|
4
|
+
data.tar.gz: cddb984181ac63722748a9196f6f5b9cee6c4102afe8da78ab03ba14732e68b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b66d065bce918b1ae112a5338188f314f7c419549ee25b385eb343f436a3d1a623397986f76a642c276109048d40f4b7957623b28ff421addcbe1336218dceec
|
7
|
+
data.tar.gz: '08a8226f13ad37cef36113bdeb373b4ba76b7b44881726feccfcbcacdeddc5651b58e5fcd0709c358cbdf3fc54922844b9d561cfbfe1cd62f32d232c171cc611'
|
data/CHANGELOG.md
CHANGED
@@ -2,7 +2,12 @@
|
|
2
2
|
|
3
3
|
## master (unreleased)
|
4
4
|
|
5
|
-
|
5
|
+
## 0.3.0 (2022-05-26)
|
6
|
+
|
7
|
+
* Allow to evaluate entity values with a callable lambda. ([#88](https://github.com/petalmd/bright_serializer/pull/88))
|
8
|
+
* Fix `FrozenError (can't modify frozen Array)` when parsing entity. ([#83](https://github.com/petalmd/bright_serializer/pull/83))
|
9
|
+
* Added the support to use instance methods from a serializer class in the library ([#85](https://github.com/petalmd/bright_serializer/pull/85))
|
10
|
+
* Use real coveralls_reborn gem
|
6
11
|
|
7
12
|
## 0.2.5 (2021-03-08)
|
8
13
|
|
@@ -33,7 +38,7 @@
|
|
33
38
|
* Define entity in serializer for grape_swagger ([#9](https://github.com/petalmd/bright_serializer/pull/9))
|
34
39
|
|
35
40
|
## 0.1.1 (2020-07-13)
|
36
|
-
|
41
|
+
|
37
42
|
* Add description in gemspec file
|
38
43
|
* Add content in CHANGELOG.md
|
39
44
|
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
bright_serializer (0.
|
4
|
+
bright_serializer (0.3.0)
|
5
5
|
oj (~> 3)
|
6
6
|
|
7
7
|
GEM
|
@@ -9,7 +9,13 @@ GEM
|
|
9
9
|
specs:
|
10
10
|
ast (2.4.1)
|
11
11
|
concurrent-ruby (1.1.7)
|
12
|
+
coveralls_reborn (0.22.0)
|
13
|
+
simplecov (>= 0.18.1, < 0.22.0)
|
14
|
+
term-ansicolor (~> 1.6)
|
15
|
+
thor (>= 0.20.3, < 2.0)
|
16
|
+
tins (~> 1.16)
|
12
17
|
diff-lcs (1.4.4)
|
18
|
+
docile (1.4.0)
|
13
19
|
faker (2.15.1)
|
14
20
|
i18n (>= 1.6, < 2)
|
15
21
|
i18n (1.8.5)
|
@@ -53,6 +59,18 @@ GEM
|
|
53
59
|
rubocop (~> 0.87)
|
54
60
|
rubocop-ast (>= 0.7.1)
|
55
61
|
ruby-progressbar (1.11.0)
|
62
|
+
simplecov (0.21.2)
|
63
|
+
docile (~> 1.1)
|
64
|
+
simplecov-html (~> 0.11)
|
65
|
+
simplecov_json_formatter (~> 0.1)
|
66
|
+
simplecov-html (0.12.3)
|
67
|
+
simplecov_json_formatter (0.1.3)
|
68
|
+
sync (0.5.0)
|
69
|
+
term-ansicolor (1.7.1)
|
70
|
+
tins (~> 1.0)
|
71
|
+
thor (1.1.0)
|
72
|
+
tins (1.29.1)
|
73
|
+
sync
|
56
74
|
unicode-display_width (1.7.0)
|
57
75
|
|
58
76
|
PLATFORMS
|
@@ -61,6 +79,7 @@ PLATFORMS
|
|
61
79
|
DEPENDENCIES
|
62
80
|
bright_serializer!
|
63
81
|
bundler (~> 2)
|
82
|
+
coveralls_reborn
|
64
83
|
faker
|
65
84
|
oj
|
66
85
|
rake (~> 13.0)
|
data/README.md
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
[![Actions Status](https://github.com/petalmd/bright_serializer/workflows/Build/badge.svg)](https://github.com/petalmd/bright_serializer/actions?query=workflow%3ABuild)
|
2
2
|
[![Gem Version](https://badge.fury.io/rb/bright_serializer.svg)](https://badge.fury.io/rb/bright_serializer)
|
3
|
+
[![Coverage Status](https://coveralls.io/repos/github/petalmd/bright_serializer/badge.svg?branch=master)](https://coveralls.io/github/petalmd/bright_serializer?branch=master)
|
3
4
|
|
4
5
|
# BrightSerializer
|
5
6
|
|
@@ -31,12 +32,12 @@ Create a class and include `BrightSerializer::Serializer`
|
|
31
32
|
class AccountSerializer
|
32
33
|
include BrightSerializer::Serializer
|
33
34
|
attributes :id, :first_name, :last_name
|
34
|
-
|
35
|
+
|
35
36
|
# With a block
|
36
37
|
attribute :name do |object|
|
37
38
|
"#{object.first_name} #{object.last_name}"
|
38
39
|
end
|
39
|
-
|
40
|
+
|
40
41
|
# With a block shorter
|
41
42
|
attribute :created_at, &:to_s
|
42
43
|
end
|
@@ -53,7 +54,7 @@ You can pass params to your serializer. For example to have more context with th
|
|
53
54
|
class AccountSerializer
|
54
55
|
include BrightSerializer::Serializer
|
55
56
|
attributes :id, :first_name, :last_name
|
56
|
-
|
57
|
+
|
57
58
|
attribute :friend do |object, params|
|
58
59
|
object.is_friend_with? params[:current_user]
|
59
60
|
end
|
@@ -65,14 +66,14 @@ AccountSerializer.new(Account.first, params: { current_user: current_user }).to_
|
|
65
66
|
|
66
67
|
### Conditional Attributes
|
67
68
|
|
68
|
-
Attribute can be remove from serialization by passing a `proc` to the option `if`. If the proc return `true` the attibute
|
69
|
-
will be serialize. `object` and `params` or accessible.
|
69
|
+
Attribute can be remove from serialization by passing a `proc` to the option `if`. If the proc return `true` the attibute
|
70
|
+
will be serialize. `object` and `params` or accessible.
|
70
71
|
|
71
72
|
```ruby
|
72
73
|
class AccountSerializer
|
73
74
|
include BrightSerializer::Serializer
|
74
75
|
attributes :id, :first_name, :last_name
|
75
|
-
|
76
|
+
|
76
77
|
attribute :email, if: -> { |object, params| params[:current_user].is_admin? }
|
77
78
|
end
|
78
79
|
```
|
@@ -118,7 +119,7 @@ end
|
|
118
119
|
class AccountSerializer
|
119
120
|
include BrightSerializer::Serializer
|
120
121
|
attributes :id, :first_name, :last_name
|
121
|
-
|
122
|
+
|
122
123
|
attribute :friends do |object|
|
123
124
|
FriendSerializer.new(object.friends)
|
124
125
|
end
|
@@ -128,7 +129,7 @@ end
|
|
128
129
|
### Entity
|
129
130
|
|
130
131
|
You can define the entity of your serializer to generate documentation with the option `entity`.
|
131
|
-
The feature was build to work with [grape-swagger](https://github.com/ruby-grape/grape-swagger).
|
132
|
+
The feature was build to work with [grape-swagger](https://github.com/ruby-grape/grape-swagger).
|
132
133
|
For more information about defining a model entity see the [Swagger documentation](https://swagger.io/specification/v2/?sbsearch=array%20response#schema-object).
|
133
134
|
|
134
135
|
```ruby
|
@@ -138,12 +139,37 @@ class AccountSerializer
|
|
138
139
|
attribute :name
|
139
140
|
|
140
141
|
attribute :friends,
|
141
|
-
entity: {
|
142
|
+
entity: {
|
142
143
|
type: :array, items: { ref: 'FriendSerializer' }, description: 'The list the account friends.'
|
143
144
|
} do |object|
|
144
145
|
FriendSerializer.new(object.friends)
|
145
146
|
end
|
146
147
|
end
|
148
|
+
|
149
|
+
```
|
150
|
+
Callable values are supported.
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
{ entity: { type: :string, enum: -> { SomeModel::ENUMVALUES } } }
|
154
|
+
```
|
155
|
+
|
156
|
+
### Instance
|
157
|
+
|
158
|
+
If you have defined instance methods inside your serializer you can access them inside block attribute.
|
159
|
+
|
160
|
+
```ruby
|
161
|
+
class AccountSerializer
|
162
|
+
include BrightSerializer::Serializer
|
163
|
+
attributes :id, :name
|
164
|
+
|
165
|
+
attribute :print do |object|
|
166
|
+
print_account(object)
|
167
|
+
end
|
168
|
+
|
169
|
+
def print_account(object)
|
170
|
+
"Account: #{object.name}"
|
171
|
+
end
|
172
|
+
end
|
147
173
|
```
|
148
174
|
|
149
175
|
## Development
|
@@ -14,12 +14,16 @@ module BrightSerializer
|
|
14
14
|
@entity = entity ? Entity::Base.new(entity) : nil
|
15
15
|
end
|
16
16
|
|
17
|
-
def serialize(object, params)
|
17
|
+
def serialize(serializer_instance, object, params)
|
18
18
|
return unless object
|
19
19
|
|
20
20
|
value =
|
21
21
|
if @block
|
22
|
-
@block.arity.abs == 1
|
22
|
+
if @block.arity.abs == 1
|
23
|
+
serializer_instance.instance_exec(object, &@block)
|
24
|
+
else
|
25
|
+
serializer_instance.instance_exec(object, params, &@block)
|
26
|
+
end
|
23
27
|
elsif object.is_a?(Hash)
|
24
28
|
object.key?(key) ? object[key] : object[key.to_s]
|
25
29
|
else
|
@@ -14,8 +14,9 @@ module BrightSerializer
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def to_h
|
17
|
-
Inflector.deep_transform_keys_in_object
|
17
|
+
@definition = Inflector.deep_transform_keys_in_object(@definition) { |k| Inflector.camel_lower k.to_s }
|
18
18
|
parse_ref!
|
19
|
+
evaluate_callable!
|
19
20
|
@definition
|
20
21
|
end
|
21
22
|
|
@@ -28,6 +29,12 @@ module BrightSerializer
|
|
28
29
|
object['$ref'] = relation
|
29
30
|
end
|
30
31
|
|
32
|
+
def evaluate_callable!
|
33
|
+
@definition = Inflector.deep_transform_values_in_object(@definition) do |value|
|
34
|
+
value.respond_to?(:call) ? value.call : value
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
31
38
|
def nested_hash(obj, key)
|
32
39
|
if obj.respond_to?(:key?) && obj.key?(key)
|
33
40
|
obj
|
@@ -60,20 +60,30 @@ class Inflector
|
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
63
|
-
# File active_support/core_ext/hash/keys.rb, line
|
64
|
-
def deep_transform_keys_in_object
|
63
|
+
# File active_support/core_ext/hash/keys.rb, line 116
|
64
|
+
def deep_transform_keys_in_object(object, &block)
|
65
65
|
case object
|
66
66
|
when Hash
|
67
|
-
object.
|
68
|
-
|
69
|
-
object[yield(key)] = deep_transform_keys_in_object!(value, &block)
|
67
|
+
object.each_with_object({}) do |(key, value), result|
|
68
|
+
result[yield(key)] = deep_transform_keys_in_object(value, &block)
|
70
69
|
end
|
71
|
-
object
|
72
70
|
when Array
|
73
|
-
object.map
|
71
|
+
object.map { |e| deep_transform_keys_in_object(e, &block) }
|
74
72
|
else
|
75
73
|
object
|
76
74
|
end
|
77
75
|
end
|
76
|
+
|
77
|
+
# File active_support/core_ext/hash/deep_transform_values.rb, line 25
|
78
|
+
def deep_transform_values_in_object(object, &block)
|
79
|
+
case object
|
80
|
+
when Hash
|
81
|
+
object.transform_values { |value| deep_transform_values_in_object(value, &block) }
|
82
|
+
when Array
|
83
|
+
object.map { |e| deep_transform_values_in_object(e, &block) }
|
84
|
+
else
|
85
|
+
yield(object)
|
86
|
+
end
|
87
|
+
end
|
78
88
|
end
|
79
89
|
end
|
@@ -28,7 +28,7 @@ module BrightSerializer
|
|
28
28
|
next if @fields.any? && !@fields.include?(attribute.key)
|
29
29
|
next unless attribute.condition?(object, @params)
|
30
30
|
|
31
|
-
result[attribute.transformed_key] = attribute.serialize(object, @params)
|
31
|
+
result[attribute.transformed_key] = attribute.serialize(self, object, @params)
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bright_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jean-Francis Bastien
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-05-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oj
|