fast_serializer_ruby 0.6.5 → 0.6.9
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/.github/workflows/ruby.yml +1 -1
- data/Gemfile +0 -4
- data/README.md +1 -1
- data/lib/fast_serializer/json_model/attribute.rb +27 -18
- data/lib/fast_serializer/json_model/node.rb +0 -10
- data/lib/fast_serializer/json_model/object.rb +2 -3
- data/lib/fast_serializer/json_model/relationship.rb +3 -3
- data/lib/fast_serializer/schema.rb +27 -24
- data/lib/fast_serializer/utils.rb +3 -1
- data/lib/fast_serializer/version.rb +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f2940f6fcbb0acd2b9606012952b2a49f2a50e2ae1af63ea42d7d31868783204
|
|
4
|
+
data.tar.gz: dae74a23431c7230147e1285cd3920c25ebb6573a012c13cabdbcc1607293ddc
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 208cac4f8ace7a76264b5d5175d290c884f1364978a2c3687ed80cc9bad9ad3d9874701547cd1c95a7a2eaeffe06678b9ed18b2e3069905a6b5a011b8685414e
|
|
7
|
+
data.tar.gz: 389bce4ee62355324e7aaffcc187f6201305a987fed082f83c339f01d662017f2c2303e0a8399d66193f2167589f7f638f220c859aa10aacfcf7672c4222ea67
|
data/.github/workflows/ruby.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
[](https://badge.fury.io/rb/fast_serializer_ruby)
|
|
2
|
-
|
|
2
|
+

|
|
3
3
|
[](https://codeclimate.com/github/estepnv/fast_serializer/maintainability)
|
|
4
4
|
[](https://codeclimate.com/github/estepnv/fast_serializer/test_coverage)
|
|
5
5
|
|
|
@@ -3,7 +3,11 @@
|
|
|
3
3
|
module FastSerializer
|
|
4
4
|
module JsonModel
|
|
5
5
|
class Attribute < Node
|
|
6
|
-
attr_accessor :
|
|
6
|
+
attr_accessor :key,
|
|
7
|
+
:method,
|
|
8
|
+
:context,
|
|
9
|
+
:opts,
|
|
10
|
+
:mixin,
|
|
7
11
|
:method_name,
|
|
8
12
|
:method_arity,
|
|
9
13
|
:cond,
|
|
@@ -11,18 +15,25 @@ module FastSerializer
|
|
|
11
15
|
:cond_method_name,
|
|
12
16
|
:injected
|
|
13
17
|
|
|
14
|
-
def initialize(
|
|
15
|
-
super
|
|
18
|
+
def initialize(key, method, opts = {})
|
|
19
|
+
super()
|
|
16
20
|
|
|
17
|
-
@
|
|
18
|
-
@method_name =
|
|
21
|
+
@opts = opts || {}
|
|
22
|
+
@injected_methods = Hash.new { |h, method_name| h[method_name] = true }
|
|
23
|
+
@key = key.to_sym
|
|
24
|
+
@method = !method ? key : method
|
|
25
|
+
|
|
26
|
+
@mixin = Module.new
|
|
19
27
|
@injected = false
|
|
20
|
-
@cond_method_name = nil
|
|
21
|
-
@cond = nil
|
|
22
|
-
@cond = @opts[:if] || @opts[:unless] || @cond
|
|
23
28
|
|
|
24
|
-
|
|
25
|
-
|
|
29
|
+
@method_name = @method && !@method.is_a?(Proc) ? @method : nil
|
|
30
|
+
@method_arity = @method.is_a?(Proc) ? [@method.arity, 0].max : nil
|
|
31
|
+
@cond = @opts[:if] || @opts[:unless]
|
|
32
|
+
@cond_method_name = @cond && !@cond.is_a?(Proc) ? @cond : nil
|
|
33
|
+
@cond_arity = @cond.is_a?(Proc) ? [@cond.arity, 0].max : nil
|
|
34
|
+
|
|
35
|
+
init_with_proc if @method.is_a?(Proc)
|
|
36
|
+
init_with_cond if @cond && @cond.is_a?(Proc)
|
|
26
37
|
end
|
|
27
38
|
|
|
28
39
|
def injectable?
|
|
@@ -39,14 +50,14 @@ module FastSerializer
|
|
|
39
50
|
# @param context [Hash]
|
|
40
51
|
# @return [Object]
|
|
41
52
|
def serialize(resource, params, context)
|
|
42
|
-
can_execute_on_mixin = injected &&
|
|
53
|
+
can_execute_on_mixin = !!(injected && method_name && @injected_methods.key?(method_name) && context)
|
|
43
54
|
|
|
44
55
|
val = if can_execute_on_mixin
|
|
45
56
|
call_method_on_context(context, method_name, method_arity, resource, params)
|
|
46
57
|
elsif method.is_a?(Proc)
|
|
47
58
|
call_proc_binding_to_context(context, method, method_arity, resource, params)
|
|
48
59
|
else
|
|
49
|
-
resource.public_send(
|
|
60
|
+
resource.public_send(method_name)
|
|
50
61
|
end
|
|
51
62
|
|
|
52
63
|
val.freeze
|
|
@@ -59,9 +70,9 @@ module FastSerializer
|
|
|
59
70
|
# @param context [Hash]
|
|
60
71
|
# @return [Boolean]
|
|
61
72
|
def included?(resource, params, context)
|
|
62
|
-
return true if cond
|
|
73
|
+
return true if !cond
|
|
63
74
|
|
|
64
|
-
can_execute_on_mixin = injected &&
|
|
75
|
+
can_execute_on_mixin = !!(injected && cond_method_name && @injected_methods.key?(cond_method_name) && context)
|
|
65
76
|
|
|
66
77
|
res = if can_execute_on_mixin
|
|
67
78
|
call_method_on_context(context, cond_method_name, cond_arity, resource, params)
|
|
@@ -80,8 +91,7 @@ module FastSerializer
|
|
|
80
91
|
|
|
81
92
|
def init_with_cond
|
|
82
93
|
@cond_method_name = "__#{key}_cond__"
|
|
83
|
-
@
|
|
84
|
-
@mixin ||= Module.new
|
|
94
|
+
@injected_methods[@cond_method_name]
|
|
85
95
|
|
|
86
96
|
if RUBY_VERSION <= '2.5.0'
|
|
87
97
|
@mixin.redefine_method @cond_method_name, &cond
|
|
@@ -92,8 +102,7 @@ module FastSerializer
|
|
|
92
102
|
|
|
93
103
|
def init_with_proc
|
|
94
104
|
@method_name = "__#{key}__"
|
|
95
|
-
@
|
|
96
|
-
@mixin = Module.new
|
|
105
|
+
@injected_methods[@method_name]
|
|
97
106
|
|
|
98
107
|
if RUBY_VERSION <= '2.5.0'
|
|
99
108
|
@mixin.redefine_method @method_name, &method
|
|
@@ -3,16 +3,6 @@
|
|
|
3
3
|
module FastSerializer
|
|
4
4
|
module JsonModel
|
|
5
5
|
class Node
|
|
6
|
-
attr_accessor :key, :method, :context
|
|
7
|
-
|
|
8
|
-
# @param key [String]
|
|
9
|
-
# @param method [String]
|
|
10
|
-
# @param opts [Hash]
|
|
11
|
-
def initialize(key: nil, method: nil, opts: {}, **_)
|
|
12
|
-
@key = key&.to_sym
|
|
13
|
-
@method = method || key
|
|
14
|
-
@opts = opts || {}
|
|
15
|
-
end
|
|
16
6
|
|
|
17
7
|
# @return [Boolean]
|
|
18
8
|
def injectable?
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
|
-
|
|
3
2
|
module FastSerializer
|
|
4
3
|
module JsonModel
|
|
5
4
|
class Object < Node
|
|
6
5
|
attr_accessor :attributes
|
|
7
6
|
|
|
8
|
-
def initialize
|
|
7
|
+
def initialize
|
|
9
8
|
super
|
|
10
9
|
@attributes = {}
|
|
11
10
|
end
|
|
@@ -20,7 +19,7 @@ module FastSerializer
|
|
|
20
19
|
# @param context [Hash]
|
|
21
20
|
# @return [Hash]
|
|
22
21
|
def serialize(resource, params, context)
|
|
23
|
-
return if resource
|
|
22
|
+
return if !resource
|
|
24
23
|
|
|
25
24
|
result = {}
|
|
26
25
|
|
|
@@ -7,13 +7,13 @@ module FastSerializer
|
|
|
7
7
|
|
|
8
8
|
# @param serializer [FastSerializer::Schema::Mixin]
|
|
9
9
|
# @param schema [FastSerializer::Schema]
|
|
10
|
-
def initialize(serializer
|
|
11
|
-
super
|
|
10
|
+
def initialize(key, method, serializer = nil, schema = nil, opts = {})
|
|
11
|
+
super(key, method, opts)
|
|
12
12
|
|
|
13
13
|
@serializer_klass = serializer
|
|
14
14
|
@schema = schema
|
|
15
15
|
|
|
16
|
-
if
|
|
16
|
+
if !@serializer_klass && !@schema
|
|
17
17
|
raise ArgumentError, 'must provide serializer or schema'
|
|
18
18
|
end
|
|
19
19
|
end
|
|
@@ -43,7 +43,7 @@ module FastSerializer
|
|
|
43
43
|
def attributes(*attribute_names)
|
|
44
44
|
attribute_names.each do |attribute_name|
|
|
45
45
|
serialization_schema.add_attribute(
|
|
46
|
-
JsonModel::Attribute.new(
|
|
46
|
+
JsonModel::Attribute.new(attribute_name, attribute_name)
|
|
47
47
|
)
|
|
48
48
|
end
|
|
49
49
|
end
|
|
@@ -57,12 +57,11 @@ module FastSerializer
|
|
|
57
57
|
# @param block [Proc] result is used as the attribute value
|
|
58
58
|
#
|
|
59
59
|
def attribute(attribute_name, opts = {}, &block)
|
|
60
|
+
method = (opts.is_a?(String) || opts.is_a?(Symbol)) ? opts : block
|
|
61
|
+
opts = opts.is_a?(Hash) ? opts : {}
|
|
62
|
+
|
|
60
63
|
serialization_schema.add_attribute(
|
|
61
|
-
JsonModel::Attribute.new(
|
|
62
|
-
key: attribute_name,
|
|
63
|
-
method: block,
|
|
64
|
-
opts: opts
|
|
65
|
-
)
|
|
64
|
+
JsonModel::Attribute.new(attribute_name, method, opts)
|
|
66
65
|
)
|
|
67
66
|
end
|
|
68
67
|
|
|
@@ -76,12 +75,14 @@ module FastSerializer
|
|
|
76
75
|
# @option opts [FastSerializer::Schema] :schema
|
|
77
76
|
#
|
|
78
77
|
def has_one(attribute_name, opts = {})
|
|
79
|
-
serialization_schema.add_attribute
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
78
|
+
serialization_schema.add_attribute(
|
|
79
|
+
JsonModel::HasOneRelationship.new(
|
|
80
|
+
opts.delete(:key) || attribute_name,
|
|
81
|
+
opts.delete(:method) || attribute_name,
|
|
82
|
+
opts.delete(:serializer),
|
|
83
|
+
opts.delete(:schema),
|
|
84
|
+
opts,
|
|
85
|
+
)
|
|
85
86
|
)
|
|
86
87
|
end
|
|
87
88
|
|
|
@@ -92,11 +93,11 @@ module FastSerializer
|
|
|
92
93
|
def has_many(attribute_name, opts = {})
|
|
93
94
|
serialization_schema.add_attribute(
|
|
94
95
|
JsonModel::HasManyRelationship.new(
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
opts:
|
|
98
|
-
|
|
99
|
-
|
|
96
|
+
opts.delete(:key) || attribute_name,
|
|
97
|
+
opts.delete(:method) || attribute_name,
|
|
98
|
+
opts.delete(:serializer),
|
|
99
|
+
opts.delete(:schema),
|
|
100
|
+
opts,
|
|
100
101
|
)
|
|
101
102
|
)
|
|
102
103
|
end
|
|
@@ -106,11 +107,11 @@ module FastSerializer
|
|
|
106
107
|
def list(attribute_name, opts = {})
|
|
107
108
|
serialization_schema.add_attribute(
|
|
108
109
|
JsonModel::Array.new(
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
opts:
|
|
112
|
-
|
|
113
|
-
|
|
110
|
+
attribute_name,
|
|
111
|
+
attribute_name,
|
|
112
|
+
opts.delete(:serializer),
|
|
113
|
+
opts.delete(:schema),
|
|
114
|
+
opts,
|
|
114
115
|
)
|
|
115
116
|
)
|
|
116
117
|
end
|
|
@@ -131,8 +132,10 @@ module FastSerializer
|
|
|
131
132
|
end
|
|
132
133
|
|
|
133
134
|
def serialize_resource(resource, params = {}, context = self)
|
|
134
|
-
Utils.ref_merge(self.params, params)
|
|
135
135
|
_params_dup = FastSerializer::Utils.symbolize_keys(self.params)
|
|
136
|
+
Utils.ref_merge(_params_dup, params)
|
|
137
|
+
self.params.delete(:meta)
|
|
138
|
+
|
|
136
139
|
meta = _params_dup.delete(:meta)
|
|
137
140
|
|
|
138
141
|
is_collection = if _params_dup.key?(:is_collection)
|
|
@@ -150,7 +153,7 @@ module FastSerializer
|
|
|
150
153
|
# need to bind context
|
|
151
154
|
resource.map { |entry| context.class.new(entry, _params_dup).serializable_hash }
|
|
152
155
|
else
|
|
153
|
-
JsonModel::Array.new(
|
|
156
|
+
JsonModel::Array.new(:base, :base, nil, serialization_schema).serialize(resource, _params_dup, context)
|
|
154
157
|
end
|
|
155
158
|
|
|
156
159
|
else
|
|
@@ -5,10 +5,12 @@ module FastSerializer
|
|
|
5
5
|
def self.symbolize_keys(hash)
|
|
6
6
|
res = {}
|
|
7
7
|
hash.each { |key, value| res[key.to_sym] = value }
|
|
8
|
-
|
|
8
|
+
res
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
def self.ref_merge(hash_a, hash_b)
|
|
12
|
+
return if hash_a.equal?(hash_b)
|
|
13
|
+
|
|
12
14
|
hash_b.each do |key, value|
|
|
13
15
|
hash_a[key] = value
|
|
14
16
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: fast_serializer_ruby
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.6.
|
|
4
|
+
version: 0.6.9
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Evgeny Stepanov
|
|
8
|
-
autorequire:
|
|
8
|
+
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2022-08-10 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: This library intends to solve such a typical and on the other hand important
|
|
14
14
|
problem as efficient ruby object to hash transformation.
|
|
@@ -50,7 +50,7 @@ metadata:
|
|
|
50
50
|
homepage_uri: https://github.com/estepnv/fast_serializer
|
|
51
51
|
source_code_uri: https://github.com/estepnv/fast_serializer
|
|
52
52
|
changelog_uri: https://github.com/estepnv/fast_serializer/CHANGELOG.md
|
|
53
|
-
post_install_message:
|
|
53
|
+
post_install_message:
|
|
54
54
|
rdoc_options: []
|
|
55
55
|
require_paths:
|
|
56
56
|
- lib
|
|
@@ -65,8 +65,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
65
65
|
- !ruby/object:Gem::Version
|
|
66
66
|
version: '0'
|
|
67
67
|
requirements: []
|
|
68
|
-
rubygems_version: 3.
|
|
69
|
-
signing_key:
|
|
68
|
+
rubygems_version: 3.3.6
|
|
69
|
+
signing_key:
|
|
70
70
|
specification_version: 4
|
|
71
71
|
summary: fast_serializer is a lightweight ruby objects serializer.
|
|
72
72
|
test_files: []
|