basic_serializer 0.1.1 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -2
- data/lib/basic_serializer/config.rb +13 -0
- data/lib/basic_serializer/dsl.rb +34 -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 +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5bff1df65a7d8f5f4291b756123bdd69b550b7b7b81154acfac6904da08591a1
|
4
|
+
data.tar.gz: 02a7c24410460c42f7a01c4b66abec77643a6753c21924c9fae01753ccadd621
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d9fc3f31a09ad0e253c0b8e8c3924cb9d89495e077e254b1f895c1b54e0445698e3f2e7d008607e504aa27112bd4944091552560c596163a6da504a3eb9bcb2
|
7
|
+
data.tar.gz: 73c410b2132335d89762d04013a985dac5f3c52a46d55da5dc10a3de1a82e2cdfddf15b71b26d148b709120a5a929dbf687c3ed2d68ae629b9229a189fb3a92c
|
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:
|
@@ -21,13 +21,16 @@ class UserSerializer < BasicSerializer
|
|
21
21
|
attribute :name, :string
|
22
22
|
attribute :email, :string
|
23
23
|
|
24
|
+
format :json, pretty: true
|
25
|
+
|
24
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
31
|
serializer.to_json
|
30
|
-
serializer.
|
32
|
+
serializer.to_yaml
|
33
|
+
serializer.serialize
|
31
34
|
```
|
32
35
|
|
33
36
|
## Development
|
@@ -0,0 +1,34 @@
|
|
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
|
31
|
+
@format ||= pretty
|
32
|
+
end
|
33
|
+
end
|
34
|
+
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(:@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)
|
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,7 +1,7 @@
|
|
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.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Ladachowski
|
@@ -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:
|