mini_serializer 0.1.9 → 0.1.10
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/lib/mini_serializer/version.rb +1 -1
- data/lib/mini_serializer.rb +101 -98
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a2e2cbe75ecd3623daaafc5668d74d6816995a3bbdfbdaed96bb24d931460743
|
4
|
+
data.tar.gz: 6f39cdda960613c7c8a442db6f59421f1c4f9f93db70894c435ffa95fdfef645
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a03950edca1b6f3c9d05fe811506fd96d754b5deb10e91654ce800397a69bd2dc7456df3241a181be7f3040071bf179d47af9b246d4bcbbabe496883f113f0c
|
7
|
+
data.tar.gz: 6a536d22c9c4b35eb543d89dfbd9ae1506627f8b4627ba0777decadffbfe3e766b12e7fba306e86d9b10a68fc267cf39cae1e06e862c360340272829dd0a2552
|
data/lib/mini_serializer.rb
CHANGED
@@ -1,132 +1,135 @@
|
|
1
1
|
require "mini_serializer/version"
|
2
2
|
|
3
3
|
module MiniSerializer
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
4
|
+
class Serializer
|
5
|
+
|
6
|
+
def initialize(object_main,except_params={})
|
7
|
+
@wams_has_one=[]
|
8
|
+
@wams_has_many=[]
|
9
|
+
@wams_association_object=[]
|
10
|
+
@wams_association_for_to_json={}
|
11
|
+
@wams_main_object=object_main
|
12
|
+
@wams_foreign_object={}
|
13
|
+
if !except_params.empty?
|
14
|
+
@wams_except_params_main_object=except_params[:except]
|
15
|
+
else
|
16
|
+
@wams_except_params_main_object={}
|
17
|
+
end
|
18
18
|
|
19
|
-
|
19
|
+
end
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
|
21
|
+
def add_has_many(object,except_methods=[])
|
22
|
+
wams_has_many<< [object,except_methods]
|
23
|
+
end
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
def add_has_one(object,except_methods=[])
|
26
|
+
wams_has_one<<[object,except_methods]
|
27
|
+
end
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
29
|
+
def get_params_included_to_json(with_except:false)
|
30
|
+
fetch_association_objects
|
31
|
+
if with_except
|
32
|
+
{include:wams_association_for_to_json,except:wams_except_params_main_object}
|
33
|
+
else
|
34
|
+
{include:wams_association_for_to_json}
|
35
|
+
end
|
35
36
|
end
|
36
|
-
end
|
37
37
|
|
38
|
-
|
38
|
+
def add_foreign_object(objects,custom_name,include_params,except_params=[]) # ezafe kardane object khareji be object asli
|
39
39
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
40
|
+
if include_params.any?
|
41
|
+
if include_params.kind_of?(Hash)
|
42
|
+
param_for_include=include_params.keys
|
43
|
+
elsif (include_params.kind_of?(Array))
|
44
44
|
|
45
|
-
|
45
|
+
param_for_include=include_params
|
46
|
+
end
|
47
|
+
objects=objects.includes(param_for_include)
|
46
48
|
end
|
47
|
-
puts param_for_include
|
48
|
-
objects=objects.includes(param_for_include)
|
49
|
-
end
|
50
|
-
|
51
|
-
if except_params.empty?
|
52
|
-
wams_foreign_object[custom_name]=objects
|
53
|
-
else
|
54
|
-
wams_foreign_object[custom_name]=objects.as_json(except:except_params,include:include_params)
|
55
|
-
end
|
56
49
|
|
57
|
-
|
50
|
+
if except_params.empty?
|
51
|
+
wams_foreign_object[custom_name]=objects
|
52
|
+
else
|
53
|
+
wams_foreign_object[custom_name]=objects.as_json(except:except_params,include:include_params)
|
54
|
+
end
|
58
55
|
|
59
|
-
def json_serializer(object=nil)
|
60
|
-
if object.nil?
|
61
|
-
included_data=includes_to_serializer
|
62
56
|
end
|
63
57
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
response_data= object.as_json({include:wams_association_for_to_json,except:wams_except_params_main_object})
|
68
|
-
else # haven't object for proccess
|
69
|
-
response_data= included_data.as_json({include:wams_association_for_to_json,except:wams_except_params_main_object})
|
58
|
+
def json_serializer(object=nil)
|
59
|
+
if object.nil?
|
60
|
+
included_data=includes_to_serializer
|
70
61
|
end
|
71
62
|
|
72
|
-
|
63
|
+
if !wams_except_params_main_object.empty? # have except params for main object
|
64
|
+
|
65
|
+
if !object.nil? # have object for proccess
|
66
|
+
response_data= object.as_json({include:wams_association_for_to_json,except:wams_except_params_main_object})
|
67
|
+
else # haven't object for proccess
|
68
|
+
response_data= included_data.as_json({include:wams_association_for_to_json,except:wams_except_params_main_object})
|
69
|
+
end
|
73
70
|
|
74
|
-
if !object.nil?
|
75
|
-
response_data= object.as_json({include:wams_association_for_to_json})
|
76
71
|
else
|
77
|
-
|
72
|
+
|
73
|
+
if !object.nil?
|
74
|
+
response_data= object.as_json({include:wams_association_for_to_json})
|
75
|
+
else
|
76
|
+
response_data= included_data.as_json({include:wams_association_for_to_json})
|
77
|
+
end
|
78
78
|
end
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
79
|
+
if wams_foreign_object.any? # when use method foreign_object
|
80
|
+
if wams_main_object.kind_of?(Class)
|
81
|
+
name_main_object=wams_main_object.to_s.downcase.pluralize
|
82
|
+
else
|
83
|
+
name_main_object=wams_main_object.klass.name.downcase.pluralize
|
84
|
+
end
|
85
|
+
return {"#{name_main_object}": response_data,included:wams_foreign_object}
|
86
|
+
|
83
87
|
else
|
84
|
-
|
88
|
+
response_data
|
85
89
|
end
|
86
|
-
return {"#{name_main_object}": response_data,included:wams_foreign_object}
|
87
90
|
|
88
|
-
|
89
|
-
response_data
|
91
|
+
# example output : wams_association_for_to_json is {'house_images':{except: [:id,:house_id]} }
|
90
92
|
end
|
91
93
|
|
92
|
-
|
93
|
-
|
94
|
+
def get_object_included
|
95
|
+
return includes_to_serializer
|
96
|
+
end
|
94
97
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
+
private
|
99
|
+
attr_accessor :wams_has_many,:wams_has_one ,:wams_association_object,
|
100
|
+
:wams_except_params,:wams_main_object,:wams_association_for_to_json,
|
101
|
+
:wams_except_params_main_object,:wams_foreign_object
|
98
102
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
:wams_except_params_main_object,:wams_foreign_object
|
103
|
+
def includes_to_serializer
|
104
|
+
data_with_includes=nil
|
105
|
+
fetch_association_objects
|
103
106
|
|
104
|
-
|
105
|
-
data_with_includes=nil
|
106
|
-
fetch_association_objects
|
107
|
-
if wams_has_many.any?
|
108
|
-
data_with_includes= wams_main_object.includes(wams_association_object)
|
109
|
-
end
|
110
|
-
if wams_has_one.any?
|
111
|
-
data_with_includes.merge! wams_main_object.includes(wams_association_object)
|
112
|
-
end
|
113
|
-
if wams_has_one.empty? && wams_has_one.empty? # when main object not any has_one and has_many
|
114
|
-
return wams_main_object # return main object
|
115
|
-
end
|
116
|
-
return data_with_includes
|
117
|
-
end
|
107
|
+
if wams_has_many.any?
|
118
108
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
109
|
+
data_with_includes= wams_main_object.includes(wams_association_object)
|
110
|
+
end
|
111
|
+
if wams_has_one.any?
|
112
|
+
data_with_includes.merge! wams_main_object.includes(wams_association_object)
|
113
|
+
end
|
114
|
+
|
115
|
+
if wams_has_one.empty? && wams_has_many.empty? # when main object not any has_one and has_many
|
116
|
+
return wams_main_object # return main object
|
117
|
+
end
|
118
|
+
|
119
|
+
return data_with_includes
|
123
120
|
end
|
124
121
|
|
125
|
-
|
126
|
-
|
127
|
-
|
122
|
+
def fetch_association_objects
|
123
|
+
wams_has_many.map do |object_asoc,except_params|
|
124
|
+
wams_association_for_to_json[object_asoc]={except: except_params} # for to_json hash
|
125
|
+
wams_association_object<<object_asoc # for included (relation record)
|
126
|
+
end
|
127
|
+
|
128
|
+
wams_has_one.map do |object_asoc,except_params|
|
129
|
+
wams_association_for_to_json[object_asoc]={except: except_params} # for to_json hash
|
130
|
+
wams_association_object<<object_asoc # for included (relation record)
|
131
|
+
end
|
128
132
|
end
|
129
|
-
end
|
130
133
|
|
131
|
-
|
132
|
-
end
|
134
|
+
end
|
135
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mini_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- mahdi alizadeh
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-10-
|
11
|
+
date: 2018-10-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|