mutils 0.2.34 → 0.2.35
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/.gitignore +1 -0
- data/.rubocop_todo.yml +4 -0
- data/BENCHMARK-SERIALIZER-JSON.md +52 -52
- data/CHANGELOG.md +19 -3
- data/Gemfile.lock +1 -1
- data/README.md +11 -1
- data/lib/mutils/serialization/serialization_methods.rb +7 -5
- data/lib/mutils/serialization/serialization_results.rb +15 -1
- data/lib/mutils/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 29ea440032b0c3a5961c65970f1e4d9654b19f44d5549c62b178a3401799aae5
|
|
4
|
+
data.tar.gz: 99d62ab7cc81e88b58644b47ebe607372fb74df7c376adef70f180f9dec6b771
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '0278f224aad256658b8b862135e0afcd29ef05873e7b392bc8c8ca5af67e38b1bc31aaf9e500c3349b7674908e3f6821c16ddee8db5b845b867e84718069d35c'
|
|
7
|
+
data.tar.gz: 18ee155fbc162d27b78056468dc064d755ed2339fd65c0fe677a75cfea73043999ca7244ad28671e076f71e54252b278ab8add538b289b1b82c413934b8a6feb
|
data/.gitignore
CHANGED
data/.rubocop_todo.yml
CHANGED
|
@@ -2,81 +2,81 @@
|
|
|
2
2
|
ruby benchmark/benchmark-serializer-json.rb
|
|
3
3
|
|
|
4
4
|
Rehearsal ------------------------------------------------
|
|
5
|
-
fast_jsonapi 0.
|
|
6
|
-
|
|
7
|
-
as_json 0.
|
|
8
|
-
grape_entity 2.
|
|
9
|
-
blueprinter 0.
|
|
10
|
-
roar 1.
|
|
11
|
-
panko 0.
|
|
12
|
-
--------------------------------------- total: 6.
|
|
5
|
+
fast_jsonapi 0.469146 0.003692 0.472838 ( 0.482972)
|
|
6
|
+
==>mutils 0.343414 0.002338 0.345752 ( 0.350116)
|
|
7
|
+
as_json 0.349795 0.002601 0.352396 ( 0.355537)
|
|
8
|
+
grape_entity 2.123932 0.008031 2.131963 ( 2.142096)
|
|
9
|
+
blueprinter 0.729144 0.005669 0.734813 ( 0.737173)
|
|
10
|
+
roar 1.815007 0.015412 1.830419 ( 1.831867)
|
|
11
|
+
panko 0.324755 0.000970 0.325725 ( 0.325953)
|
|
12
|
+
--------------------------------------- total: 6.193906sec
|
|
13
13
|
|
|
14
14
|
user system total real
|
|
15
|
-
fast_jsonapi 0.
|
|
16
|
-
|
|
17
|
-
as_json 0.
|
|
18
|
-
grape_entity 1.
|
|
19
|
-
blueprinter 0.
|
|
20
|
-
roar 1.
|
|
21
|
-
panko 0.
|
|
15
|
+
fast_jsonapi 0.326813 0.000479 0.327292 ( 0.328071)
|
|
16
|
+
==>mutils 0.228319 0.000145 0.228464 ( 0.228649)
|
|
17
|
+
as_json 0.252139 0.000242 0.252381 ( 0.252570)
|
|
18
|
+
grape_entity 1.820288 0.008052 1.828340 ( 1.837977)
|
|
19
|
+
blueprinter 0.656481 0.001270 0.657751 ( 0.659753)
|
|
20
|
+
roar 1.592949 0.008122 1.601071 ( 1.604941)
|
|
21
|
+
panko 0.260401 0.000433 0.260834 ( 0.261211)
|
|
22
22
|
|
|
23
23
|
Warming up --------------------------------------
|
|
24
24
|
fast_jsonapi 1.000 i/100ms
|
|
25
|
-
|
|
25
|
+
==>mutils 1.000 i/100ms
|
|
26
26
|
as_json 1.000 i/100ms
|
|
27
27
|
grape_entity 1.000 i/100ms
|
|
28
28
|
blueprinter 1.000 i/100ms
|
|
29
29
|
roar 1.000 i/100ms
|
|
30
30
|
panko 1.000 i/100ms
|
|
31
31
|
Calculating -------------------------------------
|
|
32
|
-
fast_jsonapi 2.
|
|
33
|
-
|
|
34
|
-
as_json 3.
|
|
35
|
-
grape_entity 0.
|
|
36
|
-
blueprinter 1.
|
|
37
|
-
roar 0.
|
|
38
|
-
panko 3.
|
|
32
|
+
fast_jsonapi 2.215 (± 5.1%) i/s - 22.000 in 10.107915s
|
|
33
|
+
==>mutils 3.136 (± 3.0%) i/s - 32.000 in 10.281530s
|
|
34
|
+
as_json 3.455 (± 3.1%) i/s - 35.000 in 10.229037s
|
|
35
|
+
grape_entity 0.509 (± 7.2%) i/s - 6.000 in 11.844870s
|
|
36
|
+
blueprinter 1.337 (± 3.9%) i/s - 14.000 in 10.537541s
|
|
37
|
+
roar 0.613 (± 2.2%) i/s - 7.000 in 11.441661s
|
|
38
|
+
panko 3.527 (± 2.9%) i/s - 36.000 in 10.292679s
|
|
39
39
|
with 95.0% confidence
|
|
40
40
|
|
|
41
41
|
Comparison:
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
fast_jsonapi: 2.
|
|
46
|
-
blueprinter: 1.
|
|
47
|
-
roar: 0.6 i/s - 5.
|
|
48
|
-
grape_entity: 0.5 i/s -
|
|
42
|
+
panko: 3.5 i/s
|
|
43
|
+
as_json: 3.5 i/s - same-ish: difference falls within error
|
|
44
|
+
==>mutils: 3.1 i/s - 1.13x (± 0.05) slower
|
|
45
|
+
fast_jsonapi: 2.2 i/s - 1.59x (± 0.09) slower
|
|
46
|
+
blueprinter: 1.3 i/s - 2.64x (± 0.13) slower
|
|
47
|
+
roar: 0.6 i/s - 5.76x (± 0.21) slower
|
|
48
|
+
grape_entity: 0.5 i/s - 6.93x (± 0.53) slower
|
|
49
49
|
with 95.0% confidence
|
|
50
50
|
|
|
51
51
|
Calculating -------------------------------------
|
|
52
|
-
fast_jsonapi 62.
|
|
53
|
-
|
|
52
|
+
fast_jsonapi 62.946M memsize ( 0.000 retained)
|
|
53
|
+
981.039k objects ( 0.000 retained)
|
|
54
54
|
50.000 strings ( 0.000 retained)
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
as_json 98.
|
|
59
|
-
1.
|
|
55
|
+
==>mutils 65.112M memsize ( 0.000 retained)
|
|
56
|
+
721.156k objects ( 0.000 retained)
|
|
57
|
+
0.000 strings ( 0.000 retained)
|
|
58
|
+
as_json 98.148M memsize ( 0.000 retained)
|
|
59
|
+
1.683M objects ( 0.000 retained)
|
|
60
60
|
8.000 strings ( 0.000 retained)
|
|
61
|
-
grape_entity 245.
|
|
62
|
-
2.
|
|
61
|
+
grape_entity 245.776M memsize ( 0.000 retained)
|
|
62
|
+
2.524M objects ( 0.000 retained)
|
|
63
63
|
4.000 strings ( 0.000 retained)
|
|
64
|
-
blueprinter 71.
|
|
65
|
-
|
|
64
|
+
blueprinter 71.856M memsize ( 0.000 retained)
|
|
65
|
+
661.154k objects ( 0.000 retained)
|
|
66
66
|
0.000 strings ( 0.000 retained)
|
|
67
|
-
roar
|
|
68
|
-
1.
|
|
67
|
+
roar 221.022M memsize ( 0.000 retained)
|
|
68
|
+
1.823M objects ( 0.000 retained)
|
|
69
69
|
1.000 strings ( 0.000 retained)
|
|
70
|
-
panko 98.
|
|
71
|
-
1.
|
|
70
|
+
panko 98.153M memsize ( 0.000 retained)
|
|
71
|
+
1.683M objects ( 0.000 retained)
|
|
72
72
|
13.000 strings ( 0.000 retained)
|
|
73
73
|
|
|
74
74
|
Comparison:
|
|
75
|
-
fast_jsonapi:
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
as_json:
|
|
79
|
-
panko:
|
|
80
|
-
roar:
|
|
81
|
-
grape_entity:
|
|
75
|
+
fast_jsonapi: 62945864 allocated
|
|
76
|
+
=>>mutils: 65111936 allocated - 1.03x more
|
|
77
|
+
blueprinter: 71856304 allocated - 1.14x more
|
|
78
|
+
as_json: 98148488 allocated - 1.56x more
|
|
79
|
+
panko: 98153072 allocated - 1.56x more
|
|
80
|
+
roar: 221022344 allocated - 3.51x more
|
|
81
|
+
grape_entity: 245775576 allocated - 3.90x morre
|
|
82
82
|
```
|
data/CHANGELOG.md
CHANGED
|
@@ -1,11 +1,27 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [v0.2.34](https://github.com/niteshpurohit/mutils/tree/v0.2.34) (2020-03-29)
|
|
4
|
+
|
|
5
|
+
[Full Changelog](https://github.com/niteshpurohit/mutils/compare/v0.2.33...v0.2.34)
|
|
6
|
+
|
|
7
|
+
**Merged pull requests:**
|
|
8
|
+
|
|
9
|
+
- Feature/attribute can be optional [\#27](https://github.com/niteshpurohit/mutils/pull/27) ([niteshpurohit](https://github.com/niteshpurohit))
|
|
10
|
+
|
|
11
|
+
## [v0.2.33](https://github.com/niteshpurohit/mutils/tree/v0.2.33) (2020-03-29)
|
|
12
|
+
|
|
13
|
+
[Full Changelog](https://github.com/niteshpurohit/mutils/compare/v0.2.32...v0.2.33)
|
|
14
|
+
|
|
15
|
+
**Merged pull requests:**
|
|
16
|
+
|
|
17
|
+
- Added new custom\_method in serializer to control if include method [\#26](https://github.com/niteshpurohit/mutils/pull/26) ([niteshpurohit](https://github.com/niteshpurohit))
|
|
18
|
+
- Update CHANGELOG.md [\#25](https://github.com/niteshpurohit/mutils/pull/25) ([niteshpurohit](https://github.com/niteshpurohit))
|
|
19
|
+
- Create .codeclimate.yml [\#24](https://github.com/niteshpurohit/mutils/pull/24) ([niteshpurohit](https://github.com/niteshpurohit))
|
|
20
|
+
- changelog: v0.2.32 [\#22](https://github.com/niteshpurohit/mutils/pull/22) ([niteshpurohit](https://github.com/niteshpurohit))
|
|
21
|
+
|
|
3
22
|
## [v0.2.32](https://github.com/niteshpurohit/mutils/tree/v0.2.32) (2020-03-18)
|
|
4
23
|
|
|
5
24
|
[Full Changelog](https://github.com/niteshpurohit/mutils/compare/v0.2.31...v0.2.32)
|
|
6
|
-
**Improvement:**
|
|
7
|
-
- Less Memory Used
|
|
8
|
-
- Greator Performance
|
|
9
25
|
|
|
10
26
|
**Merged pull requests:**
|
|
11
27
|
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -64,6 +64,8 @@ Attributes are fields in the model itself. You can reference them by below examp
|
|
|
64
64
|
# User Serializer
|
|
65
65
|
class UserSerializer < Mutils::Serialization::BaseSerializer
|
|
66
66
|
attributes :id, :first_name, :last_name, :email
|
|
67
|
+
## OR
|
|
68
|
+
attribute :email, {always_include: true} ## this will allow to selectively include email
|
|
67
69
|
end
|
|
68
70
|
```
|
|
69
71
|
##### Custom Methods
|
|
@@ -76,7 +78,11 @@ Custom methods used in Serializer can be useful for cases as below.
|
|
|
76
78
|
# User Serializer
|
|
77
79
|
class UserSerializer < Mutils::Serialization::BaseSerializer
|
|
78
80
|
attributes :id, :first_name, :last_name, :email
|
|
81
|
+
###
|
|
79
82
|
custom_methods :full_name
|
|
83
|
+
## OR
|
|
84
|
+
custom_method :full_name, {always_include: true} ## this will allow to selectively include full_name
|
|
85
|
+
###
|
|
80
86
|
|
|
81
87
|
def full_name
|
|
82
88
|
"#{scope.first_name} #{scope.last_name}"
|
|
@@ -88,14 +94,18 @@ Relations such as `has_many`, `belongs_to`, `has_one` can be used as follows
|
|
|
88
94
|
1. Every relation must be provided with their own serializer
|
|
89
95
|
2. `always_include` option can be used to instruct `Serializer` to always include this relation
|
|
90
96
|
3. `always_include` by default is disabled, relations which are not `always_include` can be included while using the serializer. Refer to next section for this usage
|
|
91
|
-
|
|
97
|
+
4. `label` option can be used to override model class name while serializing
|
|
92
98
|
```ruby
|
|
93
99
|
# frozen_string_literal: true
|
|
94
100
|
|
|
95
101
|
# User Serializer
|
|
96
102
|
class UserSerializer < Mutils::Serialization::BaseSerializer
|
|
97
103
|
attributes :id, :first_name, :last_name, :email
|
|
104
|
+
|
|
98
105
|
belongs_to :company, serializer: CompanySerializer, always_include: true
|
|
106
|
+
##OR
|
|
107
|
+
belongs_to :company, serializer: CompanySerializer, always_include: true, label: 'organization' ##<== important to give singular name
|
|
108
|
+
|
|
99
109
|
has_many :comments, serializer: CommentSerializer
|
|
100
110
|
has_one :account, serializer: AccountSerializer
|
|
101
111
|
|
|
@@ -29,16 +29,17 @@ module Mutils
|
|
|
29
29
|
end
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
-
def attribute(method_name,
|
|
33
|
-
add_single_attribute(method_name,
|
|
32
|
+
def attribute(method_name, options = {})
|
|
33
|
+
add_single_attribute(method_name, options, 'attribute')
|
|
34
34
|
end
|
|
35
35
|
|
|
36
|
-
def custom_method(method_name,
|
|
37
|
-
add_single_attribute(method_name,
|
|
36
|
+
def custom_method(method_name, options = {})
|
|
37
|
+
add_single_attribute(method_name, options, 'method')
|
|
38
38
|
end
|
|
39
39
|
|
|
40
|
-
def add_single_attribute(method_name,
|
|
40
|
+
def add_single_attribute(method_name, options, type)
|
|
41
41
|
self.attributes_to_serialize = {} if attributes_to_serialize.nil?
|
|
42
|
+
always_include = options[:always_include].nil? ? false : options[:always_include]
|
|
42
43
|
value = { method: type == 'method', always_include: always_include }
|
|
43
44
|
attributes_to_serialize[method_name] = value
|
|
44
45
|
end
|
|
@@ -64,6 +65,7 @@ module Mutils
|
|
|
64
65
|
|
|
65
66
|
options[:serializer] = class_name.to_s.constantize
|
|
66
67
|
options[:option_name] = option_name
|
|
68
|
+
options[:label] = options[:label]
|
|
67
69
|
options
|
|
68
70
|
end
|
|
69
71
|
|
|
@@ -34,7 +34,17 @@ module Mutils
|
|
|
34
34
|
def hash_relationships(relationships_array)
|
|
35
35
|
relationships = relationships_array&.compact
|
|
36
36
|
hash = {}
|
|
37
|
-
relationships&.keys&.each
|
|
37
|
+
relationships&.keys&.each do |key|
|
|
38
|
+
object = scope.send(key)
|
|
39
|
+
label = relationships[key][:label]
|
|
40
|
+
name = key
|
|
41
|
+
if label
|
|
42
|
+
name = label.underscore
|
|
43
|
+
name = name.pluralize if collection? object
|
|
44
|
+
name = name.to_sym
|
|
45
|
+
end
|
|
46
|
+
check_if_included(relationships, key) && (hash[name] = relationships[key][:serializer].new(object).to_h)
|
|
47
|
+
end
|
|
38
48
|
hash
|
|
39
49
|
end
|
|
40
50
|
|
|
@@ -47,6 +57,10 @@ module Mutils
|
|
|
47
57
|
scope.respond_to?(:size) && !scope.respond_to?(:each_pair)
|
|
48
58
|
end
|
|
49
59
|
|
|
60
|
+
def collection?(object)
|
|
61
|
+
object.respond_to?(:size) && !object.respond_to?(:each_pair)
|
|
62
|
+
end
|
|
63
|
+
|
|
50
64
|
def class_name
|
|
51
65
|
if scope_is_collection?
|
|
52
66
|
format_class_name(scope[0]).pluralize
|
data/lib/mutils/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mutils
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.2.
|
|
4
|
+
version: 0.2.35
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Nitesh Purohit
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2020-03-
|
|
11
|
+
date: 2020-03-30 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activesupport
|
|
@@ -156,7 +156,7 @@ licenses:
|
|
|
156
156
|
- MIT
|
|
157
157
|
metadata:
|
|
158
158
|
bug_tracker_uri: https://github.com/niteshpurohit/mutils/issues
|
|
159
|
-
source_code_uri: https://github.com/niteshpurohit/mutils/tree/v0.2.
|
|
159
|
+
source_code_uri: https://github.com/niteshpurohit/mutils/tree/v0.2.35
|
|
160
160
|
post_install_message:
|
|
161
161
|
rdoc_options: []
|
|
162
162
|
require_paths:
|