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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 73b05629bbbeb18415188b8c70e316f678f604ba
4
- data.tar.gz: 95922dc138d747c5fa0ca282ff6b9eda4e004d55
3
+ metadata.gz: 191ab315beed684a1a584eccdcfb500521e5f3b5
4
+ data.tar.gz: bdc4bf8c0bdc1e66b09a0221d640ec033b21463c
5
5
  SHA512:
6
- metadata.gz: 1b6489dc3d9532d01766a38cbb6d0652275883d8ea8ad7b8dc6b82494f1d5035f5c95a925b2b0908bdcaf3a6807ec7afaca3c5982a1a894e102ac9aa7ae35243
7
- data.tar.gz: 83c2b331d7acb26d4bbf6e851b5f30250b7074195c2f8a722bea053db867aa34af23a05dc015a0ceec098cfbc747b9f892516f9a8d534c50f5189180bcd5cf82
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 = SerializationDescriptorBuilder.build(@each_serializer, serializer_options)
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
- module SerializationDescriptorBuilder
4
- def self.apply_filters(backend, options)
5
- serializer_only_filters, attributes_only_filters = resolve_filters(options, :only)
6
- serializer_except_filters, attributes_except_filters = resolve_filters(options, :except)
7
-
8
- backend.fields = apply_fields_filters(
9
- backend.fields,
10
- serializer_only_filters,
11
- serializer_except_filters
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
- def self.build(serializer, options={})
56
- backend = duplicate(serializer._descriptor)
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
- apply_filters(backend, options)
39
+ self.fields = apply_fields_filters(
40
+ self.fields,
41
+ attributes_only_filters,
42
+ attributes_except_filters
43
+ )
59
44
 
60
- backend
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 self.build_associations(associations, attributes_only_filters, attributes_except_filters)
64
- associations.each do |association|
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
- descriptor = association.last
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
- options = {
70
- only: attributes_only_filters.fetch(name, []),
71
- except: attributes_except_filters.fetch(name, [])
72
- }
77
+ descriptor.apply_filters({
78
+ only: only_filters[:associations].fetch(name, []),
79
+ except: except_filters[:associations].fetch(name, [])
80
+ })
73
81
 
74
- apply_filters(descriptor, options)
75
- end
82
+ association
83
+ end.compact
76
84
  end
77
85
 
78
- def self.resolve_filters(options, filter)
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
- serializer_filters = filters.fetch(:instance, [])
99
+ attributes_filters = filters.fetch(:instance, [])
92
100
  association_filters = filters.except(:instance)
93
101
 
94
- return serializer_filters, association_filters
102
+ return attributes_filters, association_filters
95
103
  end
96
104
 
97
- def self.apply_fields_filters(fields, only, except)
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)
@@ -28,26 +28,26 @@ module Panko
28
28
  end
29
29
 
30
30
  def has_one(name, options)
31
- serializer_const = SerializationDescriptorBuilder.resolve_serializer(options[:serializer])
31
+ serializer_const = Panko::SerializationDescriptor.resolve_serializer(options[:serializer])
32
32
 
33
33
  @_descriptor.has_one_associations << [
34
34
  name,
35
- SerializationDescriptorBuilder.build(serializer_const, options)
35
+ Panko::SerializationDescriptor.build(serializer_const, options)
36
36
  ]
37
37
  end
38
38
 
39
39
  def has_many(name, options)
40
- serializer_const = SerializationDescriptorBuilder.resolve_serializer(options[:serializer])
40
+ serializer_const = Panko::SerializationDescriptor.resolve_serializer(options[:serializer])
41
41
 
42
42
  @_descriptor.has_many_associations << [
43
43
  name,
44
- SerializationDescriptorBuilder.build(serializer_const, options)
44
+ Panko::SerializationDescriptor.build(serializer_const, options)
45
45
  ]
46
46
  end
47
47
  end
48
48
 
49
49
  def initialize(options = {})
50
- @descriptor = SerializationDescriptorBuilder.build(self.class, options)
50
+ @descriptor = Panko::SerializationDescriptor.build(self.class, options)
51
51
  @context = options[:context]
52
52
  end
53
53
 
data/lib/panko/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Panko
3
- VERSION = "0.1.5"
3
+ VERSION = "0.1.6"
4
4
  end
@@ -26,5 +26,5 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency "rspec", "~> 3.0"
27
27
  spec.add_development_dependency "rake-compiler"
28
28
 
29
- spec.add_dependency "oj", "~> 3.2.0"
29
+ spec.add_dependency "oj", "~> 3.3.0"
30
30
  end
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.5
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-15 00:00:00.000000000 Z
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.2.0
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.2.0
82
+ version: 3.3.0
83
83
  description:
84
84
  email:
85
85
  - yosy101@gmail.com