mutils 0.2.37 → 1.0.0
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/.rspec +0 -1
- data/.rubocop.yml +1 -2
- data/.rubocop_todo.yml +2 -0
- data/CHANGELOG.md +13 -4
- data/Gemfile.lock +10 -1
- data/lib/mutils.rb +1 -0
- data/lib/mutils/lib/helper.rb +14 -4
- data/lib/mutils/lib/result_hash.rb +25 -0
- data/lib/mutils/serialization/base_serializer.rb +0 -8
- data/lib/mutils/serialization/methods/attributes.rb +52 -0
- data/lib/mutils/serialization/methods/main.rb +27 -0
- data/lib/mutils/serialization/methods/relations.rb +40 -0
- data/lib/mutils/serialization/results/attributes.rb +24 -0
- data/lib/mutils/serialization/results/main.rb +59 -0
- data/lib/mutils/serialization/results/relations.rb +23 -0
- data/lib/mutils/serialization/serialization_methods.rb +6 -79
- data/lib/mutils/serialization/serialization_results.rb +6 -77
- data/lib/mutils/version.rb +1 -1
- data/mutils.gemspec +1 -0
- metadata +24 -4
- data/Makefile +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e38e7ca1f83b872f6a72819355f44ef30a1de7b6c3756abd27e3ec66e256b90a
|
4
|
+
data.tar.gz: a3267a83c8a82770d84f7636f3f8b19aa5039eabcfc6b2953b605981d21e1d6b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a423ea8de2ccf77982fdfec8a9dd9b164a42b0da5b2aa4664889d1e13bae2eba384344bc25c7a24ec8267218a98170c0f7b92c7695926cb72a8b2d6ad695d9b
|
7
|
+
data.tar.gz: 3f1f7b51833b2cb57d8ad2da72aebbca7799ac9f86f61380ab2a9cce5af15bc642591fe5b483d78054a37b4e9ff8b5cbcfe4776537bd8c7da650b83a8ba59679
|
data/.rspec
CHANGED
data/.rubocop.yml
CHANGED
data/.rubocop_todo.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,16 @@
|
|
1
1
|
# Changelog
|
2
|
+
## [v1.0.0](https://github.com/code-vedas/mutils/tree/v0.2.36) (2020-04-17)
|
3
|
+
|
4
|
+
[Full Changelog](https://github.com/code-vedas/mutils/compare/v0.2.36...v1.0.0)
|
5
|
+
|
6
|
+
**First Stable release: Features**
|
7
|
+
- Performance upto 35i/s under load
|
8
|
+
- Observed 35-40 MB of memory usage
|
9
|
+
- Fully featured JSON serializer similar to Active Serializer but with more power and speed.
|
10
|
+
|
11
|
+
**Changes:**
|
12
|
+
- Code refactored
|
13
|
+
- Using cache for expensive operations
|
2
14
|
|
3
15
|
## [v0.2.36](https://github.com/code-vedas/mutils/tree/v0.2.36) (2020-04-14)
|
4
16
|
|
@@ -108,7 +120,4 @@
|
|
108
120
|
**Merged pull requests:**
|
109
121
|
|
110
122
|
- Feature/improve concurrency [\#4](https://github.com/code-vedas/mutils/pull/4) ([niteshpurohit](https://github.com/niteshpurohit))
|
111
|
-
- Testing Deploy [\#3](https://github.com/code-vedas/mutils/pull/3) ([niteshpurohit](https://github.com/niteshpurohit))
|
112
|
-
|
113
|
-
|
114
|
-
\* *This Changelog was automatically generated by [github_changelog_generator](https://github.com/github-changelog-generator/github-changelog-generator)*
|
123
|
+
- Testing Deploy [\#3](https://github.com/code-vedas/mutils/pull/3) ([niteshpurohit](https://github.com/niteshpurohit))
|
data/Gemfile.lock
CHANGED
@@ -1,12 +1,15 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
mutils (0.
|
4
|
+
mutils (1.0.0)
|
5
5
|
dry-inflector
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
+
benchmark-malloc (0.2.0)
|
11
|
+
benchmark-perf (0.6.0)
|
12
|
+
benchmark-trend (0.4.0)
|
10
13
|
coveralls (0.8.23)
|
11
14
|
json (>= 1.8, < 3)
|
12
15
|
simplecov (~> 0.16.1)
|
@@ -23,6 +26,11 @@ GEM
|
|
23
26
|
rspec-core (~> 3.9.0)
|
24
27
|
rspec-expectations (~> 3.9.0)
|
25
28
|
rspec-mocks (~> 3.9.0)
|
29
|
+
rspec-benchmark (0.6.0)
|
30
|
+
benchmark-malloc (~> 0.2)
|
31
|
+
benchmark-perf (~> 0.6)
|
32
|
+
benchmark-trend (~> 0.4)
|
33
|
+
rspec (>= 3.0)
|
26
34
|
rspec-core (3.9.1)
|
27
35
|
rspec-support (~> 3.9.1)
|
28
36
|
rspec-expectations (3.9.1)
|
@@ -55,6 +63,7 @@ DEPENDENCIES
|
|
55
63
|
mutils!
|
56
64
|
rake (~> 13.0)
|
57
65
|
rspec
|
66
|
+
rspec-benchmark
|
58
67
|
rspec-json_expectations
|
59
68
|
|
60
69
|
BUNDLED WITH
|
data/lib/mutils.rb
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
require 'dry/inflector'
|
4
4
|
require_relative 'mutils/version'
|
5
5
|
require_relative 'mutils/lib/helper'
|
6
|
+
require_relative 'mutils/lib/result_hash'
|
6
7
|
require_relative 'mutils/serialization/serialization_results'
|
7
8
|
require_relative 'mutils/serialization/serialization_includes'
|
8
9
|
require_relative 'mutils/serialization/serialization_methods'
|
data/lib/mutils/lib/helper.rb
CHANGED
@@ -4,20 +4,30 @@ require 'singleton'
|
|
4
4
|
# module Mutils
|
5
5
|
module Mutils
|
6
6
|
module Lib
|
7
|
-
#
|
7
|
+
# Helper: caching expensive repetitive operations
|
8
8
|
class Helper
|
9
9
|
include Singleton
|
10
10
|
|
11
11
|
def initialize
|
12
12
|
self.inflector_object = Dry::Inflector.new
|
13
|
+
self.pluralize_cache = {}
|
14
|
+
self.underscore_cache = {}
|
15
|
+
self.constantize_cache = {}
|
13
16
|
end
|
14
17
|
|
15
18
|
def underscore(string)
|
16
|
-
inflector_object.underscore string
|
19
|
+
underscore_cache[string] = inflector_object.underscore string unless underscore_cache[string]
|
20
|
+
underscore_cache[string]
|
17
21
|
end
|
18
22
|
|
19
23
|
def pluralize(string)
|
20
|
-
inflector_object.pluralize string
|
24
|
+
pluralize_cache[string] = inflector_object.pluralize string unless pluralize_cache[string]
|
25
|
+
pluralize_cache[string]
|
26
|
+
end
|
27
|
+
|
28
|
+
def constantize(string)
|
29
|
+
constantize_cache[string] = Object.const_get string unless constantize_cache[string]
|
30
|
+
constantize_cache[string]
|
21
31
|
end
|
22
32
|
|
23
33
|
def collection?(object)
|
@@ -26,7 +36,7 @@ module Mutils
|
|
26
36
|
|
27
37
|
private
|
28
38
|
|
29
|
-
attr_accessor :inflector_object
|
39
|
+
attr_accessor :inflector_object, :pluralize_cache, :underscore_cache, :constantize_cache
|
30
40
|
end
|
31
41
|
end
|
32
42
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# module Mutils
|
4
|
+
module Mutils
|
5
|
+
module Lib
|
6
|
+
# ResultHash: Store result using this class.
|
7
|
+
class ResultHash
|
8
|
+
def initialize
|
9
|
+
self._hash = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def []=(key, value)
|
13
|
+
_hash[key] = value
|
14
|
+
end
|
15
|
+
|
16
|
+
def hash
|
17
|
+
_hash
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
attr_accessor :_hash
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -33,14 +33,6 @@ module Mutils
|
|
33
33
|
JSON.generate(as_json, options)
|
34
34
|
end
|
35
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
36
|
private
|
45
37
|
|
46
38
|
attr_writer :scope
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Module Mutils
|
4
|
+
module Mutils
|
5
|
+
# Module SerializationCore
|
6
|
+
module Serialization
|
7
|
+
# Module Methods
|
8
|
+
module Methods
|
9
|
+
# Module Attributes
|
10
|
+
module Attributes
|
11
|
+
def attributes(*attributes_list)
|
12
|
+
parse_attributes_methods(attributes_list, false)
|
13
|
+
end
|
14
|
+
|
15
|
+
def custom_methods(*attributes_list)
|
16
|
+
parse_attributes_methods(attributes_list, true)
|
17
|
+
end
|
18
|
+
|
19
|
+
def parse_attributes_methods(list, is_method)
|
20
|
+
self.attributes_to_serialize = {} if attributes_to_serialize.nil?
|
21
|
+
list&.each do |attr|
|
22
|
+
value = { method: is_method, always_include: true }
|
23
|
+
attributes_to_serialize[attr] = value
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def attribute(method_name, options = {}, &proc)
|
28
|
+
raise "if: should be a Proc object for attribute #{method_name}" if options[:if] && !options[:if].instance_of?(Proc)
|
29
|
+
|
30
|
+
if proc.instance_of? Proc
|
31
|
+
self.attributes_to_serialize_blocks = {} if attributes_to_serialize_blocks.nil?
|
32
|
+
options[:block] = proc
|
33
|
+
attributes_to_serialize_blocks[method_name] = options
|
34
|
+
else
|
35
|
+
add_single_attribute(method_name, options, false)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def custom_method(method_name, options = {})
|
40
|
+
add_single_attribute(method_name, options, true)
|
41
|
+
end
|
42
|
+
|
43
|
+
def add_single_attribute(method_name, options, is_method)
|
44
|
+
self.attributes_to_serialize = {} if attributes_to_serialize.nil?
|
45
|
+
always_include = options[:always_include].nil? ? false : options[:always_include]
|
46
|
+
value = { method: is_method, always_include: always_include, if: options[:if] }
|
47
|
+
attributes_to_serialize[method_name] = value
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Module Mutils
|
4
|
+
module Mutils
|
5
|
+
# Module SerializationCore
|
6
|
+
module Serialization
|
7
|
+
# Module Methods
|
8
|
+
module Methods
|
9
|
+
# Module Main
|
10
|
+
module Main
|
11
|
+
def name_tag(name_tag, root = nil)
|
12
|
+
self.serializer_name = name_tag
|
13
|
+
self.include_root = root
|
14
|
+
end
|
15
|
+
|
16
|
+
def class_exists?(class_name)
|
17
|
+
klass = begin
|
18
|
+
Mutils::Lib::Helper.instance.constantize(class_name.to_s)
|
19
|
+
rescue StandardError
|
20
|
+
nil
|
21
|
+
end
|
22
|
+
klass && defined?(klass) && klass.is_a?(Class)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Module Mutils
|
4
|
+
module Mutils
|
5
|
+
# Module SerializationCore
|
6
|
+
module Serialization
|
7
|
+
# Module Methods
|
8
|
+
module Methods
|
9
|
+
# Module Relations
|
10
|
+
module Relations
|
11
|
+
def relationship(relationship_name, options = {})
|
12
|
+
raise "if: should be a Proc object for attribute #{relationship_name}" if options[:if] && !options[:if].instance_of?(Proc)
|
13
|
+
|
14
|
+
options = prepare_options(relationship_name, options, __callee__)
|
15
|
+
self.relationships = {} if relationships.nil?
|
16
|
+
relationships[relationship_name] = options
|
17
|
+
end
|
18
|
+
|
19
|
+
alias belongs_to relationship
|
20
|
+
alias has_many relationship
|
21
|
+
alias has_one relationship
|
22
|
+
|
23
|
+
def prepare_options(relationship_name, options, option_name)
|
24
|
+
class_name = options[:serializer]
|
25
|
+
if class_name.nil?
|
26
|
+
raise "Serializer is Required for belongs_to :#{relationship_name}." \
|
27
|
+
"\nDefine it like:\n#{option_name} :#{relationship_name}, " \
|
28
|
+
'serializer: SERIALIZER_CLASS'
|
29
|
+
end
|
30
|
+
raise "Serializer class not defined for relationship: #{relationship_name}" unless class_exists? class_name
|
31
|
+
|
32
|
+
options[:serializer] = class_name.to_s
|
33
|
+
options[:always_include] = options[:always_include].nil? ? false : options[:always_include]
|
34
|
+
options[:label] = Lib::Helper.instance.underscore options[:label] || relationship_name
|
35
|
+
options
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Module Mutils
|
4
|
+
module Mutils
|
5
|
+
module Serialization
|
6
|
+
# Module Results
|
7
|
+
module Results
|
8
|
+
# Module Attributes
|
9
|
+
module Attributes
|
10
|
+
def fetch_block_attributes(attributes, result_hash)
|
11
|
+
attributes&.each do |key, s_options|
|
12
|
+
result_hash[key] = s_options[:block].call(scope, options[:params])
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def fetch_attributes(attributes, result_hash)
|
17
|
+
attributes&.each do |key, s_options|
|
18
|
+
check_if_included(s_options, key) && (result_hash[key] = s_options[:method] ? send(key) : scope.send(key))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Module Mutils
|
4
|
+
module Mutils
|
5
|
+
module Serialization
|
6
|
+
# Module Results
|
7
|
+
module Results
|
8
|
+
# Module Attributes
|
9
|
+
module Main
|
10
|
+
def generate_hash
|
11
|
+
if scope
|
12
|
+
if scope_is_collection?
|
13
|
+
options[:child] = true
|
14
|
+
scope.map { |inner_scope| self.class.new(inner_scope, options).generate_hash }
|
15
|
+
else
|
16
|
+
hashed_result(Lib::ResultHash.new)
|
17
|
+
end
|
18
|
+
else
|
19
|
+
{}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def hashed_result(result_hash)
|
24
|
+
fetch_block_attributes(self.class.attributes_to_serialize_blocks, result_hash)
|
25
|
+
fetch_attributes(self.class.attributes_to_serialize, result_hash)
|
26
|
+
hash_relationships(self.class.relationships, result_hash)
|
27
|
+
|
28
|
+
result_hash.hash
|
29
|
+
end
|
30
|
+
|
31
|
+
def check_if_included(s_options, key)
|
32
|
+
return s_options[:if].call(scope) unless s_options[:if].nil? || scope_is_collection?
|
33
|
+
|
34
|
+
s_options[:always_include] || options[:includes]&.include?(key)
|
35
|
+
end
|
36
|
+
|
37
|
+
def scope_is_collection?
|
38
|
+
Lib::Helper.instance.collection? scope
|
39
|
+
end
|
40
|
+
|
41
|
+
def class_name
|
42
|
+
if scope_is_collection?
|
43
|
+
Lib::Helper.instance.pluralize(format_class_name(scope[0]))
|
44
|
+
else
|
45
|
+
format_class_name(scope)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def format_class_name(object)
|
50
|
+
if self.class.serializer_name&.length&.positive?
|
51
|
+
self.class.serializer_name
|
52
|
+
else
|
53
|
+
object.class.to_s.downcase
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Module Mutils
|
4
|
+
module Mutils
|
5
|
+
module Serialization
|
6
|
+
# Module Results
|
7
|
+
module Results
|
8
|
+
# Module Relations
|
9
|
+
module Relations
|
10
|
+
def hash_relationships(relationships_array, result_hash)
|
11
|
+
relationships = relationships_array&.compact
|
12
|
+
relationships&.each do |key, s_options|
|
13
|
+
object = scope.send(key)
|
14
|
+
name = s_options[:label]
|
15
|
+
Lib::Helper.instance.collection?(object) && (name = Lib::Helper.instance.pluralize(name))
|
16
|
+
name = name.to_sym
|
17
|
+
check_if_included(s_options, key) && (result_hash[name] = Lib::Helper.instance.constantize(s_options[:serializer]).new(object).to_h)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -1,90 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative 'methods/main'
|
4
|
+
require_relative 'methods/relations'
|
5
|
+
require_relative 'methods/attributes'
|
3
6
|
# Module Mutils
|
4
7
|
module Mutils
|
5
8
|
module Serialization
|
6
9
|
# Module SerializationCore
|
7
10
|
module SerializationMethods
|
8
11
|
def self.included(base)
|
9
|
-
base.extend
|
10
|
-
|
11
|
-
|
12
|
-
# Module ClassMethods
|
13
|
-
module ClassMethods
|
14
|
-
def name_tag(name_tag, root = nil)
|
15
|
-
self.serializer_name = name_tag
|
16
|
-
self.include_root = root
|
17
|
-
end
|
18
|
-
|
19
|
-
def attributes(*attributes_list)
|
20
|
-
parse_attributes_methods(attributes_list, 'attributes')
|
21
|
-
end
|
22
|
-
|
23
|
-
def custom_methods(*attributes_list)
|
24
|
-
parse_attributes_methods(attributes_list, 'method')
|
25
|
-
end
|
26
|
-
|
27
|
-
def parse_attributes_methods(list, type)
|
28
|
-
self.attributes_to_serialize = {} if attributes_to_serialize.nil?
|
29
|
-
list&.each do |attr|
|
30
|
-
value = { method: type == 'method', always_include: true }
|
31
|
-
attributes_to_serialize[attr] = value
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def attribute(method_name, options = {}, &proc)
|
36
|
-
raise "if: should be a Proc object for attribute #{method_name}" if options[:if] && (options[:if].class.to_s != 'Proc')
|
37
|
-
|
38
|
-
if proc.class.to_s == 'Proc'
|
39
|
-
self.attributes_to_serialize_blocks = {} if attributes_to_serialize_blocks.nil?
|
40
|
-
options[:block] = proc
|
41
|
-
attributes_to_serialize_blocks[method_name] = options
|
42
|
-
else
|
43
|
-
add_single_attribute(method_name, options, 'attribute')
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
def custom_method(method_name, options = {})
|
48
|
-
add_single_attribute(method_name, options, 'method')
|
49
|
-
end
|
50
|
-
|
51
|
-
def add_single_attribute(method_name, options, type)
|
52
|
-
self.attributes_to_serialize = {} if attributes_to_serialize.nil?
|
53
|
-
always_include = options[:always_include].nil? ? false : options[:always_include]
|
54
|
-
value = { method: type == 'method', always_include: always_include, if: options[:if] }
|
55
|
-
attributes_to_serialize[method_name] = value
|
56
|
-
end
|
57
|
-
|
58
|
-
def relationship(relationship_name, options = {})
|
59
|
-
options = prepare_options(relationship_name, options, __callee__)
|
60
|
-
self.relationships = {} if relationships.nil?
|
61
|
-
relationships[relationship_name] = options
|
62
|
-
end
|
63
|
-
|
64
|
-
alias belongs_to relationship
|
65
|
-
alias has_many relationship
|
66
|
-
alias has_one relationship
|
67
|
-
|
68
|
-
def prepare_options(relationship_name, options, option_name)
|
69
|
-
class_name = options[:serializer]
|
70
|
-
if class_name.nil?
|
71
|
-
raise "Serializer is Required for belongs_to :#{relationship_name}." \
|
72
|
-
"\nDefine it like:\n#{option_name} :#{relationship_name}, " \
|
73
|
-
'serializer: SERIALIZER_CLASS'
|
74
|
-
end
|
75
|
-
raise "Serializer class not defined for relationship: #{relationship_name}" unless class_exists? class_name
|
76
|
-
raise "if: should be a Proc object for attribute #{relationship_name}" if options[:if] && (options[:if].class.to_s != 'Proc')
|
77
|
-
|
78
|
-
options[:serializer] = Object.const_get class_name.to_s
|
79
|
-
options[:option_name] = option_name
|
80
|
-
options[:label] = options[:label]
|
81
|
-
options
|
82
|
-
end
|
83
|
-
|
84
|
-
def class_exists?(class_name)
|
85
|
-
klass = Object.const_get class_name.to_s rescue nil
|
86
|
-
klass && defined?(klass) && klass.is_a?(Class)
|
87
|
-
end
|
12
|
+
base.extend Methods::Main
|
13
|
+
base.extend Methods::Attributes
|
14
|
+
base.extend Methods::Relations
|
88
15
|
end
|
89
16
|
end
|
90
17
|
end
|
@@ -1,87 +1,16 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative 'results/main'
|
4
|
+
require_relative 'results/attributes'
|
5
|
+
require_relative 'results/relations'
|
3
6
|
# Module Mutils
|
4
7
|
module Mutils
|
5
8
|
module Serialization
|
6
9
|
# Module SerializationResults
|
7
10
|
module SerializationResults
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
options[:child] = true
|
12
|
-
scope.map { |inner_scope| self.class.new(inner_scope, options).generate_hash }
|
13
|
-
else
|
14
|
-
hashed_result
|
15
|
-
end
|
16
|
-
else
|
17
|
-
{}
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def hashed_result
|
22
|
-
[fetch_block_attributes(self.class.attributes_to_serialize_blocks),
|
23
|
-
fetch_attributes(self.class.attributes_to_serialize),
|
24
|
-
hash_relationships(self.class.relationships)].reduce(&:merge)
|
25
|
-
end
|
26
|
-
|
27
|
-
def fetch_block_attributes(attributes)
|
28
|
-
hash = {}
|
29
|
-
attributes&.keys&.each do |key|
|
30
|
-
hash[key] = attributes[key][:block].call(scope, options[:params])
|
31
|
-
end
|
32
|
-
hash
|
33
|
-
end
|
34
|
-
|
35
|
-
def fetch_attributes(attributes)
|
36
|
-
hash = {}
|
37
|
-
attributes&.keys&.each do |key|
|
38
|
-
check_if_included(attributes, key) && (hash[key] = attributes[key][:method] ? send(key) : scope.send(key))
|
39
|
-
end
|
40
|
-
hash
|
41
|
-
end
|
42
|
-
|
43
|
-
def hash_relationships(relationships_array)
|
44
|
-
relationships = relationships_array&.compact
|
45
|
-
hash = {}
|
46
|
-
relationships&.keys&.each do |key|
|
47
|
-
object = scope.send(key)
|
48
|
-
name = key
|
49
|
-
if (label = relationships[key][:label])
|
50
|
-
name = Lib::Helper.instance.underscore label
|
51
|
-
Lib::Helper.instance.collection?(object) && (name = Lib::Helper.instance.pluralize(name))
|
52
|
-
name = name.to_sym
|
53
|
-
end
|
54
|
-
check_if_included(relationships, key) && (hash[name] = relationships[key][:serializer].new(object).to_h)
|
55
|
-
end
|
56
|
-
hash
|
57
|
-
end
|
58
|
-
|
59
|
-
def check_if_included(relationships, key)
|
60
|
-
return relationships[key][:if].call(scope) unless relationships[key][:if].nil? || scope_is_collection?
|
61
|
-
|
62
|
-
always_include = relationships[key][:always_include] == true
|
63
|
-
always_include || options[:includes]&.include?(key)
|
64
|
-
end
|
65
|
-
|
66
|
-
def scope_is_collection?
|
67
|
-
Lib::Helper.instance.collection? scope
|
68
|
-
end
|
69
|
-
|
70
|
-
def class_name
|
71
|
-
if scope_is_collection?
|
72
|
-
Lib::Helper.name.pluralize(format_class_name(scope[0]))
|
73
|
-
else
|
74
|
-
format_class_name(scope)
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
def format_class_name(object)
|
79
|
-
if self.class.serializer_name&.length&.positive?
|
80
|
-
self.class.serializer_name
|
81
|
-
else
|
82
|
-
object.class.to_s.downcase
|
83
|
-
end
|
84
|
-
end
|
11
|
+
include Mutils::Serialization::Results::Main
|
12
|
+
include Mutils::Serialization::Results::Attributes
|
13
|
+
include Mutils::Serialization::Results::Relations
|
85
14
|
end
|
86
15
|
end
|
87
16
|
end
|
data/lib/mutils/version.rb
CHANGED
data/mutils.gemspec
CHANGED
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
|
|
27
27
|
spec.add_development_dependency 'coveralls'
|
28
28
|
spec.add_development_dependency 'rake', '~> 13.0'
|
29
29
|
spec.add_development_dependency 'rspec'
|
30
|
+
spec.add_development_dependency 'rspec-benchmark'
|
30
31
|
spec.add_development_dependency 'rspec-json_expectations'
|
31
32
|
|
32
33
|
spec.metadata = {
|
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.
|
4
|
+
version: 1.0.0
|
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-04-
|
11
|
+
date: 2020-04-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dry-inflector
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec-benchmark
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: rspec-json_expectations
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -119,7 +133,6 @@ files:
|
|
119
133
|
- Gemfile
|
120
134
|
- Gemfile.lock
|
121
135
|
- LICENSE.txt
|
122
|
-
- Makefile
|
123
136
|
- README.md
|
124
137
|
- Rakefile
|
125
138
|
- bin/console
|
@@ -129,7 +142,14 @@ files:
|
|
129
142
|
- lib/generators/mutils/templates/serializer.rb.tt
|
130
143
|
- lib/mutils.rb
|
131
144
|
- lib/mutils/lib/helper.rb
|
145
|
+
- lib/mutils/lib/result_hash.rb
|
132
146
|
- lib/mutils/serialization/base_serializer.rb
|
147
|
+
- lib/mutils/serialization/methods/attributes.rb
|
148
|
+
- lib/mutils/serialization/methods/main.rb
|
149
|
+
- lib/mutils/serialization/methods/relations.rb
|
150
|
+
- lib/mutils/serialization/results/attributes.rb
|
151
|
+
- lib/mutils/serialization/results/main.rb
|
152
|
+
- lib/mutils/serialization/results/relations.rb
|
133
153
|
- lib/mutils/serialization/serialization_includes.rb
|
134
154
|
- lib/mutils/serialization/serialization_methods.rb
|
135
155
|
- lib/mutils/serialization/serialization_results.rb
|
@@ -140,7 +160,7 @@ licenses:
|
|
140
160
|
- MIT
|
141
161
|
metadata:
|
142
162
|
bug_tracker_uri: https://github.com/code-vedas/mutils/issues
|
143
|
-
source_code_uri: https://github.com/code-vedas/mutils/tree/
|
163
|
+
source_code_uri: https://github.com/code-vedas/mutils/tree/v1.0.0
|
144
164
|
post_install_message:
|
145
165
|
rdoc_options: []
|
146
166
|
require_paths:
|