panko_serializer 0.4.3 → 0.4.4

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: fe095c4ba577a1757ae1cfde90e6847bb6064753dd34e68aaa0c16d6bb9cc844
4
- data.tar.gz: 5b3d50d6dc2191240069fa36733248197a96555a1b130c405d799d9ca982e9ce
3
+ metadata.gz: b82a221e5207cf94860579c3ae74a5a31b51a50667785f83c6d9311160096792
4
+ data.tar.gz: e8177737c068e142e2273fdfe70cb367a2b22fe841d4e3c49a63817f59383b25
5
5
  SHA512:
6
- metadata.gz: c7e73c88806d3195cf9c63aeed87ff67739cc4ff31a6f0121ebe59612627c8f4b020e64eba2aaa55d56242a616e4de177996263c7c18cb5416124e7364f32977
7
- data.tar.gz: 91505d9d99eddab0e81e05340ea324c0350507ff9fcc6db77fc9c4efc34c84bd28f9ee69d3b3e876ee23e0258ad785971c1a85893af35307c3882948eb7996b2
6
+ metadata.gz: 78f1c9ae160336f9d950d2bab9796a1b124674cc4a0e8a2e41b000b38689e4093aa3783d63e64547598e23914c2e7156921f052409efc645d0d46e06d4daddba
7
+ data.tar.gz: 44d99088e0afad8264c599026df2584d1df58b8c243540001b05f77b4caef9f110cf71b17bf08b15dfe525ef5bb9671125fefe822b5bffc4542e6ab8887e716d
data/.rubocop.yml CHANGED
@@ -1,6 +1,5 @@
1
1
  AllCops:
2
- DisabledByDefault: true
3
- TargetRubyVersion: 2.4.1
2
+ TargetRubyVersion: 2.5
4
3
  DisplayCopNames: true
5
4
  StyleGuideCopsOnly: false
6
5
 
@@ -20,3 +19,11 @@ Layout/TrailingBlankLines:
20
19
  Layout/TrailingWhitespace:
21
20
  Enabled: true
22
21
 
22
+ Style/FrozenStringLiteralComment:
23
+ Enabled: true
24
+
25
+ Style/RedundantFreeze:
26
+ Enabled: true
27
+
28
+ Performance/UnfreezeString:
29
+ Enabled: true
@@ -15,7 +15,6 @@ Please pass valid each_serializer to ArraySerializer, for example:
15
15
  }
16
16
  end
17
17
 
18
-
19
18
  serializer_options = {
20
19
  only: options.fetch(:only, []),
21
20
  except: options.fetch(:except, []),
@@ -41,7 +40,7 @@ Please pass valid each_serializer to ArraySerializer, for example:
41
40
 
42
41
  def serialize_to_json(subjects)
43
42
  writer = Oj::StringWriter.new(mode: :rails)
44
- Panko::serialize_subjects(subjects.to_a, writer, @descriptor)
43
+ Panko.serialize_subjects(subjects.to_a, writer, @descriptor)
45
44
  writer.to_s
46
45
  end
47
46
  end
@@ -9,9 +9,7 @@ module Panko
9
9
 
10
10
  def ==(attr)
11
11
  return name.to_sym == attr if attr.is_a? Symbol
12
- if attr.is_a? Panko::Attribute
13
- return name == attr.name && alias_name == attr.alias_name
14
- end
12
+ return name == attr.name && alias_name == attr.alias_name if attr.is_a? Panko::Attribute
15
13
  super
16
14
  end
17
15
 
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require "oj"
3
4
 
4
5
  module Panko
@@ -6,6 +7,28 @@ module Panko
6
7
  def self.from(value)
7
8
  JsonValue.new(value)
8
9
  end
10
+
11
+ def to_json
12
+ value
13
+ end
14
+ end
15
+
16
+ class ResponseCreator
17
+ def self.value(value)
18
+ Panko::Response.new(value)
19
+ end
20
+
21
+ def self.json(value)
22
+ Panko::JsonValue.from(value)
23
+ end
24
+
25
+ def self.array_serializer(data, serializer)
26
+ Panko::ArraySerializer.new(data, each_serializer: serializer)
27
+ end
28
+
29
+ def self.serializer(data, serializer)
30
+ json serializer.new.serialize_to_json(data)
31
+ end
9
32
  end
10
33
 
11
34
  class Response
@@ -13,42 +36,51 @@ module Panko
13
36
  @data = data
14
37
  end
15
38
 
16
- def to_json(options = nil)
39
+ def to_json(_options = nil)
17
40
  writer = Oj::StringWriter.new(mode: :rails)
41
+ write(writer, @data)
42
+ writer.to_s
43
+ end
18
44
 
19
- writer.push_object
45
+ def self.create
46
+ Response.new(yield ResponseCreator)
47
+ end
20
48
 
21
- @data.each do |key, value|
22
- key = key.to_s
49
+ private
23
50
 
24
- if value.is_a? Array
25
- writer.push_array key
26
- value.each { |v| write_object(writer, v) }
27
- writer.pop
28
- next
29
- end
51
+ def write(writer, data, key = nil)
52
+ return write_array(writer, data, key) if data.is_a?(Array)
30
53
 
31
- write_object writer, value, key
54
+ return write_object(writer, data) if data.is_a?(Hash)
32
55
 
33
- end
56
+ write_value(writer, data, key)
57
+ end
34
58
 
59
+ def write_array(writer, value, key = nil)
60
+ writer.push_array key
61
+ value.each { |v| write(writer, v) }
35
62
  writer.pop
36
-
37
- writer.to_s
38
63
  end
39
64
 
40
- private
41
-
42
65
  def write_object(writer, value, key = nil)
43
- if value.is_a?(Panko::ArraySerializer) ||
44
- value.is_a?(Panko::Serializer) ||
45
- value.is_a?(Panko::Response)
46
- writer.push_json(value.to_json, key)
47
- elsif value.is_a?(Panko::JsonValue)
48
- writer.push_json(value.value, key)
49
- else
50
- writer.push_value(value, key)
51
- end
66
+ writer.push_object key
67
+
68
+ value.each do |entry_key, entry_value|
69
+ write(writer, entry_value, entry_key.to_s)
70
+ end
71
+
72
+ writer.pop
73
+ end
74
+
75
+ def write_value(writer, value, key = nil)
76
+ if value.is_a?(Panko::ArraySerializer) ||
77
+ value.is_a?(Panko::Serializer) ||
78
+ value.is_a?(Panko::Response) ||
79
+ value.is_a?(Panko::JsonValue)
80
+ writer.push_json(value.to_json, key)
81
+ else
82
+ writer.push_value(value, key)
83
+ end
52
84
  end
53
85
  end
54
86
  end
@@ -1,16 +1,15 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Panko
3
4
  class SerializationDescriptor
4
5
  #
5
6
  # Creates new description and apply the options
6
7
  # on the new descriptor
7
8
  #
8
- def self.build(serializer, options={})
9
+ def self.build(serializer, options = {})
9
10
  backend = Panko::SerializationDescriptor.duplicate(serializer._descriptor)
10
11
 
11
- if serializer.respond_to? :filters_for
12
- options.merge! serializer.filters_for(options[:context], options[:scope])
13
- end
12
+ options.merge! serializer.filters_for(options[:context], options[:scope]) if serializer.respond_to? :filters_for
14
13
 
15
14
  backend.context = options[:context]
16
15
  backend.scope = options[:scope]
@@ -32,9 +31,7 @@ module Panko
32
31
 
33
32
  backend.method_fields = descriptor.method_fields.dup
34
33
 
35
- unless descriptor.serializer.nil?
36
- backend.serializer = descriptor.serializer.reset
37
- end
34
+ backend.serializer = descriptor.serializer.reset unless descriptor.serializer.nil?
38
35
 
39
36
  backend.has_many_associations = descriptor.has_many_associations.dup
40
37
  backend.has_one_associations = descriptor.has_one_associations.dup
@@ -55,30 +52,30 @@ module Panko
55
52
  attributes_except_filters, associations_except_filters = resolve_filters(options, :except)
56
53
 
57
54
  self.attributes = apply_attribute_filters(
58
- self.attributes,
55
+ attributes,
59
56
  attributes_only_filters,
60
57
  attributes_except_filters
61
58
  )
62
59
 
63
60
  self.method_fields = apply_fields_filters(
64
- self.method_fields,
61
+ method_fields,
65
62
  attributes_only_filters,
66
63
  attributes_except_filters
67
64
  )
68
65
 
69
- unless self.has_many_associations.empty?
66
+ unless has_many_associations.empty?
70
67
  self.has_many_associations = apply_association_filters(
71
- self.has_many_associations,
68
+ has_many_associations,
72
69
  { attributes: attributes_only_filters, associations: associations_only_filters },
73
- { attributes: attributes_except_filters, associations: associations_except_filters }
70
+ attributes: attributes_except_filters, associations: associations_except_filters
74
71
  )
75
72
  end
76
73
 
77
- unless self.has_one_associations.empty?
74
+ unless has_one_associations.empty?
78
75
  self.has_one_associations = apply_association_filters(
79
- self.has_one_associations,
76
+ has_one_associations,
80
77
  { attributes: attributes_only_filters, associations: associations_only_filters },
81
- { attributes: attributes_except_filters, associations: associations_except_filters }
78
+ attributes: attributes_except_filters, associations: associations_except_filters
82
79
  )
83
80
  end
84
81
  end
@@ -91,7 +88,6 @@ module Panko
91
88
  end
92
89
  end
93
90
 
94
-
95
91
  attributes_except_filters = except_filters[:attributes] || []
96
92
  unless attributes_except_filters.empty?
97
93
  associations.reject! do |association|
@@ -99,13 +95,10 @@ module Panko
99
95
  end
100
96
  end
101
97
 
102
-
103
98
  associations_only_filters = only_filters[:associations]
104
99
  associations_except_filters = except_filters[:associations]
105
100
 
106
- if associations_only_filters.empty? && associations_except_filters.empty?
107
- return associations
108
- end
101
+ return associations if associations_only_filters.empty? && associations_except_filters.empty?
109
102
 
110
103
  associations.map do |association|
111
104
  name = association.name_sym
@@ -132,9 +125,7 @@ module Panko
132
125
 
133
126
  def resolve_filters(options, filter)
134
127
  filters = options.fetch(filter, {})
135
- if filters.is_a? Array
136
- return filters, {}
137
- end
128
+ return filters, {} if filters.is_a? Array
138
129
 
139
130
  # hash filters looks like this
140
131
  # { instance: [:a], foo: [:b] }
@@ -146,7 +137,7 @@ module Panko
146
137
  attributes_filters = filters.fetch(:instance, [])
147
138
  association_filters = filters.except(:instance)
148
139
 
149
- return attributes_filters, association_filters
140
+ [attributes_filters, association_filters]
150
141
  end
151
142
 
152
143
  def apply_fields_filters(fields, only, except)
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require_relative "serialization_descriptor"
3
4
  require "oj"
4
5
 
@@ -6,10 +7,7 @@ module Panko
6
7
  class Serializer
7
8
  class << self
8
9
  def inherited(base)
9
-
10
- unless _descriptor.nil?
11
- base._descriptor = Panko::SerializationDescriptor.duplicate(_descriptor)
12
- else
10
+ if _descriptor.nil?
13
11
  base._descriptor = Panko::SerializationDescriptor.new
14
12
 
15
13
  base._descriptor.attributes = []
@@ -19,9 +17,10 @@ module Panko
19
17
 
20
18
  base._descriptor.has_many_associations = []
21
19
  base._descriptor.has_one_associations = []
20
+ else
21
+ base._descriptor = Panko::SerializationDescriptor.duplicate(_descriptor)
22
22
  end
23
23
  base._descriptor.type = base
24
-
25
24
  end
26
25
 
27
26
  attr_accessor :_descriptor
@@ -39,18 +38,14 @@ module Panko
39
38
  def method_added(method)
40
39
  return if @_descriptor.nil?
41
40
  deleted_attr = @_descriptor.attributes.delete(method)
42
- unless deleted_attr.nil?
43
- @_descriptor.method_fields << method
44
- end
41
+ @_descriptor.method_fields << method unless deleted_attr.nil?
45
42
  end
46
43
 
47
44
  def has_one(name, options = {})
48
45
  serializer_const = options[:serializer]
49
46
  serializer_const = Panko::SerializerResolver.resolve(name.to_s) if serializer_const.nil?
50
47
 
51
- if serializer_const.nil?
52
- raise "Can't find serializer for #{self.name}.#{name} has_one relationship."
53
- end
48
+ raise "Can't find serializer for #{self.name}.#{name} has_one relationship." if serializer_const.nil?
54
49
 
55
50
  @_descriptor.has_one_associations << Panko::Association.new(
56
51
  name,
@@ -63,9 +58,7 @@ module Panko
63
58
  serializer_const = options[:serializer] || options[:each_serializer]
64
59
  serializer_const = Panko::SerializerResolver.resolve(name.to_s) if serializer_const.nil?
65
60
 
66
- if serializer_const.nil?
67
- raise "Can't find serializer for #{self.name}.#{name} has_many relationship."
68
- end
61
+ raise "Can't find serializer for #{self.name}.#{name} has_many relationship." if serializer_const.nil?
69
62
 
70
63
  @_descriptor.has_many_associations << Panko::Association.new(
71
64
  name,
@@ -89,7 +82,7 @@ module Panko
89
82
 
90
83
  def serialize_to_json(object)
91
84
  writer = Oj::StringWriter.new(mode: :rails)
92
- Panko::serialize_subject(object, writer, @descriptor)
85
+ Panko.serialize_subject(object, writer, @descriptor)
93
86
  writer.to_s
94
87
  end
95
88
 
@@ -1,18 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Panko::SerializerResolver
4
-
5
4
  def self.resolve(name)
6
5
  serializer_name = "#{name.singularize.camelize}Serializer"
7
- serializer_const = self.safe_const_get(serializer_name)
6
+ serializer_const = safe_const_get(serializer_name)
8
7
 
9
8
  return nil if serializer_const.nil?
10
- return nil unless self.is_serializer(serializer_const)
9
+ return nil unless is_serializer(serializer_const)
11
10
 
12
11
  serializer_const
13
12
  end
14
13
 
15
-
16
14
  private
17
15
 
18
16
  def self.is_serializer(const)
@@ -24,5 +22,4 @@ class Panko::SerializerResolver
24
22
  rescue NameError
25
23
  nil
26
24
  end
27
-
28
25
  end
data/lib/panko/version.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Panko
3
- VERSION = "0.4.3"
4
+ VERSION = "0.4.4"
4
5
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require "panko/version"
3
4
  require "panko/attribute"
4
5
  require "panko/serializer"
@@ -6,7 +7,6 @@ require "panko/array_serializer"
6
7
  require "panko/response"
7
8
  require "panko/serializer_resolver"
8
9
 
9
-
10
10
  # C Extension
11
11
  require "oj"
12
12
  require "panko/panko_serializer"
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.4.3
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yosi Attias
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-16 00:00:00.000000000 Z
11
+ date: 2018-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj