panko_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 +4 -4
- data/lib/panko/array_serializer.rb +1 -1
- data/lib/panko/serialization_descriptor.rb +61 -64
- data/lib/panko/serializer.rb +5 -5
- data/lib/panko/version.rb +1 -1
- data/panko_serializer.gemspec +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 191ab315beed684a1a584eccdcfb500521e5f3b5
|
4
|
+
data.tar.gz: bdc4bf8c0bdc1e66b09a0221d640ec033b21463c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1075d2458fc18945ece197e3c1b45ed19d7c37cfcad48beed72ee38d0b147b1a338b756ddce9a740d24681192a3a7159953daee5816d182678921d5c818b72ca
|
7
|
+
data.tar.gz: 278b97815ce09041391744fd01c385e92b0b8bcc54edeecc1250a7f9cc459f19fe5df0d43d77e955e5b6d717677fd81ee67baaa2c5dc87da4b68551da28b921e
|
@@ -14,7 +14,7 @@ module Panko
|
|
14
14
|
except: options.fetch(:except, [])
|
15
15
|
}
|
16
16
|
|
17
|
-
@descriptor =
|
17
|
+
@descriptor = Panko::SerializationDescriptor.build(@each_serializer, serializer_options)
|
18
18
|
@context = options.fetch(:context, nil)
|
19
19
|
end
|
20
20
|
|
@@ -1,43 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
module Panko
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
)
|
13
|
-
|
14
|
-
backend.method_fields = apply_fields_filters(
|
15
|
-
backend.method_fields,
|
16
|
-
serializer_only_filters,
|
17
|
-
serializer_except_filters
|
18
|
-
)
|
19
|
-
|
20
|
-
backend.has_many_associations = build_associations(
|
21
|
-
apply_association_filters(
|
22
|
-
backend.has_many_associations,
|
23
|
-
serializer_only_filters,
|
24
|
-
serializer_except_filters
|
25
|
-
),
|
26
|
-
attributes_only_filters,
|
27
|
-
attributes_except_filters
|
28
|
-
)
|
29
|
-
|
30
|
-
backend.has_one_associations = build_associations(
|
31
|
-
apply_association_filters(
|
32
|
-
backend.has_one_associations,
|
33
|
-
serializer_only_filters,
|
34
|
-
serializer_except_filters
|
35
|
-
),
|
36
|
-
attributes_only_filters,
|
37
|
-
attributes_except_filters
|
38
|
-
)
|
3
|
+
class SerializationDescriptor
|
4
|
+
#
|
5
|
+
# Creates new description and apply the options
|
6
|
+
# on the new descriptor
|
7
|
+
#
|
8
|
+
def self.build(serializer, options={})
|
9
|
+
backend = Panko::SerializationDescriptor.duplicate(serializer._descriptor)
|
10
|
+
backend.apply_filters(options)
|
11
|
+
backend
|
39
12
|
end
|
40
13
|
|
14
|
+
#
|
15
|
+
# Create new descriptor with same properties
|
16
|
+
# useful when you want to apply filters
|
17
|
+
#
|
41
18
|
def self.duplicate(descriptor)
|
42
19
|
backend = Panko::SerializationDescriptor.new
|
43
20
|
|
@@ -52,30 +29,61 @@ module Panko
|
|
52
29
|
backend
|
53
30
|
end
|
54
31
|
|
55
|
-
|
56
|
-
|
32
|
+
#
|
33
|
+
# Applies attributes and association filters
|
34
|
+
#
|
35
|
+
def apply_filters(options)
|
36
|
+
attributes_only_filters, associations_only_filters = resolve_filters(options, :only)
|
37
|
+
attributes_except_filters, associations_except_filters = resolve_filters(options, :except)
|
57
38
|
|
58
|
-
|
39
|
+
self.fields = apply_fields_filters(
|
40
|
+
self.fields,
|
41
|
+
attributes_only_filters,
|
42
|
+
attributes_except_filters
|
43
|
+
)
|
59
44
|
|
60
|
-
|
45
|
+
self.method_fields = apply_fields_filters(
|
46
|
+
self.method_fields,
|
47
|
+
attributes_only_filters,
|
48
|
+
attributes_except_filters
|
49
|
+
)
|
50
|
+
|
51
|
+
self.has_many_associations = apply_association_filters(
|
52
|
+
self.has_many_associations,
|
53
|
+
{ attributes: attributes_only_filters, associations: associations_only_filters },
|
54
|
+
{ attributes: attributes_except_filters, associations: associations_except_filters }
|
55
|
+
)
|
56
|
+
|
57
|
+
self.has_one_associations = apply_association_filters(
|
58
|
+
self.has_one_associations,
|
59
|
+
{ attributes: attributes_only_filters, associations: associations_only_filters },
|
60
|
+
{ attributes: attributes_except_filters, associations: associations_except_filters }
|
61
|
+
)
|
61
62
|
end
|
62
63
|
|
63
|
-
def
|
64
|
-
|
64
|
+
def apply_association_filters(associations, only_filters, except_filters)
|
65
|
+
attributes_only_filters = only_filters[:attributes]
|
66
|
+
attributes_only_filters = associations.map(&:first) if attributes_only_filters.empty?
|
67
|
+
attributes_except_filters = except_filters[:attributes] || []
|
68
|
+
|
69
|
+
|
70
|
+
associations.map do |association|
|
65
71
|
name = association.first
|
66
|
-
|
72
|
+
next if attributes_except_filters.include? name
|
73
|
+
next unless attributes_only_filters.include? name
|
67
74
|
|
75
|
+
descriptor = association.last
|
68
76
|
|
69
|
-
|
70
|
-
only:
|
71
|
-
except:
|
72
|
-
}
|
77
|
+
descriptor.apply_filters({
|
78
|
+
only: only_filters[:associations].fetch(name, []),
|
79
|
+
except: except_filters[:associations].fetch(name, [])
|
80
|
+
})
|
73
81
|
|
74
|
-
|
75
|
-
end
|
82
|
+
association
|
83
|
+
end.compact
|
76
84
|
end
|
77
85
|
|
78
|
-
def
|
86
|
+
def resolve_filters(options, filter)
|
79
87
|
filters = options.fetch(filter, {})
|
80
88
|
if filters.is_a? Array
|
81
89
|
return filters, {}
|
@@ -88,30 +96,19 @@ module Panko
|
|
88
96
|
|
89
97
|
return [], {} if filters.empty?
|
90
98
|
|
91
|
-
|
99
|
+
attributes_filters = filters.fetch(:instance, [])
|
92
100
|
association_filters = filters.except(:instance)
|
93
101
|
|
94
|
-
return
|
102
|
+
return attributes_filters, association_filters
|
95
103
|
end
|
96
104
|
|
97
|
-
def
|
105
|
+
def apply_fields_filters(fields, only, except)
|
98
106
|
return fields & only unless only.empty?
|
99
107
|
return fields - except unless except.empty?
|
100
108
|
|
101
109
|
fields
|
102
110
|
end
|
103
111
|
|
104
|
-
def self.apply_association_filters(associations, only, except)
|
105
|
-
unless only.empty?
|
106
|
-
return associations.select { |assoc| only.include?(assoc.first) }
|
107
|
-
end
|
108
|
-
|
109
|
-
unless except.empty?
|
110
|
-
return associations.reject { |assoc| except.include?(assoc.first) }
|
111
|
-
end
|
112
|
-
|
113
|
-
associations
|
114
|
-
end
|
115
112
|
|
116
113
|
def self.resolve_serializer(serializer)
|
117
114
|
Object.const_get(serializer.name)
|
data/lib/panko/serializer.rb
CHANGED
@@ -28,26 +28,26 @@ module Panko
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def has_one(name, options)
|
31
|
-
serializer_const =
|
31
|
+
serializer_const = Panko::SerializationDescriptor.resolve_serializer(options[:serializer])
|
32
32
|
|
33
33
|
@_descriptor.has_one_associations << [
|
34
34
|
name,
|
35
|
-
|
35
|
+
Panko::SerializationDescriptor.build(serializer_const, options)
|
36
36
|
]
|
37
37
|
end
|
38
38
|
|
39
39
|
def has_many(name, options)
|
40
|
-
serializer_const =
|
40
|
+
serializer_const = Panko::SerializationDescriptor.resolve_serializer(options[:serializer])
|
41
41
|
|
42
42
|
@_descriptor.has_many_associations << [
|
43
43
|
name,
|
44
|
-
|
44
|
+
Panko::SerializationDescriptor.build(serializer_const, options)
|
45
45
|
]
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
49
|
def initialize(options = {})
|
50
|
-
@descriptor =
|
50
|
+
@descriptor = Panko::SerializationDescriptor.build(self.class, options)
|
51
51
|
@context = options[:context]
|
52
52
|
end
|
53
53
|
|
data/lib/panko/version.rb
CHANGED
data/panko_serializer.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: panko_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yosi Attias
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-09-
|
11
|
+
date: 2017-09-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -72,14 +72,14 @@ dependencies:
|
|
72
72
|
requirements:
|
73
73
|
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: 3.
|
75
|
+
version: 3.3.0
|
76
76
|
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: 3.
|
82
|
+
version: 3.3.0
|
83
83
|
description:
|
84
84
|
email:
|
85
85
|
- yosy101@gmail.com
|