mini_serializer 0.1.6 → 0.1.7

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: fd3d9b9566b9eda16c84c821f2b6e19795b4b5735d78719bc746c36bef3efb19
4
- data.tar.gz: 4dc8ed3f7b9c895ea721390c16f3f6499f031977106e9299148cecde0e147b30
3
+ metadata.gz: fbe022f9c41cd2780b9af9b6d139fe3af61705f675ed97c858f61b9c18943476
4
+ data.tar.gz: 462ca7ab0f6ea07066887905d906e9a8e2f83060dc3390907b0b1270461549ff
5
5
  SHA512:
6
- metadata.gz: '099f77dadf6a78702d303f07c4ab3d7ddcdf55dc78dab0fef7cf2033a974efe306cb9feb72d8c52e269b7eaf3d0b9ea6bb3db42653a0f0933fa220372b78559b'
7
- data.tar.gz: 9c122ffc023f9a84437e841d66deaaaa2363310fbe78b1d15266c26387fab9bebc1ae4ba09e06cf11912653ea721b5cacc8bf917c357e68643cc44406d029376
6
+ metadata.gz: 5b95cc252fc99a076c9c7a7933fa3c44965d9fd4258adf8e0feb88ac098bf98f5c92b1d9f50194a1301be58ab6a59896536923c548c0e5cb0dce5fe62bf4b5e8
7
+ data.tar.gz: 9df3651371370e4087fdd089703e7b417d21623bdee13b6813beb054898574bb7dfecf0cbee50bdbeada8e7b467785d6e3b68cc9719a2aa261d9677dac724b1a
@@ -1,3 +1,3 @@
1
1
  module MiniSerializer
2
- VERSION = "0.1.6".freeze
2
+ VERSION = "0.1.7".freeze
3
3
  end
@@ -1,115 +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.append [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}
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
- def add_foreign_object(objects,custom_name) # ezafe kardane object khareji be object asli
39
- wams_foreign_object.store(custom_name,objects)
40
- end
38
+ def add_foreign_object(objects,custom_name,except_params=[]) # ezafe kardane object khareji be object asli
41
39
 
42
- def json_serializer(object=nil)
43
- if object.nil?
44
- included_data=includes_to_serializer
45
- end
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
46
45
 
47
- if !wams_except_params_main_object.empty? # have except params for main object
46
+ end
48
47
 
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})
48
+ def json_serializer(object=nil)
49
+ if object.nil?
50
+ included_data=includes_to_serializer
53
51
  end
54
52
 
55
- else
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
56
60
 
57
- if !object.nil?
58
- response_data= object.as_json({include:wams_association_for_to_json})
59
61
  else
60
- response_data= included_data.as_json({include:wams_association_for_to_json})
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
61
68
  end
62
- end
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
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
+
66
77
  else
67
- name_main_object=wams_main_object.klass.name.downcase.pluralize
78
+ response_data
68
79
  end
69
- return {"#{name_main_object}": response_data,included:wams_foreign_object}
70
80
 
71
- else
72
- response_data
81
+ # example output : wams_association_for_to_json is {'house_images':{except: [:id,:house_id]} }
73
82
  end
74
83
 
75
- # example output : wams_association_for_to_json is {'house_images':{except: [:id,:house_id]} }
76
- end
77
-
78
- def get_object_included
79
- return includes_to_serializer
80
- end
84
+ def get_object_included
85
+ return includes_to_serializer
86
+ end
81
87
 
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
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
86
92
 
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
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
98
106
  end
99
- return data_with_includes
100
- end
101
107
 
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
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
107
113
 
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)
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
111
118
  end
112
- end
113
119
 
114
- end
115
- end
120
+ end
121
+ end
@@ -26,4 +26,5 @@ Gem::Specification.new do |spec|
26
26
 
27
27
  spec.add_development_dependency "bundler", "~> 1.16"
28
28
  spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rails", "~> 5.0"
29
30
  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.6
4
+ version: 0.1.7
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-21 00:00:00.000000000 Z
11
+ date: 2018-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
41
55
  description: Serializes Activemodel object to JSON API format
42
56
  email:
43
57
  - mahdializadeh20@gmail.com