basic_serializer 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +12 -7
- data/lib/basic_serializer/config.rb +4 -3
- data/lib/basic_serializer/dsl.rb +1 -2
- data/lib/basic_serializer.rb +2 -2
- 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: 92045b1fba9ae2936d58f4dc47127467a4264dd53e7e1622a410d0f75e984daf
|
4
|
+
data.tar.gz: e2c8f5bd89a2955a10c505ab88449f5c8e19e3ad83de58a0dafcb81f357ba286
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 101db57a5015c1c6be60590dfb7c5a04d4144a30811a0a1ade238ed1492dbc156d076cd4691ad062d277d6765df4919918d532ffe320ca8544a3f92f41799217
|
7
|
+
data.tar.gz: 7202ed9ffea28ba0c26f71577b68c06fc6b66ff62da718c0af91689f1be3834b89ecc53f42f0165d5ffc05ae0ad25f6d757c3a7ec4271d4ee02f5587a04a0ddd
|
data/README.md
CHANGED
@@ -17,21 +17,26 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
17
17
|
|
18
18
|
```ruby
|
19
19
|
class UserSerializer < BasicSerializer
|
20
|
-
attribute :id, :integer
|
21
|
-
attribute :name, :string
|
22
|
-
attribute :email, :string
|
20
|
+
attribute :id, :integer # attribute definition
|
21
|
+
attribute :name, :string # attribute definition
|
22
|
+
attribute :email, :string # attribute definition
|
23
23
|
|
24
|
-
format :json, pretty: true
|
24
|
+
format :json, pretty: true # output format and options
|
25
25
|
|
26
|
-
model_name "User"
|
26
|
+
model_name "User" # optional model name metadata
|
27
27
|
schema_ref "#/components/schemas/User" # optional swagger schema reference
|
28
28
|
end
|
29
29
|
|
30
30
|
serializer = UserSerializer.new(name: 'Foobar', email: 'email@domain.com', id: 1)
|
31
|
-
serializer.to_json
|
32
|
-
serializer.to_yaml
|
33
31
|
serializer.serialize
|
34
32
|
```
|
33
|
+
```json
|
34
|
+
{
|
35
|
+
"id": 1,
|
36
|
+
"name": "Foobar",
|
37
|
+
"email": "email@domain.com"
|
38
|
+
}
|
39
|
+
```
|
35
40
|
|
36
41
|
## Development
|
37
42
|
|
@@ -3,9 +3,10 @@
|
|
3
3
|
class BasicSerializer
|
4
4
|
module Config
|
5
5
|
OJ_FORMAT = {
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
mode: :compat,
|
7
|
+
indent: " ",
|
8
|
+
space: " ", # This controls both spaces after commas AND colons
|
9
|
+
space_before: "", # This controls space before colons
|
9
10
|
object_nl: "\n",
|
10
11
|
array_nl: "\n"
|
11
12
|
}.freeze
|
data/lib/basic_serializer/dsl.rb
CHANGED
data/lib/basic_serializer.rb
CHANGED
@@ -31,7 +31,7 @@ class BasicSerializer
|
|
31
31
|
alias as_json stringified_attributes
|
32
32
|
|
33
33
|
def to_json(*_args)
|
34
|
-
pretty = self.class.instance_variable_get(:@pretty)
|
34
|
+
pretty = self.class.instance_variable_get(:@format)&.dig(:pretty)
|
35
35
|
|
36
36
|
Oj.dump(stringified_attributes, **(pretty ? Config::OJ_FORMAT : {}))
|
37
37
|
end
|
@@ -41,7 +41,7 @@ class BasicSerializer
|
|
41
41
|
end
|
42
42
|
|
43
43
|
def serialize
|
44
|
-
case self.class.instance_variable_get(:@format)
|
44
|
+
case self.class.instance_variable_get(:@format)&.dig(:name)
|
45
45
|
when :json then to_json
|
46
46
|
when :yaml then to_yaml
|
47
47
|
else stringified_attributes
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: basic_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Ladachowski
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-11-
|
11
|
+
date: 2024-11-07 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A lightweight and straightforward serializer for Ruby objects without
|
14
14
|
heavy dependencies
|