fast_serializer_ruby 0.6.5 → 0.6.7
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 +24 -23
- 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: a8f0ab89ab3ce2f693f4c850057ecbf4a0651dfb82c7de1fccd2142c5788070a
|
4
|
+
data.tar.gz: d767dcb5b4e8b21c8ca1b558fed4860cca7ce7062d94dd32b76d57deb16d7263
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0555c28854299014c62e812d3e4c2f9c124ed3d016994202224eea9d8e29188c8c8f7a3f18371f04c892b4dabca2130269fd3212ada2056942c7856d9bbbb042
|
7
|
+
data.tar.gz: bf17f9ff6be9eae047392c49d567d9ab246fa088eeeba2c9b90688fc206646b992bb075d6f6024bd73a5e367533d58738bdfc797adf81d9bb14bb0a6eb65f716
|
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
|
@@ -150,7 +151,7 @@ module FastSerializer
|
|
150
151
|
# need to bind context
|
151
152
|
resource.map { |entry| context.class.new(entry, _params_dup).serializable_hash }
|
152
153
|
else
|
153
|
-
JsonModel::Array.new(
|
154
|
+
JsonModel::Array.new(:base, :base, nil, serialization_schema).serialize(resource, _params_dup, context)
|
154
155
|
end
|
155
156
|
|
156
157
|
else
|
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.7
|
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: 2021-11-
|
11
|
+
date: 2021-11-14 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.1.6
|
69
|
+
signing_key:
|
70
70
|
specification_version: 4
|
71
71
|
summary: fast_serializer is a lightweight ruby objects serializer.
|
72
72
|
test_files: []
|