mini_serializer 0.1.7 → 0.1.8
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.rb +103 -103
- data/lib/mini_serializer/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2619c6668f462b9b5e58d9c8ad18fe638608261686149caff6a68fe332fcc649
|
4
|
+
data.tar.gz: 28b7d74b1b5b8e44f8c6d0f19edfb9a0f0926c6329d8a136f4b4471261a7e3ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f996b4f6b2b42de72c0d989ae49b83f40194a4537b523c1f2391e6be8b34fa7d85e4c50dd1d1949549ad21ffb42ca3eae9334ee57628ba38829870aad2496c91
|
7
|
+
data.tar.gz: 696ee0d331bb125d4a82d5f6a57e84566467668632b724f78d8c3e098764da9a31e4c601c5b0de45d80d9eb082439188771e838f196ab962d63ce07225b47185
|
data/lib/mini_serializer.rb
CHANGED
@@ -1,121 +1,121 @@
|
|
1
1
|
require "mini_serializer/version"
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
3
|
+
module MiniSerializer
|
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
|
-
|
35
|
-
|
36
|
-
|
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
|
36
|
+
end
|
37
37
|
|
38
|
-
|
38
|
+
def add_foreign_object(objects,custom_name,except_params=[]) # ezafe kardane object khareji be object asli
|
39
39
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
40
|
+
if except_params.empty?
|
41
|
+
wams_foreign_object[custom_name]=objects
|
42
|
+
else
|
43
|
+
wams_foreign_object[custom_name]=objects.as_json(except:except_params)
|
44
|
+
end
|
45
45
|
|
46
|
-
|
46
|
+
end
|
47
47
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
if !wams_except_params_main_object.empty? # have except params for main object
|
54
|
-
|
55
|
-
if !object.nil? # have object for proccess
|
56
|
-
response_data= object.as_json({include:wams_association_for_to_json,except:wams_except_params_main_object})
|
57
|
-
else # haven't object for proccess
|
58
|
-
response_data= included_data.as_json({include:wams_association_for_to_json,except:wams_except_params_main_object})
|
59
|
-
end
|
60
|
-
|
61
|
-
else
|
62
|
-
|
63
|
-
if !object.nil?
|
64
|
-
response_data= object.as_json({include:wams_association_for_to_json})
|
65
|
-
else
|
66
|
-
response_data= included_data.as_json({include:wams_association_for_to_json})
|
67
|
-
end
|
68
|
-
end
|
69
|
-
if wams_foreign_object.any? # when use method foreign_object
|
70
|
-
if wams_main_object.kind_of? Class
|
71
|
-
name_main_object=wams_main_object.to_s.downcase.pluralize
|
72
|
-
else
|
73
|
-
name_main_object=wams_main_object.klass.name.downcase.pluralize
|
74
|
-
end
|
75
|
-
return {"#{name_main_object}": response_data,included:wams_foreign_object}
|
76
|
-
|
77
|
-
else
|
78
|
-
response_data
|
79
|
-
end
|
80
|
-
|
81
|
-
# example output : wams_association_for_to_json is {'house_images':{except: [:id,:house_id]} }
|
82
|
-
end
|
48
|
+
def json_serializer(object=nil)
|
49
|
+
if object.nil?
|
50
|
+
included_data=includes_to_serializer
|
51
|
+
end
|
83
52
|
|
84
|
-
|
85
|
-
return includes_to_serializer
|
86
|
-
end
|
53
|
+
if !wams_except_params_main_object.empty? # have except params for main object
|
87
54
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
def includes_to_serializer
|
94
|
-
data_with_includes=nil
|
95
|
-
fetch_association_objects
|
96
|
-
if wams_has_many.any?
|
97
|
-
data_with_includes= wams_main_object.includes(wams_association_object)
|
98
|
-
end
|
99
|
-
if wams_has_one.any?
|
100
|
-
data_with_includes.merge! wams_main_object.includes(wams_association_object)
|
101
|
-
end
|
102
|
-
if wams_has_one.empty? && wams_has_one.empty? # when main object not any has_one and has_many
|
103
|
-
return wams_main_object # return main object
|
104
|
-
end
|
105
|
-
return data_with_includes
|
55
|
+
if !object.nil? # have object for proccess
|
56
|
+
response_data= object.as_json({include:wams_association_for_to_json,except:wams_except_params_main_object})
|
57
|
+
else # haven't object for proccess
|
58
|
+
response_data= included_data.as_json({include:wams_association_for_to_json,except:wams_except_params_main_object})
|
106
59
|
end
|
107
60
|
|
108
|
-
|
109
|
-
wams_has_many.map do |object_asoc,except_params|
|
110
|
-
wams_association_for_to_json.store(object_asoc,{except: except_params}) # for to_json hash
|
111
|
-
wams_association_object<<object_asoc # for included (relation record)
|
112
|
-
end
|
61
|
+
else
|
113
62
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
63
|
+
if !object.nil?
|
64
|
+
response_data= object.as_json({include:wams_association_for_to_json})
|
65
|
+
else
|
66
|
+
response_data= included_data.as_json({include:wams_association_for_to_json})
|
67
|
+
end
|
68
|
+
end
|
69
|
+
if wams_foreign_object.any? # when use method foreign_object
|
70
|
+
if wams_main_object.kind_of? Class
|
71
|
+
name_main_object=wams_main_object.to_s.downcase.pluralize
|
72
|
+
else
|
73
|
+
name_main_object=wams_main_object.klass.name.downcase.pluralize
|
118
74
|
end
|
75
|
+
return {"#{name_main_object}": response_data,included:wams_foreign_object}
|
119
76
|
|
77
|
+
else
|
78
|
+
response_data
|
120
79
|
end
|
121
|
-
|
80
|
+
|
81
|
+
# example output : wams_association_for_to_json is {'house_images':{except: [:id,:house_id]} }
|
82
|
+
end
|
83
|
+
|
84
|
+
def get_object_included
|
85
|
+
return includes_to_serializer
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
attr_accessor :wams_has_many,:wams_has_one ,:wams_association_object,
|
90
|
+
:wams_except_params,:wams_main_object,:wams_association_for_to_json,
|
91
|
+
:wams_except_params_main_object,:wams_foreign_object
|
92
|
+
|
93
|
+
def includes_to_serializer
|
94
|
+
data_with_includes=nil
|
95
|
+
fetch_association_objects
|
96
|
+
if wams_has_many.any?
|
97
|
+
data_with_includes= wams_main_object.includes(wams_association_object)
|
98
|
+
end
|
99
|
+
if wams_has_one.any?
|
100
|
+
data_with_includes.merge! wams_main_object.includes(wams_association_object)
|
101
|
+
end
|
102
|
+
if wams_has_one.empty? && wams_has_one.empty? # when main object not any has_one and has_many
|
103
|
+
return wams_main_object # return main object
|
104
|
+
end
|
105
|
+
return data_with_includes
|
106
|
+
end
|
107
|
+
|
108
|
+
def fetch_association_objects
|
109
|
+
wams_has_many.map do |object_asoc,except_params|
|
110
|
+
wams_association_for_to_json[object_asoc]={except: except_params} # for to_json hash
|
111
|
+
wams_association_object<<object_asoc # for included (relation record)
|
112
|
+
end
|
113
|
+
|
114
|
+
wams_has_one.map do |object_asoc,except_params|
|
115
|
+
wams_association_for_to_json[object_asoc]={except: except_params} # for to_json hash
|
116
|
+
wams_association_object<<object_asoc # for included (relation record)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
121
|
+
end
|