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