mutils 0.2.25 → 0.2.27
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/.mergify.yml +8 -0
- data/.rubocop.yml +11 -1
- data/.rubocop_todo.yml +37 -0
- data/.travis.yml +7 -0
- data/CHANGELOG.md +26 -0
- data/Gemfile +3 -1
- data/Gemfile.lock +29 -1
- data/Makefile +2 -0
- data/README.md +32 -6
- data/Rakefile +5 -3
- data/benchmark/benchmark-serializer-json.rb +38 -36
- data/bin/console +4 -3
- data/lib/generators/serializer/serializer_generator.rb +4 -4
- data/lib/mutils/serialization/base_serializer.rb +22 -5
- data/lib/mutils/serialization/serialization_includes.rb +3 -1
- data/lib/mutils/serialization/serialization_methods.rb +25 -21
- data/lib/mutils/serialization/serialization_results.rb +18 -2
- data/lib/mutils/version.rb +3 -1
- data/lib/mutils.rb +4 -0
- data/mutils.gemspec +10 -5
- metadata +53 -8
- data/.github/workflows/ruby.yml +0 -20
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6c381da1faa5f3165f54a17cddd246bfb5bfffa7ec9114766181556e529ffb4d
|
|
4
|
+
data.tar.gz: 27e8bdebcf4be2fc4cbb20771e44946df652447be534c19e68805525ad813356
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2a22ff71a77c8eaad11f69d9d7d9e1abd46e2727e8bd59576ac6bc5eabba3d32b246f5a6b54aaa4a3fb9aea3d33e3eec323c2df9d58fb92a4550d3f230c93244
|
|
7
|
+
data.tar.gz: 20d74b6f9062c88abe29ed3570acc09ae56bdcdffeb2499d85876b31a9a8a9a3076f4073eb7106a2d3ad58f088903f43cf99b232b957b49fa50059322f1b7fcd
|
data/.mergify.yml
ADDED
data/.rubocop.yml
CHANGED
|
@@ -1,2 +1,12 @@
|
|
|
1
|
+
inherit_from: .rubocop_todo.yml
|
|
2
|
+
|
|
1
3
|
AllCops:
|
|
2
|
-
TargetRubyVersion: "2.5.0"
|
|
4
|
+
TargetRubyVersion: "2.5.0"
|
|
5
|
+
Naming/FileName:
|
|
6
|
+
Exclude:
|
|
7
|
+
- 'benchmark/benchmark-serializer-json.rb'
|
|
8
|
+
Style/Documentation:
|
|
9
|
+
Exclude:
|
|
10
|
+
- 'spec/**/*'
|
|
11
|
+
- 'test/**/*'
|
|
12
|
+
- 'benchmark/benchmark-serializer-json.rb'
|
data/.rubocop_todo.yml
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# This configuration was generated by
|
|
2
|
+
# `rubocop --auto-gen-config`
|
|
3
|
+
# on 2019-11-25 04:01:32 +0800 using RuboCop version 0.76.0.
|
|
4
|
+
# The point is for the user to remove these configuration records
|
|
5
|
+
# one by one as the offenses are removed from the code base.
|
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
|
8
|
+
|
|
9
|
+
# Offense count: 2
|
|
10
|
+
# Configuration parameters: CountComments, ExcludedMethods.
|
|
11
|
+
# ExcludedMethods: refine
|
|
12
|
+
Metrics/BlockLength:
|
|
13
|
+
Max: 42
|
|
14
|
+
|
|
15
|
+
# Offense count: 1
|
|
16
|
+
# Configuration parameters: NamePrefix, NamePrefixBlacklist, NameWhitelist, MethodDefinitionMacros.
|
|
17
|
+
# NamePrefix: is_, has_, have_
|
|
18
|
+
# NamePrefixBlacklist: is_, has_, have_
|
|
19
|
+
# NameWhitelist: is_a?
|
|
20
|
+
# MethodDefinitionMacros: define_method, define_singleton_method
|
|
21
|
+
Naming/PredicateName:
|
|
22
|
+
Exclude:
|
|
23
|
+
- 'spec/**/*'
|
|
24
|
+
- 'lib/mutils/serialization/serialization_methods.rb'
|
|
25
|
+
|
|
26
|
+
# Offense count: 1
|
|
27
|
+
# Cop supports --auto-correct.
|
|
28
|
+
Style/RescueModifier:
|
|
29
|
+
Exclude:
|
|
30
|
+
- 'lib/mutils/serialization/serialization_methods.rb'
|
|
31
|
+
|
|
32
|
+
# Offense count: 17
|
|
33
|
+
# Cop supports --auto-correct.
|
|
34
|
+
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
|
|
35
|
+
# URISchemes: http, https
|
|
36
|
+
Metrics/LineLength:
|
|
37
|
+
Max: 608
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
## [v0.2.27](https://github.com/niteshpurohit/mutils/tree/v0.2.27) (2019-11-25)
|
|
3
|
+
|
|
4
|
+
[Full Changelog](https://github.com/niteshpurohit/mutils/compare/v0.2.26...v0.2.27)
|
|
5
|
+
**Changes:**
|
|
6
|
+
- Code smells fixed.
|
|
7
|
+
|
|
8
|
+
## [v0.2.26](https://github.com/niteshpurohit/mutils/tree/v0.2.26) (2019-11-24)
|
|
9
|
+
|
|
10
|
+
[Full Changelog](https://github.com/niteshpurohit/mutils/compare/v0.2.25...v0.2.26)
|
|
11
|
+
|
|
12
|
+
**Changes:**
|
|
13
|
+
- Added xml serialization.
|
|
14
|
+
- rubocop offenses handled remaining added to todo.
|
|
15
|
+
- root added for json, now
|
|
16
|
+
- name_tag option added for Serializers to have custom name while serializing to json or xml
|
|
17
|
+
- added option name_tag, true for json serializing to generate json with root
|
|
18
|
+
|
|
19
|
+
## [v0.2.25](https://github.com/niteshpurohit/mutils/tree/v0.2.25) (2019-11-24)
|
|
20
|
+
|
|
21
|
+
[Full Changelog](https://github.com/niteshpurohit/mutils/compare/885f96959dd3701ac20ed0981b920be753c00db0...v0.2.25)
|
|
22
|
+
|
|
23
|
+
**Merged pull requests:**
|
|
24
|
+
|
|
25
|
+
- Feature/improve concurrency [\#4](https://github.com/niteshpurohit/mutils/pull/4) ([niteshpurohit](https://github.com/niteshpurohit))
|
|
26
|
+
- Testing Deploy [\#3](https://github.com/niteshpurohit/mutils/pull/3) ([niteshpurohit](https://github.com/niteshpurohit))
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
mutils (0.2.
|
|
4
|
+
mutils (0.2.27)
|
|
5
5
|
activesupport (>= 4.2)
|
|
6
6
|
|
|
7
7
|
GEM
|
|
@@ -28,19 +28,31 @@ GEM
|
|
|
28
28
|
zeitwerk (~> 2.2)
|
|
29
29
|
builder (3.2.3)
|
|
30
30
|
concurrent-ruby (1.1.5)
|
|
31
|
+
coveralls (0.8.23)
|
|
32
|
+
json (>= 1.8, < 3)
|
|
33
|
+
simplecov (~> 0.16.1)
|
|
34
|
+
term-ansicolor (~> 1.3)
|
|
35
|
+
thor (>= 0.19.4, < 2.0)
|
|
36
|
+
tins (~> 1.6)
|
|
31
37
|
crass (1.0.5)
|
|
32
38
|
diff-lcs (1.3)
|
|
39
|
+
diffy (3.3.0)
|
|
40
|
+
docile (1.3.2)
|
|
33
41
|
erubi (1.9.0)
|
|
34
42
|
i18n (1.7.0)
|
|
35
43
|
concurrent-ruby (~> 1.0)
|
|
44
|
+
json (2.2.0)
|
|
36
45
|
loofah (2.3.1)
|
|
37
46
|
crass (~> 1.0.2)
|
|
38
47
|
nokogiri (>= 1.5.9)
|
|
39
48
|
method_source (0.9.2)
|
|
40
49
|
mini_portile2 (2.4.0)
|
|
41
50
|
minitest (5.13.0)
|
|
51
|
+
naws_xml (0.1.1.pre)
|
|
52
|
+
ox (~> 2)
|
|
42
53
|
nokogiri (1.10.5)
|
|
43
54
|
mini_portile2 (~> 2.4.0)
|
|
55
|
+
ox (2.11.0)
|
|
44
56
|
rack (2.0.7)
|
|
45
57
|
rack-test (1.1.0)
|
|
46
58
|
rack (>= 1.0, < 3)
|
|
@@ -61,6 +73,7 @@ GEM
|
|
|
61
73
|
rspec-expectations (3.9.0)
|
|
62
74
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
63
75
|
rspec-support (~> 3.9.0)
|
|
76
|
+
rspec-json_expectations (2.2.0)
|
|
64
77
|
rspec-mocks (3.9.0)
|
|
65
78
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
66
79
|
rspec-support (~> 3.9.0)
|
|
@@ -73,8 +86,20 @@ GEM
|
|
|
73
86
|
rspec-mocks (~> 3.9.0)
|
|
74
87
|
rspec-support (~> 3.9.0)
|
|
75
88
|
rspec-support (3.9.0)
|
|
89
|
+
rspec-xml_helpers (1.0.5)
|
|
90
|
+
diff-lcs (~> 1.3)
|
|
91
|
+
diffy (~> 3.2)
|
|
92
|
+
naws_xml (= 0.1.1.pre)
|
|
93
|
+
simplecov (0.16.1)
|
|
94
|
+
docile (~> 1.1)
|
|
95
|
+
json (>= 1.8, < 3)
|
|
96
|
+
simplecov-html (~> 0.10.0)
|
|
97
|
+
simplecov-html (0.10.2)
|
|
98
|
+
term-ansicolor (1.7.1)
|
|
99
|
+
tins (~> 1.0)
|
|
76
100
|
thor (0.20.3)
|
|
77
101
|
thread_safe (0.3.6)
|
|
102
|
+
tins (1.22.2)
|
|
78
103
|
tzinfo (1.2.5)
|
|
79
104
|
thread_safe (~> 0.1)
|
|
80
105
|
zeitwerk (2.2.1)
|
|
@@ -84,9 +109,12 @@ PLATFORMS
|
|
|
84
109
|
|
|
85
110
|
DEPENDENCIES
|
|
86
111
|
bundler (~> 2.0)
|
|
112
|
+
coveralls
|
|
87
113
|
mutils!
|
|
88
114
|
rake (~> 10.0)
|
|
115
|
+
rspec-json_expectations
|
|
89
116
|
rspec-rails
|
|
117
|
+
rspec-xml_helpers
|
|
90
118
|
|
|
91
119
|
BUNDLED WITH
|
|
92
120
|
2.0.2
|
data/Makefile
CHANGED
data/README.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
[](https://codeclimate.com/github/niteshpurohit/mutils/maintainability)
|
|
2
|
+

|
|
2
3
|
[](https://badge.fury.io/rb/mutils)
|
|
3
|
-
|
|
4
|
+
[](https://coveralls.io/github/niteshpurohit/mutils?branch=feature/coverall)
|
|
5
|
+
[](https://travis-ci.com/niteshpurohit/mutils)
|
|
4
6
|
# Mutils
|
|
5
7
|
## Introduction
|
|
6
8
|
`mutils` is collection of useful modules for `ruby on rails` which is tested and benchmarked against high load.
|
|
@@ -23,9 +25,9 @@ Or install it yourself as:
|
|
|
23
25
|
## Modules
|
|
24
26
|
| Sno | Name | Status |
|
|
25
27
|
|:---: |:-----------------: |:------: |
|
|
26
|
-
| 1 | Serializer - JSON |
|
|
27
|
-
| 2 | Serializer - XML |
|
|
28
|
-
|
|
28
|
+
| 1 | Serializer - JSON | Done |
|
|
29
|
+
| 2 | Serializer - XML | Done |
|
|
30
|
+
|
|
29
31
|
## Usage
|
|
30
32
|
### Serializer - JSON
|
|
31
33
|
JSON Serializer for Active Models
|
|
@@ -52,6 +54,7 @@ end
|
|
|
52
54
|
1. Attributes
|
|
53
55
|
2. Custom Methods
|
|
54
56
|
3. Relations
|
|
57
|
+
3. name_tag
|
|
55
58
|
|
|
56
59
|
##### Attributes
|
|
57
60
|
Attributes are fields in the model itself. You can reference them by below example
|
|
@@ -101,7 +104,30 @@ class UserSerializer < Mutils::Serialization::BaseSerializer
|
|
|
101
104
|
end
|
|
102
105
|
end
|
|
103
106
|
```
|
|
104
|
-
|
|
107
|
+
##### name_tag
|
|
108
|
+
name_tag is used to provide custom name to serializer output keys for json or tags for xml
|
|
109
|
+
|
|
110
|
+
**Options**
|
|
111
|
+
- ``name_tag 'Person', true`` # Include Person or People in JSON serialization as root, true|false this only implies to root serializer
|
|
112
|
+
- ``name_tag 'Person', false`` # not Include Person or People in JSON serialization as root, true|false this only implies to root serializer
|
|
113
|
+
- ``name_tag 'Person'`` # same as ``name_tag 'Person', false``
|
|
114
|
+
- without name_tag, actual class name of scope object inside serializer will be used
|
|
115
|
+
```ruby
|
|
116
|
+
# frozen_string_literal: true
|
|
117
|
+
|
|
118
|
+
# User Serializer
|
|
119
|
+
class UserSerializer < Mutils::Serialization::BaseSerializer
|
|
120
|
+
name_tag 'Person', true
|
|
121
|
+
attributes :id, :first_name, :last_name, :email
|
|
122
|
+
custom_methods :full_name
|
|
123
|
+
|
|
124
|
+
def full_name
|
|
125
|
+
"#{scope.first_name} #{scope.last_name}"
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
#### Usage: Use anywhere by
|
|
105
131
|
|
|
106
132
|
```ruby
|
|
107
133
|
user = User.first
|
data/Rakefile
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'bundler/inline'
|
|
2
4
|
|
|
3
5
|
require_relative '../lib//mutils'
|
|
@@ -180,41 +182,41 @@ labels = Array.new(4) { |i| Label.new(i, "Label #{i}", 'ffffff') }
|
|
|
180
182
|
issues = Array.new(10_000) { |i| Issue.new(i, i, "Issue #{i}", users.sample, labels.sample(rand(2..4))) }
|
|
181
183
|
|
|
182
184
|
serializers = [
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
185
|
+
{
|
|
186
|
+
name: :as_json,
|
|
187
|
+
serializer: -> { issues.as_json },
|
|
188
|
+
output_inspector: ->(output) { output.first }
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
name: :fast_jsonapi,
|
|
192
|
+
serializer: -> { FastJsonApi::IssueSerializer.new(issues, include: %i[labels user]).serializable_hash },
|
|
193
|
+
output_inspector: ->(output) { output[:data].first }
|
|
194
|
+
},
|
|
195
|
+
{
|
|
196
|
+
name: :grape_entity,
|
|
197
|
+
serializer: -> { GrapeEntity::Issue.represent(issues).as_json },
|
|
198
|
+
output_inspector: ->(output) { output.first }
|
|
199
|
+
},
|
|
200
|
+
{
|
|
201
|
+
name: :blueprinter,
|
|
202
|
+
serializer: -> { BluePrint::Issue.render_as_hash(issues) },
|
|
203
|
+
output_inspector: ->(output) { output.first }
|
|
204
|
+
},
|
|
205
|
+
{
|
|
206
|
+
name: :mutils,
|
|
207
|
+
serializer: -> { Mutils::Issue.new(issues).as_json },
|
|
208
|
+
output_inspector: ->(output) { output.first }
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
name: :roar,
|
|
212
|
+
serializer: -> { ROAR::IssueRepresenter.for_collection.new(issues).as_json },
|
|
213
|
+
output_inspector: ->(output) { output.first }
|
|
214
|
+
},
|
|
215
|
+
{
|
|
216
|
+
name: :panko,
|
|
217
|
+
serializer: -> { Panko::ArraySerializer.new(issues, each_serializer: PANKO::IssueSerializer).as_json },
|
|
218
|
+
output_inspector: ->(output) { output['subjects'].first }
|
|
219
|
+
}
|
|
218
220
|
]
|
|
219
221
|
|
|
220
222
|
# Display output
|
|
@@ -245,4 +247,4 @@ end
|
|
|
245
247
|
|
|
246
248
|
b.compare!
|
|
247
249
|
end
|
|
248
|
-
end
|
|
250
|
+
end
|
data/bin/console
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
2
3
|
|
|
3
|
-
require
|
|
4
|
-
require
|
|
4
|
+
require 'bundler/setup'
|
|
5
|
+
require 'mutils'
|
|
5
6
|
|
|
6
7
|
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
8
|
# with your gem easier. You can also use a different console, if you like.
|
|
@@ -10,5 +11,5 @@ require "mutils"
|
|
|
10
11
|
# require "pry"
|
|
11
12
|
# Pry.start
|
|
12
13
|
|
|
13
|
-
require
|
|
14
|
+
require 'irb'
|
|
14
15
|
IRB.start(__FILE__)
|
|
@@ -2,18 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
require 'rails/generators/base'
|
|
4
4
|
|
|
5
|
+
# Class SerializerGenerator
|
|
5
6
|
class SerializerGenerator < Rails::Generators::NamedBase
|
|
6
7
|
source_root File.expand_path('templates', __dir__)
|
|
7
8
|
|
|
8
9
|
argument :attributes, type: :array, default: [], banner: 'field field'
|
|
9
|
-
|
|
10
10
|
def create_serializer_file
|
|
11
11
|
template 'serializer.rb.tt', File.join('app', 'serializers', class_path, "#{file_name}_serializer.rb")
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
private
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
def attributes_names
|
|
17
|
+
attributes.map { |attribute| attribute.name.to_sym.inspect }
|
|
18
|
+
end
|
|
19
19
|
end
|
|
@@ -1,23 +1,28 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
|
+
|
|
2
3
|
require 'json/ext'
|
|
3
4
|
# module Mutils
|
|
4
5
|
module Mutils
|
|
5
6
|
module Serialization
|
|
6
|
-
# BaseSerializer
|
|
7
|
+
# BaseSerializer: inherit this class to get Serializer functionality
|
|
7
8
|
class BaseSerializer
|
|
8
9
|
include Mutils::Serialization::SerializationIncludes
|
|
9
10
|
include Mutils::Serialization::SerializationMethods
|
|
10
11
|
include Mutils::Serialization::SerializationResults
|
|
11
|
-
attr_accessor :scope, :options, :mutex
|
|
12
12
|
|
|
13
13
|
def initialize(object, options = {})
|
|
14
|
+
options[:child] = false unless options[:child]
|
|
14
15
|
self.scope = object
|
|
15
16
|
self.options = options
|
|
16
17
|
self.mutex = Mutex.new
|
|
17
18
|
end
|
|
18
19
|
|
|
19
20
|
def as_json(_options = {})
|
|
20
|
-
|
|
21
|
+
if options[:child] || !self.class.include_root
|
|
22
|
+
to_h
|
|
23
|
+
else
|
|
24
|
+
{ class_name => to_h }
|
|
25
|
+
end
|
|
21
26
|
end
|
|
22
27
|
|
|
23
28
|
def to_h
|
|
@@ -25,8 +30,20 @@ module Mutils
|
|
|
25
30
|
end
|
|
26
31
|
|
|
27
32
|
def to_json(_options = {})
|
|
28
|
-
JSON.generate(
|
|
33
|
+
JSON.generate(as_json, options)
|
|
29
34
|
end
|
|
35
|
+
|
|
36
|
+
def to_xml(_options = {})
|
|
37
|
+
to_h.to_xml(root: class_name, skip_instruct: true, indent: 2)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def as_xml(_options = {})
|
|
41
|
+
to_xml
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
attr_accessor :scope, :options, :mutex
|
|
30
47
|
end
|
|
31
48
|
end
|
|
32
|
-
end
|
|
49
|
+
end
|
|
@@ -10,6 +10,8 @@ module Mutils
|
|
|
10
10
|
class << self
|
|
11
11
|
attr_accessor :method_to_serialize,
|
|
12
12
|
:attributes_to_serialize,
|
|
13
|
+
:serializer_name,
|
|
14
|
+
:include_root,
|
|
13
15
|
:array_index,
|
|
14
16
|
:belongs_to_relationships,
|
|
15
17
|
:has_many_relationships
|
|
@@ -17,4 +19,4 @@ module Mutils
|
|
|
17
19
|
end
|
|
18
20
|
end
|
|
19
21
|
end
|
|
20
|
-
end
|
|
22
|
+
end
|
|
@@ -6,7 +6,13 @@ module Mutils
|
|
|
6
6
|
# Module SerializationCore
|
|
7
7
|
module SerializationMethods
|
|
8
8
|
extend ActiveSupport::Concern
|
|
9
|
+
# Module ClassMethods
|
|
9
10
|
module ClassMethods
|
|
11
|
+
def name_tag(name_tag, root = nil)
|
|
12
|
+
self.serializer_name = name_tag
|
|
13
|
+
self.include_root = root
|
|
14
|
+
end
|
|
15
|
+
|
|
10
16
|
def attributes(*attributes_list)
|
|
11
17
|
self.attributes_to_serialize = {} if attributes_to_serialize.nil?
|
|
12
18
|
attributes_list&.each { |attr| attributes_to_serialize[attr] = attr }
|
|
@@ -17,40 +23,38 @@ module Mutils
|
|
|
17
23
|
method_list&.each { |attr| method_to_serialize[attr] = attr }
|
|
18
24
|
end
|
|
19
25
|
|
|
20
|
-
def belongs_to(relationship_name, options = {})
|
|
21
|
-
|
|
22
|
-
raise "Serializer is Required for belongs_to :#{relationship_name}." \
|
|
23
|
-
"\nDefine it like:\nbelongs_to :#{relationship_name}, " \
|
|
24
|
-
'serializer: SERIALIZER_CLASS'
|
|
25
|
-
end
|
|
26
|
-
unless class_exists? options[:serializer]
|
|
27
|
-
raise 'Serializer class not defined'
|
|
28
|
-
end
|
|
29
|
-
|
|
26
|
+
def belongs_to(relationship_name, options = {}, option_name = 'belongs_to')
|
|
27
|
+
options = prepare_options(relationship_name, options, option_name)
|
|
30
28
|
self.belongs_to_relationships = {} if belongs_to_relationships.nil?
|
|
31
29
|
belongs_to_relationships[relationship_name] = options
|
|
32
30
|
end
|
|
33
31
|
|
|
34
32
|
alias has_one belongs_to
|
|
35
33
|
|
|
36
|
-
def has_many(relationship_name, options = {})
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
34
|
+
def has_many(relationship_name, options = {}, option_name = 'has_many')
|
|
35
|
+
options = prepare_options(relationship_name, options, option_name)
|
|
36
|
+
self.has_many_relationships = {} if has_many_relationships.nil?
|
|
37
|
+
has_many_relationships[relationship_name] = options
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def prepare_options(relationship_name, options, option_name)
|
|
41
|
+
class_name = options[:serializer]
|
|
42
|
+
if class_name.nil?
|
|
43
|
+
raise "Serializer is Required for belongs_to :#{relationship_name}." \
|
|
44
|
+
"\nDefine it like:\n#{option_name} :#{relationship_name}, " \
|
|
40
45
|
'serializer: SERIALIZER_CLASS'
|
|
41
46
|
end
|
|
42
|
-
unless class_exists?
|
|
43
|
-
raise 'Serializer class not defined'
|
|
44
|
-
end
|
|
47
|
+
raise "Serializer class not defined for relationship: #{relationship_name}" unless class_exists? class_name
|
|
45
48
|
|
|
46
|
-
|
|
47
|
-
|
|
49
|
+
options[:serializer] = class_name.to_s.constantize
|
|
50
|
+
options
|
|
48
51
|
end
|
|
49
52
|
|
|
50
53
|
def class_exists?(class_name)
|
|
51
|
-
|
|
54
|
+
klass = class_name.to_s.constantize rescue nil
|
|
55
|
+
klass && defined?(klass) && klass.is_a?(Class)
|
|
52
56
|
end
|
|
53
57
|
end
|
|
54
58
|
end
|
|
55
59
|
end
|
|
56
|
-
end
|
|
60
|
+
end
|
|
@@ -5,10 +5,10 @@ module Mutils
|
|
|
5
5
|
module Serialization
|
|
6
6
|
# Module SerializationResults
|
|
7
7
|
module SerializationResults
|
|
8
|
-
|
|
9
8
|
def generate_hash
|
|
10
9
|
if scope
|
|
11
10
|
if scope_is_collection?
|
|
11
|
+
options[:child] = true
|
|
12
12
|
scope.map { |inner_scope| self.class.new(inner_scope, options).generate_hash }
|
|
13
13
|
else
|
|
14
14
|
hashed_result
|
|
@@ -80,6 +80,22 @@ module Mutils
|
|
|
80
80
|
def scope_is_collection?
|
|
81
81
|
scope.respond_to?(:size) && !scope.respond_to?(:each_pair)
|
|
82
82
|
end
|
|
83
|
+
|
|
84
|
+
def class_name
|
|
85
|
+
if scope_is_collection?
|
|
86
|
+
format_class_name(scope[0]).pluralize
|
|
87
|
+
else
|
|
88
|
+
format_class_name(scope)
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def format_class_name(object)
|
|
93
|
+
if self.class.serializer_name&.length&.positive?
|
|
94
|
+
self.class.serializer_name
|
|
95
|
+
else
|
|
96
|
+
object.class.to_s.downcase
|
|
97
|
+
end
|
|
98
|
+
end
|
|
83
99
|
end
|
|
84
100
|
end
|
|
85
|
-
end
|
|
101
|
+
end
|
data/lib/mutils/version.rb
CHANGED
data/lib/mutils.rb
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require_relative 'mutils/version'
|
|
2
4
|
require 'active_support/concern'
|
|
5
|
+
require 'active_support/inflector'
|
|
6
|
+
require 'active_support/core_ext/hash'
|
|
3
7
|
require_relative 'mutils/serialization/serialization_results'
|
|
4
8
|
require_relative 'mutils/serialization/serialization_includes'
|
|
5
9
|
require_relative 'mutils/serialization/serialization_methods'
|
data/mutils.gemspec
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
|
2
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
3
5
|
require 'mutils/version'
|
|
4
6
|
|
|
@@ -12,7 +14,7 @@ Gem::Specification.new do |spec|
|
|
|
12
14
|
spec.description = 'mutils Utilities for rails app'
|
|
13
15
|
spec.homepage = 'https://github.com/niteshpurohit/mutils'
|
|
14
16
|
spec.license = 'MIT'
|
|
15
|
-
spec.files = Dir.chdir(File.expand_path(
|
|
17
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
16
18
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
17
19
|
end
|
|
18
20
|
spec.bindir = 'exe'
|
|
@@ -20,13 +22,16 @@ Gem::Specification.new do |spec|
|
|
|
20
22
|
spec.require_paths = ['lib']
|
|
21
23
|
spec.required_ruby_version = '>= 2.5.0'
|
|
22
24
|
spec.required_rubygems_version = '>= 1.8.11'
|
|
25
|
+
spec.add_runtime_dependency('activesupport', ['>= 4.2'])
|
|
23
26
|
spec.add_development_dependency 'bundler', '~> 2.0'
|
|
27
|
+
spec.add_development_dependency 'coveralls'
|
|
24
28
|
spec.add_development_dependency 'rake', '~> 10.0'
|
|
25
|
-
spec.
|
|
29
|
+
spec.add_development_dependency 'rspec-json_expectations'
|
|
26
30
|
spec.add_development_dependency 'rspec-rails'
|
|
31
|
+
spec.add_development_dependency 'rspec-xml_helpers'
|
|
27
32
|
|
|
28
33
|
spec.metadata = {
|
|
29
|
-
|
|
30
|
-
|
|
34
|
+
'bug_tracker_uri' => 'https://github.com/niteshpurohit/mutils/issues',
|
|
35
|
+
'source_code_uri' => "https://github.com/niteshpurohit/mutils/tree/v#{Mutils::VERSION}"
|
|
31
36
|
}
|
|
32
37
|
end
|
metadata
CHANGED
|
@@ -1,15 +1,29 @@
|
|
|
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.27
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Nitesh Purohit
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2019-11-
|
|
11
|
+
date: 2019-11-25 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: activesupport
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '4.2'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '4.2'
|
|
13
27
|
- !ruby/object:Gem::Dependency
|
|
14
28
|
name: bundler
|
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -24,6 +38,20 @@ dependencies:
|
|
|
24
38
|
- - "~>"
|
|
25
39
|
- !ruby/object:Gem::Version
|
|
26
40
|
version: '2.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: coveralls
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
27
55
|
- !ruby/object:Gem::Dependency
|
|
28
56
|
name: rake
|
|
29
57
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -39,19 +67,19 @@ dependencies:
|
|
|
39
67
|
- !ruby/object:Gem::Version
|
|
40
68
|
version: '10.0'
|
|
41
69
|
- !ruby/object:Gem::Dependency
|
|
42
|
-
name:
|
|
70
|
+
name: rspec-json_expectations
|
|
43
71
|
requirement: !ruby/object:Gem::Requirement
|
|
44
72
|
requirements:
|
|
45
73
|
- - ">="
|
|
46
74
|
- !ruby/object:Gem::Version
|
|
47
|
-
version: '
|
|
48
|
-
type: :
|
|
75
|
+
version: '0'
|
|
76
|
+
type: :development
|
|
49
77
|
prerelease: false
|
|
50
78
|
version_requirements: !ruby/object:Gem::Requirement
|
|
51
79
|
requirements:
|
|
52
80
|
- - ">="
|
|
53
81
|
- !ruby/object:Gem::Version
|
|
54
|
-
version: '
|
|
82
|
+
version: '0'
|
|
55
83
|
- !ruby/object:Gem::Dependency
|
|
56
84
|
name: rspec-rails
|
|
57
85
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -66,6 +94,20 @@ dependencies:
|
|
|
66
94
|
- - ">="
|
|
67
95
|
- !ruby/object:Gem::Version
|
|
68
96
|
version: '0'
|
|
97
|
+
- !ruby/object:Gem::Dependency
|
|
98
|
+
name: rspec-xml_helpers
|
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
|
100
|
+
requirements:
|
|
101
|
+
- - ">="
|
|
102
|
+
- !ruby/object:Gem::Version
|
|
103
|
+
version: '0'
|
|
104
|
+
type: :development
|
|
105
|
+
prerelease: false
|
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
107
|
+
requirements:
|
|
108
|
+
- - ">="
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: '0'
|
|
69
111
|
description: mutils Utilities for rails app
|
|
70
112
|
email:
|
|
71
113
|
- nitesh.purohit.it@gmail.com
|
|
@@ -79,11 +121,14 @@ files:
|
|
|
79
121
|
- ".github/ISSUE_TEMPLATE/feature_request.md"
|
|
80
122
|
- ".github/PULL_REQUEST_TEMPLATE.md"
|
|
81
123
|
- ".github/workflows/gempush.yml"
|
|
82
|
-
- ".github/workflows/ruby.yml"
|
|
83
124
|
- ".gitignore"
|
|
125
|
+
- ".mergify.yml"
|
|
84
126
|
- ".rspec"
|
|
85
127
|
- ".rubocop.yml"
|
|
128
|
+
- ".rubocop_todo.yml"
|
|
129
|
+
- ".travis.yml"
|
|
86
130
|
- BENCHMARK-SERIALIZER-JSON.md
|
|
131
|
+
- CHANGELOG.md
|
|
87
132
|
- CODE_OF_CONDUCT.md
|
|
88
133
|
- Gemfile
|
|
89
134
|
- Gemfile.lock
|
|
@@ -109,7 +154,7 @@ licenses:
|
|
|
109
154
|
- MIT
|
|
110
155
|
metadata:
|
|
111
156
|
bug_tracker_uri: https://github.com/niteshpurohit/mutils/issues
|
|
112
|
-
source_code_uri: https://github.com/niteshpurohit/mutils/tree/v0.2.
|
|
157
|
+
source_code_uri: https://github.com/niteshpurohit/mutils/tree/v0.2.27
|
|
113
158
|
post_install_message:
|
|
114
159
|
rdoc_options: []
|
|
115
160
|
require_paths:
|
data/.github/workflows/ruby.yml
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
name: Ruby
|
|
2
|
-
|
|
3
|
-
on: [push]
|
|
4
|
-
|
|
5
|
-
jobs:
|
|
6
|
-
build:
|
|
7
|
-
|
|
8
|
-
runs-on: ubuntu-latest
|
|
9
|
-
|
|
10
|
-
steps:
|
|
11
|
-
- uses: actions/checkout@v1
|
|
12
|
-
- name: Set up Ruby 2.6
|
|
13
|
-
uses: actions/setup-ruby@v1
|
|
14
|
-
with:
|
|
15
|
-
ruby-version: 2.6.x
|
|
16
|
-
- name: Build and test with Rake
|
|
17
|
-
run: |
|
|
18
|
-
gem install bundler
|
|
19
|
-
bundle install --jobs 4 --retry 3
|
|
20
|
-
bundle exec rake
|