mutils 0.2.34 → 0.2.35

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cc2bb3522e87ab43ab73fd33c293f037a362be1711c72e426302ad4895f77965
4
- data.tar.gz: 775f6b17dcf6599d9d9e5ed851a18c4d9db769abd80398835430a5e58dcc98e3
3
+ metadata.gz: 29ea440032b0c3a5961c65970f1e4d9654b19f44d5549c62b178a3401799aae5
4
+ data.tar.gz: 99d62ab7cc81e88b58644b47ebe607372fb74df7c376adef70f180f9dec6b771
5
5
  SHA512:
6
- metadata.gz: 6e1103228cb1d0fcf5236812825f240ba1df2bd8f7f8c25a8655906a2a5edab161c5d300b29d44163cdb47570f8272a0bbddcb1ca2e6cee1431d009d9bbf5b3b
7
- data.tar.gz: 1c0a90e7dd51811384aa2c8078c888950e6227a301f4283300846b3770fbe204ff5b115d02dfad6ae8b4ac2e134119758d640fd7bea7e32f2003d59f62e718e9
6
+ metadata.gz: '0278f224aad256658b8b862135e0afcd29ef05873e7b392bc8c8ca5af67e38b1bc31aaf9e500c3349b7674908e3f6821c16ddee8db5b845b867e84718069d35c'
7
+ data.tar.gz: 18ee155fbc162d27b78056468dc064d755ed2339fd65c0fe677a75cfea73043999ca7244ad28671e076f71e54252b278ab8add538b289b1b82c413934b8a6feb
data/.gitignore CHANGED
@@ -10,3 +10,4 @@
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
12
  .idea/*
13
+ *.gem
@@ -35,3 +35,7 @@ Style/RescueModifier:
35
35
  # URISchemes: http, https
36
36
  Metrics/LineLength:
37
37
  Max: 608
38
+ Metrics/MethodLength:
39
+ Max: 20
40
+ Metrics/AbcSize:
41
+ Max: 20
@@ -2,81 +2,81 @@
2
2
  ruby benchmark/benchmark-serializer-json.rb
3
3
 
4
4
  Rehearsal ------------------------------------------------
5
- fast_jsonapi 0.420896 0.001788 0.422684 ( 0.425161)
6
- => mutils 0.330089 0.000778 0.330867 ( 0.331820)
7
- as_json 0.328482 0.002571 0.331053 ( 0.333599)
8
- grape_entity 2.136319 0.012292 2.148611 ( 2.160330)
9
- blueprinter 0.809256 0.008755 0.818011 ( 0.826980)
10
- roar 1.905847 0.019966 1.925813 ( 1.935937)
11
- panko 0.323696 0.001208 0.324904 ( 0.325517)
12
- --------------------------------------- total: 6.301943sec
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.329029 0.000763 0.329792 ( 0.330612)
16
- => mutils 0.255709 0.000688 0.256397 ( 0.257546)
17
- as_json 0.259330 0.001016 0.260346 ( 0.261641)
18
- grape_entity 1.653116 0.004544 1.657660 ( 1.659194)
19
- blueprinter 0.692518 0.003879 0.696397 ( 0.700324)
20
- roar 1.578995 0.011059 1.590054 ( 1.598151)
21
- panko 0.270695 0.000999 0.271694 ( 0.272918)
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
- => mutils 1.000 i/100ms
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.5022.3%) i/s - 25.000 in 10.025595s
33
- => mutils 3.016 (± 3.0%) i/s - 31.000 in 10.322736s
34
- as_json 3.766 (± 3.2%) i/s - 38.000 in 10.197421s
35
- grape_entity 0.5334.8%) i/s - 6.000 in 11.273301s
36
- blueprinter 1.497 (± 3.8%) i/s - 15.000 in 10.095146s
37
- roar 0.631 (± 2.2%) i/s - 7.000 in 11.087625s
38
- panko 3.7093.0%) i/s - 37.000 in 10.073211s
32
+ fast_jsonapi 2.2155.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.5097.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.5272.9%) i/s - 36.000 in 10.292679s
39
39
  with 95.0% confidence
40
40
 
41
41
  Comparison:
42
- as_json: 3.8 i/s
43
- panko: 3.7 i/s - same-ish: difference falls within error
44
- => mutils: 3.0 i/s - 1.25x (± 0.05) slower
45
- fast_jsonapi: 2.5 i/s - 1.51x (± 0.06) slower
46
- blueprinter: 1.5 i/s - 2.52x (± 0.13) slower
47
- roar: 0.6 i/s - 5.96x (± 0.23) slower
48
- grape_entity: 0.5 i/s - 7.06x (± 0.40) slower
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.915M memsize ( 0.000 retained)
53
- 980.599k objects ( 0.000 retained)
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
- => mutils 76.280M memsize ( 0.000 retained)
56
- 1.001M objects ( 0.000 retained)
57
- 6.000 strings ( 0.000 retained)
58
- as_json 98.069M memsize ( 0.000 retained)
59
- 1.682M objects ( 0.000 retained)
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.598M memsize ( 0.000 retained)
62
- 2.522M objects ( 0.000 retained)
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.800M memsize ( 0.000 retained)
65
- 660.626k objects ( 0.000 retained)
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 220.796M memsize ( 0.000 retained)
68
- 1.822M objects ( 0.000 retained)
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.073M memsize ( 0.000 retained)
71
- 1.682M objects ( 0.000 retained)
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: 62914944 allocated
76
- blueprinter: 71800368 allocated - 1.14x more
77
- => mutils: 76279744 allocated - 1.21x more
78
- as_json: 98068616 allocated - 1.56x more
79
- panko: 98073200 allocated - 1.56x more
80
- roar: 220795848 allocated - 3.51x more
81
- grape_entity: 245598232 allocated - 3.90x more
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
  ```
@@ -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
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mutils (0.2.34)
4
+ mutils (0.2.35)
5
5
  activesupport (>= 4.2)
6
6
 
7
7
  GEM
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, always_include = false)
33
- add_single_attribute(method_name, always_include, 'attribute')
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, always_include = false)
37
- add_single_attribute(method_name, always_include, 'method')
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, always_include, type)
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 { |key| check_if_included(relationships, key) && (hash[key] = relationships[key][:serializer].new(scope.send(key)).to_h) }
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mutils
4
- VERSION = '0.2.34'
4
+ VERSION = '0.2.35'
5
5
  end
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.34
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-29 00:00:00.000000000 Z
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.34
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: