mini_serializer 0.1.6 → 0.1.7
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 +93 -87
- data/mini_serializer.gemspec +1 -0
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fbe022f9c41cd2780b9af9b6d139fe3af61705f675ed97c858f61b9c18943476
|
4
|
+
data.tar.gz: 462ca7ab0f6ea07066887905d906e9a8e2f83060dc3390907b0b1270461549ff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5b95cc252fc99a076c9c7a7933fa3c44965d9fd4258adf8e0feb88ac098bf98f5c92b1d9f50194a1301be58ab6a59896536923c548c0e5cb0dce5fe62bf4b5e8
|
7
|
+
data.tar.gz: 9df3651371370e4087fdd089703e7b417d21623bdee13b6813beb054898574bb7dfecf0cbee50bdbeada8e7b467785d6e3b68cc9719a2aa261d9677dac724b1a
|
data/lib/mini_serializer.rb
CHANGED
@@ -1,115 +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.append [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
|
-
|
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
|
-
|
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
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
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
|
-
|
46
|
+
end
|
48
47
|
|
49
|
-
|
50
|
-
|
51
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
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
|
-
|
78
|
+
response_data
|
68
79
|
end
|
69
|
-
return {"#{name_main_object}": response_data,included:wams_foreign_object}
|
70
80
|
|
71
|
-
|
72
|
-
response_data
|
81
|
+
# example output : wams_association_for_to_json is {'house_images':{except: [:id,:house_id]} }
|
73
82
|
end
|
74
83
|
|
75
|
-
|
76
|
-
|
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
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
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
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
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
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
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
|
-
|
109
|
-
|
110
|
-
|
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
|
-
|
115
|
-
|
120
|
+
end
|
121
|
+
end
|
data/mini_serializer.gemspec
CHANGED
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.
|
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-
|
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
|