mutils 0.2.25
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 +7 -0
- data/.github/CONTRIBUTING.md +30 -0
- data/.github/ISSUE_TEMPLATE.md +32 -0
- data/.github/ISSUE_TEMPLATE/bug_report.md +41 -0
- data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +23 -0
- data/.github/workflows/gempush.yml +27 -0
- data/.github/workflows/ruby.yml +20 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.rubocop.yml +2 -0
- data/BENCHMARK-SERIALIZER-JSON.md +82 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +92 -0
- data/LICENSE.txt +21 -0
- data/Makefile +6 -0
- data/README.md +126 -0
- data/Rakefile +6 -0
- data/benchmark/benchmark-serializer-json.rb +248 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/generators/serializer/USAGE +8 -0
- data/lib/generators/serializer/serializer_generator.rb +19 -0
- data/lib/generators/serializer/templates/serializer.rb.tt +8 -0
- data/lib/mutils.rb +11 -0
- data/lib/mutils/serialization/base_serializer.rb +32 -0
- data/lib/mutils/serialization/serialization_includes.rb +20 -0
- data/lib/mutils/serialization/serialization_methods.rb +56 -0
- data/lib/mutils/serialization/serialization_results.rb +85 -0
- data/lib/mutils/version.rb +3 -0
- data/mutils.gemspec +32 -0
- metadata +132 -0
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails/generators/base'
|
4
|
+
|
5
|
+
class SerializerGenerator < Rails::Generators::NamedBase
|
6
|
+
source_root File.expand_path('templates', __dir__)
|
7
|
+
|
8
|
+
argument :attributes, type: :array, default: [], banner: 'field field'
|
9
|
+
|
10
|
+
def create_serializer_file
|
11
|
+
template 'serializer.rb.tt', File.join('app', 'serializers', class_path, "#{file_name}_serializer.rb")
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def attributes_names
|
17
|
+
attributes.map { |a| a.name.to_sym.inspect }
|
18
|
+
end
|
19
|
+
end
|
data/lib/mutils.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require_relative 'mutils/version'
|
2
|
+
require 'active_support/concern'
|
3
|
+
require_relative 'mutils/serialization/serialization_results'
|
4
|
+
require_relative 'mutils/serialization/serialization_includes'
|
5
|
+
require_relative 'mutils/serialization/serialization_methods'
|
6
|
+
require_relative 'mutils/serialization/base_serializer'
|
7
|
+
|
8
|
+
module Mutils
|
9
|
+
class Error < StandardError
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'json/ext'
|
3
|
+
# module Mutils
|
4
|
+
module Mutils
|
5
|
+
module Serialization
|
6
|
+
# BaseSerializer
|
7
|
+
class BaseSerializer
|
8
|
+
include Mutils::Serialization::SerializationIncludes
|
9
|
+
include Mutils::Serialization::SerializationMethods
|
10
|
+
include Mutils::Serialization::SerializationResults
|
11
|
+
attr_accessor :scope, :options, :mutex
|
12
|
+
|
13
|
+
def initialize(object, options = {})
|
14
|
+
self.scope = object
|
15
|
+
self.options = options
|
16
|
+
self.mutex = Mutex.new
|
17
|
+
end
|
18
|
+
|
19
|
+
def as_json(_options = {})
|
20
|
+
to_h
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_h
|
24
|
+
generate_hash
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_json(_options = {})
|
28
|
+
JSON.generate(to_h)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Module Mutils
|
4
|
+
module Mutils
|
5
|
+
module Serialization
|
6
|
+
# Module SerializationIncludes
|
7
|
+
module SerializationIncludes
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
included do
|
10
|
+
class << self
|
11
|
+
attr_accessor :method_to_serialize,
|
12
|
+
:attributes_to_serialize,
|
13
|
+
:array_index,
|
14
|
+
:belongs_to_relationships,
|
15
|
+
:has_many_relationships
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Module Mutils
|
4
|
+
module Mutils
|
5
|
+
module Serialization
|
6
|
+
# Module SerializationCore
|
7
|
+
module SerializationMethods
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
module ClassMethods
|
10
|
+
def attributes(*attributes_list)
|
11
|
+
self.attributes_to_serialize = {} if attributes_to_serialize.nil?
|
12
|
+
attributes_list&.each { |attr| attributes_to_serialize[attr] = attr }
|
13
|
+
end
|
14
|
+
|
15
|
+
def custom_methods(*method_list)
|
16
|
+
self.method_to_serialize = {} if method_to_serialize.nil?
|
17
|
+
method_list&.each { |attr| method_to_serialize[attr] = attr }
|
18
|
+
end
|
19
|
+
|
20
|
+
def belongs_to(relationship_name, options = {})
|
21
|
+
if options[:serializer].nil?
|
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
|
+
|
30
|
+
self.belongs_to_relationships = {} if belongs_to_relationships.nil?
|
31
|
+
belongs_to_relationships[relationship_name] = options
|
32
|
+
end
|
33
|
+
|
34
|
+
alias has_one belongs_to
|
35
|
+
|
36
|
+
def has_many(relationship_name, options = {})
|
37
|
+
if options[:serializer].nil?
|
38
|
+
raise "Serializer is Required for has_many :#{relationship_name}." \
|
39
|
+
"\nDefine it like:\nbelongs_to :#{relationship_name}, " \
|
40
|
+
'serializer: SERIALIZER_CLASS'
|
41
|
+
end
|
42
|
+
unless class_exists? options[:serializer]
|
43
|
+
raise 'Serializer class not defined'
|
44
|
+
end
|
45
|
+
|
46
|
+
self.has_many_relationships = {} if has_many_relationships.nil?
|
47
|
+
has_many_relationships[relationship_name] = options
|
48
|
+
end
|
49
|
+
|
50
|
+
def class_exists?(class_name)
|
51
|
+
eval("defined?(#{class_name}) && #{class_name}.is_a?(Class)") == true
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Module Mutils
|
4
|
+
module Mutils
|
5
|
+
module Serialization
|
6
|
+
# Module SerializationResults
|
7
|
+
module SerializationResults
|
8
|
+
|
9
|
+
def generate_hash
|
10
|
+
if scope
|
11
|
+
if scope_is_collection?
|
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
|
+
relationships = [self.class.belongs_to_relationships, self.class.has_many_relationships]
|
23
|
+
[fetch_attributes(self.class.attributes_to_serialize&.keys),
|
24
|
+
call_methods(self.class.method_to_serialize&.keys),
|
25
|
+
hash_relationships(relationships)].reduce(&:merge)
|
26
|
+
end
|
27
|
+
|
28
|
+
def fetch_attributes(keys)
|
29
|
+
invoke_sends_async(keys)
|
30
|
+
end
|
31
|
+
|
32
|
+
def call_methods(keys)
|
33
|
+
invoke_sends(keys, true)
|
34
|
+
end
|
35
|
+
|
36
|
+
def invoke_sends(keys, call_method = nil)
|
37
|
+
hash = {}
|
38
|
+
keys&.each do |key|
|
39
|
+
invoke_send(hash, key, call_method)
|
40
|
+
end
|
41
|
+
hash
|
42
|
+
end
|
43
|
+
|
44
|
+
def invoke_sends_async(keys, call_method = nil)
|
45
|
+
hash = {}
|
46
|
+
runners = []
|
47
|
+
keys&.each do |key|
|
48
|
+
runners << Thread.new do
|
49
|
+
mutex.synchronize { invoke_send(hash, key, call_method) }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
runners.map(&:join)
|
53
|
+
hash
|
54
|
+
end
|
55
|
+
|
56
|
+
def invoke_send(hash, key, call_method = nil)
|
57
|
+
hash[key] = send(key) if call_method
|
58
|
+
hash[key] = scope.send(key) unless call_method
|
59
|
+
hash
|
60
|
+
end
|
61
|
+
|
62
|
+
def hash_relationships(relationships_array)
|
63
|
+
relationships = relationships_array.compact.reduce(&:merge)
|
64
|
+
hash = {}
|
65
|
+
relationships&.keys&.each do |key|
|
66
|
+
if check_if_included(relationships, key)
|
67
|
+
klass = relationships[key][:serializer]
|
68
|
+
hash[key] = klass.new(scope.send(key)).to_h
|
69
|
+
end
|
70
|
+
end
|
71
|
+
hash
|
72
|
+
end
|
73
|
+
|
74
|
+
def check_if_included(relationships, key)
|
75
|
+
always_include = relationships[key][:always_include]
|
76
|
+
always_include = always_include.nil? ? false : always_include == true
|
77
|
+
always_include || (options[:includes]&.include?(key))
|
78
|
+
end
|
79
|
+
|
80
|
+
def scope_is_collection?
|
81
|
+
scope.respond_to?(:size) && !scope.respond_to?(:each_pair)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
data/mutils.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'mutils/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'mutils'
|
7
|
+
spec.version = Mutils::VERSION
|
8
|
+
spec.authors = ['Nitesh Purohit']
|
9
|
+
spec.email = ['nitesh.purohit.it@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'mutils Utilities for rails app'
|
12
|
+
spec.description = 'mutils Utilities for rails app'
|
13
|
+
spec.homepage = 'https://github.com/niteshpurohit/mutils'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
16
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
end
|
18
|
+
spec.bindir = 'exe'
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
spec.required_ruby_version = '>= 2.5.0'
|
22
|
+
spec.required_rubygems_version = '>= 1.8.11'
|
23
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
24
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
25
|
+
spec.add_runtime_dependency('activesupport', ['>= 4.2'])
|
26
|
+
spec.add_development_dependency 'rspec-rails'
|
27
|
+
|
28
|
+
spec.metadata = {
|
29
|
+
"bug_tracker_uri" => "https://github.com/niteshpurohit/mutils/issues",
|
30
|
+
"source_code_uri" => "https://github.com/niteshpurohit/mutils/tree/v#{Mutils::VERSION}"
|
31
|
+
}
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mutils
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.25
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nitesh Purohit
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-11-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activesupport
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '4.2'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '4.2'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec-rails
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: mutils Utilities for rails app
|
70
|
+
email:
|
71
|
+
- nitesh.purohit.it@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".github/CONTRIBUTING.md"
|
77
|
+
- ".github/ISSUE_TEMPLATE.md"
|
78
|
+
- ".github/ISSUE_TEMPLATE/bug_report.md"
|
79
|
+
- ".github/ISSUE_TEMPLATE/feature_request.md"
|
80
|
+
- ".github/PULL_REQUEST_TEMPLATE.md"
|
81
|
+
- ".github/workflows/gempush.yml"
|
82
|
+
- ".github/workflows/ruby.yml"
|
83
|
+
- ".gitignore"
|
84
|
+
- ".rspec"
|
85
|
+
- ".rubocop.yml"
|
86
|
+
- BENCHMARK-SERIALIZER-JSON.md
|
87
|
+
- CODE_OF_CONDUCT.md
|
88
|
+
- Gemfile
|
89
|
+
- Gemfile.lock
|
90
|
+
- LICENSE.txt
|
91
|
+
- Makefile
|
92
|
+
- README.md
|
93
|
+
- Rakefile
|
94
|
+
- benchmark/benchmark-serializer-json.rb
|
95
|
+
- bin/console
|
96
|
+
- bin/setup
|
97
|
+
- lib/generators/serializer/USAGE
|
98
|
+
- lib/generators/serializer/serializer_generator.rb
|
99
|
+
- lib/generators/serializer/templates/serializer.rb.tt
|
100
|
+
- lib/mutils.rb
|
101
|
+
- lib/mutils/serialization/base_serializer.rb
|
102
|
+
- lib/mutils/serialization/serialization_includes.rb
|
103
|
+
- lib/mutils/serialization/serialization_methods.rb
|
104
|
+
- lib/mutils/serialization/serialization_results.rb
|
105
|
+
- lib/mutils/version.rb
|
106
|
+
- mutils.gemspec
|
107
|
+
homepage: https://github.com/niteshpurohit/mutils
|
108
|
+
licenses:
|
109
|
+
- MIT
|
110
|
+
metadata:
|
111
|
+
bug_tracker_uri: https://github.com/niteshpurohit/mutils/issues
|
112
|
+
source_code_uri: https://github.com/niteshpurohit/mutils/tree/v0.2.25
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: 2.5.0
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: 1.8.11
|
127
|
+
requirements: []
|
128
|
+
rubygems_version: 3.0.3
|
129
|
+
signing_key:
|
130
|
+
specification_version: 4
|
131
|
+
summary: mutils Utilities for rails app
|
132
|
+
test_files: []
|