mini_serializer 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e9f0c9e9a6f581e208d02b39cb6f6193586941c12a92ba9d577f25935ebb4ef6
4
- data.tar.gz: c6e4d205a6b993a4d84581472da54c9c2a14df001cf5a7bd8b2d13d68afe0705
3
+ metadata.gz: fd3d9b9566b9eda16c84c821f2b6e19795b4b5735d78719bc746c36bef3efb19
4
+ data.tar.gz: 4dc8ed3f7b9c895ea721390c16f3f6499f031977106e9299148cecde0e147b30
5
5
  SHA512:
6
- metadata.gz: e27f5de0f1d2092b7fe1fb798e1937474791d9eacd4bf865b2708ad354f81d0d85674cb1cf264038775922a99d6373638ef69b41622f04dba152a8ca2c1cd611
7
- data.tar.gz: bfc7e5e688c58668b646e73e01829913f0cd9b7fb8e550d5818c21c86bb8602c0e4c05e2e4fe966333ddcd113e4e48555d5c446b185657b6dc6dca5697781ee8
6
+ metadata.gz: '099f77dadf6a78702d303f07c4ab3d7ddcdf55dc78dab0fef7cf2033a974efe306cb9feb72d8c52e269b7eaf3d0b9ea6bb3db42653a0f0933fa220372b78559b'
7
+ data.tar.gz: 9c122ffc023f9a84437e841d66deaaaa2363310fbe78b1d15266c26387fab9bebc1ae4ba09e06cf11912653ea721b5cacc8bf917c357e68643cc44406d029376
@@ -1,3 +1,3 @@
1
1
  module MiniSerializer
2
- VERSION = "0.1.5".freeze
2
+ VERSION = "0.1.6".freeze
3
3
  end
@@ -1,101 +1,115 @@
1
1
  require "mini_serializer/version"
2
2
 
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
- 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
- def add_has_many(object,except_methods=[])
21
- wams_has_many.append [object,except_methods]
22
- end
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
- def add_has_one(object,except_methods=[])
25
- wams_has_one<<[object,except_methods]
26
- end
19
+ end
27
20
 
28
- def get_params_included_to_json(with_except:false)
29
- fetch_association_objects
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
- def json_serializer(object=nil)
38
- included_data=includes_to_serializer
39
- var_hash={}
25
+ def add_has_one(object,except_methods=[])
26
+ wams_has_one<<[object,except_methods]
27
+ end
40
28
 
41
- wams_has_many.map do |object_assosiation,except_params|
42
- var_hash.store(object_assosiation,{except: except_params})
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
- wams_has_one.map do |object_assosiation,except_params|
45
- var_hash.store(object_assosiation,{except: except_params})
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
- if !wams_except_params_main_object.empty? # have except params for main object
49
- unless object.nil?
50
- object.to_json({include:wams_association_for_to_json,except:wams_except_params_main_object})
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.to_json({include:wams_association_for_to_json,except:wams_except_params_main_object})
60
+ response_data= included_data.as_json({include:wams_association_for_to_json})
61
+ end
53
62
  end
54
- else
55
- unless object.nil?
56
- return object.to_json({include:wams_association_for_to_json})
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
- return included_data.to_json({include:wams_association_for_to_json})
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
- def get_object_included
65
- return includes_to_serializer
66
- end
78
+ def get_object_included
79
+ return includes_to_serializer
80
+ end
67
81
 
68
- private
69
- attr_accessor :wams_has_many,:wams_has_one ,:wams_association_object,
70
- :wams_except_params,:wams_main_object,:wams_association_for_to_json,
71
- :wams_except_params_main_object
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
- def includes_to_serializer
74
- data_with_includes=nil
75
- fetch_association_objects
76
- if wams_has_many.any?
77
- data_with_includes= wams_main_object.includes(wams_association_object)
78
- end
79
- if wams_has_one.any?
80
- data_with_includes.merge! wams_main_object.includes(wams_association_object)
81
- end
82
- if wams_has_one.empty? && wams_has_one.empty? # when main object not any has_one and has_many
83
- return wams_main_object # return main object
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
- def fetch_association_objects
89
- wams_has_many.map do |object_asoc,except_params|
90
- wams_association_for_to_json.store(object_asoc,{except: except_params}) # for to_json hash
91
- wams_association_object<<object_asoc # for included (relation record)
92
- end
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
- wams_has_one.map do |object_asoc,except_params|
95
- wams_association_for_to_json.store(object_asoc,{except: except_params}) # for to_json hash
96
- wams_association_object<<object_asoc # for included (relation record)
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
- end
101
- end
114
+ end
115
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - mahdi alizadeh