basic_serializer 0.1.1 → 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 +15 -7
- data/lib/basic_serializer/config.rb +14 -0
- data/lib/basic_serializer/dsl.rb +33 -0
- data/lib/basic_serializer/struct.rb +3 -0
- data/lib/basic_serializer/swagger.rb +22 -0
- data/lib/basic_serializer.rb +33 -51
- metadata +6 -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
@@ -1,8 +1,8 @@
|
|
1
1
|
# BasicSerializer
|
2
|
+
[![Ruby](https://github.com/aladac/basic_serializer/actions/workflows/main.yml/badge.svg)](https://github.com/aladac/basic_serializer/actions/workflows/main.yml)
|
2
3
|
|
3
4
|
This is a simple serializer for Ruby objects without heavy dependencies.
|
4
5
|
|
5
|
-
|
6
6
|
## Installation
|
7
7
|
|
8
8
|
Add to the application's Gemfile by executing:
|
@@ -17,17 +17,25 @@ 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
|
+
|
24
|
+
format :json, pretty: true # output format and options
|
23
25
|
|
24
|
-
model_name "User"
|
26
|
+
model_name "User" # optional model name metadata
|
25
27
|
schema_ref "#/components/schemas/User" # optional swagger schema reference
|
26
28
|
end
|
27
29
|
|
28
30
|
serializer = UserSerializer.new(name: 'Foobar', email: 'email@domain.com', id: 1)
|
29
|
-
serializer.
|
30
|
-
|
31
|
+
serializer.serialize
|
32
|
+
```
|
33
|
+
```json
|
34
|
+
{
|
35
|
+
"id": 1,
|
36
|
+
"name": "Foobar",
|
37
|
+
"email": "email@domain.com"
|
38
|
+
}
|
31
39
|
```
|
32
40
|
|
33
41
|
## Development
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class BasicSerializer
|
4
|
+
module Config
|
5
|
+
OJ_FORMAT = {
|
6
|
+
mode: :compat,
|
7
|
+
indent: " ",
|
8
|
+
space: " ", # This controls both spaces after commas AND colons
|
9
|
+
space_before: "", # This controls space before colons
|
10
|
+
object_nl: "\n",
|
11
|
+
array_nl: "\n"
|
12
|
+
}.freeze
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class BasicSerializer
|
4
|
+
module DSL
|
5
|
+
def attributes
|
6
|
+
@attributes ||= {}
|
7
|
+
end
|
8
|
+
|
9
|
+
def attribute(name, type)
|
10
|
+
attributes[name] = type
|
11
|
+
|
12
|
+
return if method_defined?(name)
|
13
|
+
|
14
|
+
define_method(name) { object.send(name) }
|
15
|
+
end
|
16
|
+
|
17
|
+
def model_name(name)
|
18
|
+
@model_name ||= name
|
19
|
+
end
|
20
|
+
|
21
|
+
def custom_model_name
|
22
|
+
@model_name || name.gsub("Serializer", "")
|
23
|
+
end
|
24
|
+
|
25
|
+
def schema_ref(ref)
|
26
|
+
@schema_ref ||= ref
|
27
|
+
end
|
28
|
+
|
29
|
+
def format(name, pretty: false)
|
30
|
+
@format ||= { name: name, pretty: pretty }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class BasicSerializer
|
4
|
+
module Swagger
|
5
|
+
def swagger_ref
|
6
|
+
@schema_ref || "#/components/schemas/#{custom_model_name}"
|
7
|
+
end
|
8
|
+
|
9
|
+
def swagger_schema
|
10
|
+
hash = {
|
11
|
+
type: "object",
|
12
|
+
properties: {}
|
13
|
+
}
|
14
|
+
|
15
|
+
attributes.each_pair do |name, type|
|
16
|
+
hash[:properties][name] = { type: type }
|
17
|
+
end
|
18
|
+
|
19
|
+
hash
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/basic_serializer.rb
CHANGED
@@ -1,79 +1,51 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# class UserSerializer < BasicSerializer
|
4
|
-
# attribute :id, :integer
|
5
|
-
# attribute :name, :string
|
6
|
-
# attribute :email, :string
|
7
|
-
# end
|
8
|
-
|
9
3
|
require "oj"
|
10
|
-
|
11
|
-
|
4
|
+
require "yaml"
|
5
|
+
require "basic_serializer/dsl"
|
6
|
+
require "basic_serializer/swagger"
|
7
|
+
require "basic_serializer/struct"
|
8
|
+
require "basic_serializer/config"
|
12
9
|
|
13
10
|
class BasicSerializer
|
14
|
-
|
15
|
-
|
16
|
-
class << self
|
17
|
-
def attributes
|
18
|
-
@attributes ||= {}
|
19
|
-
end
|
20
|
-
|
21
|
-
def attribute(name, type)
|
22
|
-
attributes[name] = type
|
23
|
-
|
24
|
-
return if method_defined?(name)
|
25
|
-
|
26
|
-
define_method(name) { object.send(name) }
|
27
|
-
end
|
28
|
-
|
29
|
-
def model_name(name)
|
30
|
-
@model_name ||= name
|
31
|
-
end
|
32
|
-
|
33
|
-
def custom_model_name
|
34
|
-
@model_name || name.gsub("Serializer", "")
|
35
|
-
end
|
36
|
-
|
37
|
-
def schema_ref(ref)
|
38
|
-
@schema_ref ||= ref
|
39
|
-
end
|
40
|
-
end
|
11
|
+
extend DSL
|
12
|
+
extend Swagger
|
41
13
|
|
42
14
|
attr_reader :object
|
43
15
|
|
44
16
|
def initialize(object)
|
45
17
|
@object = object.is_a?(Hash) ? struct(object) : object
|
18
|
+
remove_instance_variable(:@struct) if defined?(@struct)
|
46
19
|
end
|
47
20
|
|
48
|
-
def
|
21
|
+
def stringified_attributes
|
49
22
|
hash = {}
|
50
23
|
|
51
24
|
self.class.attributes.each_key do |attr_name|
|
52
25
|
hash[attr_name] = send(attr_name)
|
53
26
|
end
|
54
27
|
|
55
|
-
hash
|
28
|
+
deep_stringify_keys(hash)
|
56
29
|
end
|
57
30
|
|
31
|
+
alias as_json stringified_attributes
|
32
|
+
|
58
33
|
def to_json(*_args)
|
59
|
-
|
60
|
-
end
|
34
|
+
pretty = self.class.instance_variable_get(:@format)&.dig(:pretty)
|
61
35
|
|
62
|
-
|
63
|
-
@schema_ref || "#/components/schemas/#{custom_model_name}"
|
36
|
+
Oj.dump(stringified_attributes, **(pretty ? Config::OJ_FORMAT : {}))
|
64
37
|
end
|
65
38
|
|
66
|
-
def
|
67
|
-
|
68
|
-
|
69
|
-
properties: {}
|
70
|
-
}
|
39
|
+
def to_yaml
|
40
|
+
YAML.dump(stringified_attributes)
|
41
|
+
end
|
71
42
|
|
72
|
-
|
73
|
-
|
43
|
+
def serialize
|
44
|
+
case self.class.instance_variable_get(:@format)&.dig(:name)
|
45
|
+
when :json then to_json
|
46
|
+
when :yaml then to_yaml
|
47
|
+
else stringified_attributes
|
74
48
|
end
|
75
|
-
|
76
|
-
hash
|
77
49
|
end
|
78
50
|
|
79
51
|
singleton_class.alias_method :openapi_ref, :swagger_ref
|
@@ -81,10 +53,20 @@ class BasicSerializer
|
|
81
53
|
|
82
54
|
private
|
83
55
|
|
56
|
+
def deep_stringify_keys(hash)
|
57
|
+
hash.transform_keys(&:to_s).transform_values do |value|
|
58
|
+
case value
|
59
|
+
when Hash then deep_stringify_keys(value)
|
60
|
+
when Array then value.map { |item| item.is_a?(Hash) ? deep_stringify_keys(item) : item }
|
61
|
+
else value
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
84
66
|
def struct(object)
|
85
67
|
return @struct if @struct
|
86
68
|
|
87
|
-
basic_struct ||=
|
69
|
+
basic_struct ||= BasicSerializer::Struct.new(*object.keys.map(&:to_sym), keyword_init: true)
|
88
70
|
|
89
71
|
@struct ||= basic_struct.new(**object)
|
90
72
|
end
|
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
|
@@ -25,6 +25,10 @@ files:
|
|
25
25
|
- README.md
|
26
26
|
- Rakefile
|
27
27
|
- lib/basic_serializer.rb
|
28
|
+
- lib/basic_serializer/config.rb
|
29
|
+
- lib/basic_serializer/dsl.rb
|
30
|
+
- lib/basic_serializer/struct.rb
|
31
|
+
- lib/basic_serializer/swagger.rb
|
28
32
|
- sig/simple_serializer.rbs
|
29
33
|
homepage: https://github.com/aladac/basic_serializer
|
30
34
|
licenses:
|