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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fbe022f9c41cd2780b9af9b6d139fe3af61705f675ed97c858f61b9c18943476
4
- data.tar.gz: 462ca7ab0f6ea07066887905d906e9a8e2f83060dc3390907b0b1270461549ff
3
+ metadata.gz: 2619c6668f462b9b5e58d9c8ad18fe638608261686149caff6a68fe332fcc649
4
+ data.tar.gz: 28b7d74b1b5b8e44f8c6d0f19edfb9a0f0926c6329d8a136f4b4471261a7e3ec
5
5
  SHA512:
6
- metadata.gz: 5b95cc252fc99a076c9c7a7933fa3c44965d9fd4258adf8e0feb88ac098bf98f5c92b1d9f50194a1301be58ab6a59896536923c548c0e5cb0dce5fe62bf4b5e8
7
- data.tar.gz: 9df3651371370e4087fdd089703e7b417d21623bdee13b6813beb054898574bb7dfecf0cbee50bdbeada8e7b467785d6e3b68cc9719a2aa261d9677dac724b1a
6
+ metadata.gz: f996b4f6b2b42de72c0d989ae49b83f40194a4537b523c1f2391e6be8b34fa7d85e4c50dd1d1949549ad21ffb42ca3eae9334ee57628ba38829870aad2496c91
7
+ data.tar.gz: 696ee0d331bb125d4a82d5f6a57e84566467668632b724f78d8c3e098764da9a31e4c601c5b0de45d80d9eb082439188771e838f196ab962d63ce07225b47185
@@ -1,121 +1,121 @@
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
- @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
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
- end
19
+ end
20
20
 
21
- def add_has_many(object,except_methods=[])
22
- wams_has_many.append [object,except_methods]
23
- end
21
+ def add_has_many(object,except_methods=[])
22
+ wams_has_many<< [object,except_methods]
23
+ end
24
24
 
25
- def add_has_one(object,except_methods=[])
26
- wams_has_one<<[object,except_methods]
27
- end
25
+ def add_has_one(object,except_methods=[])
26
+ wams_has_one<<[object,except_methods]
27
+ end
28
28
 
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
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
- def add_foreign_object(objects,custom_name,except_params=[]) # ezafe kardane object khareji be object asli
38
+ def add_foreign_object(objects,custom_name,except_params=[]) # ezafe kardane object khareji be object asli
39
39
 
40
- if except_params.empty?
41
- wams_foreign_object.store(custom_name,objects)
42
- else
43
- wams_foreign_object.store(custom_name,objects.as_json(except:except_params))
44
- end
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
- end
46
+ end
47
47
 
48
- def json_serializer(object=nil)
49
- if object.nil?
50
- included_data=includes_to_serializer
51
- end
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
- def get_object_included
85
- return includes_to_serializer
86
- end
53
+ if !wams_except_params_main_object.empty? # have except params for main object
87
54
 
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
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
- def fetch_association_objects
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
- wams_has_one.map do |object_asoc,except_params|
115
- wams_association_for_to_json.store(object_asoc,{except: except_params}) # for to_json hash
116
- wams_association_object<<object_asoc # for included (relation record)
117
- end
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
- end
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
@@ -1,3 +1,3 @@
1
1
  module MiniSerializer
2
- VERSION = "0.1.7".freeze
2
+ VERSION = "0.1.8".freeze
3
3
  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.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - mahdi alizadeh