fast_serializer_ruby 0.1.4 → 0.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2fb6eb91b6bb055330cdbc9c985ae9adcbabec5c0b03c201c5d2e131a58dbd7d
4
- data.tar.gz: 3f2ecb8143d86959d6539e625aa606b101eda57c4d32ae1b38760184e5b63e6c
3
+ metadata.gz: 4226b5f0bc68c749226425f93db90d60bfe5b0cba0faa74a77e9cfd24680a73b
4
+ data.tar.gz: 697304fc0c88cdd9848c974027689c5eb4a0f90daa3e95fb4c2d2e723ceb8040
5
5
  SHA512:
6
- metadata.gz: a895d4646f7f341411c7d52950ac9c28a34a809d5dc064ed4a9c7a26bf59527389596e3f9223a4e38aad21aba50a6b0d100e1132b2138f299c62b07ca0bdfef1
7
- data.tar.gz: c8c237765f1c08cecf753483b878ecdb360fcdf03982a93270832872097389169030e22432a74f20f42b07b5e7e20b348f744235c8629921e28fd1521aab6a45
6
+ metadata.gz: ad76357998ec5b0de5768057e6dd6191f4cc8f7eba5dfc9170206245570ad725ba822f1bbf5d35dfed5364227f6dd28130aaadc70717158f42d80d906f76f98b
7
+ data.tar.gz: 2a43e81fe55e7d4a8dd56b56a489030d38f4d810564b332ee0f965b0d16b3152622a5aab97779dfa45b8cfe276640b16da6fd1eb7c81c4a629bbb82939ab6e05
data/.travis.yml CHANGED
@@ -19,7 +19,6 @@ matrix:
19
19
 
20
20
  before_install:
21
21
  - gem update --system -N
22
- - gem install bundler:2.0.2 -N
23
22
 
24
23
  before_script:
25
24
  - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fast_serializer_ruby (0.1.4)
4
+ fast_serializer_ruby (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -76,7 +76,7 @@ schema.attribute(:id)
76
76
  schema.attribute(:email)
77
77
  schema.attribute(:full_name) { |resource| "#{resource.first_name} #{resource.last_name}"}
78
78
  schema.attribute(:phone)
79
- schema.has_one(:has_one_relationship, serializer: schema)
79
+ schema.has_one(:has_one_relationship, schema: schema)
80
80
 
81
81
  schema.serializable_hash
82
82
  => {
@@ -2,16 +2,18 @@
2
2
 
3
3
  module FastSerializer
4
4
  module JsonModel
5
- class Array < HasManyRelationship
6
- def serialize(resources, params = {})
5
+ class Array < Relationship
6
+ def serialize(resources, params = {}, context = nil)
7
7
  return if resources.nil?
8
8
 
9
- resources.map do |resource|
10
- serialization_schema.serialize(resource, params)
9
+ if @serializer_klass
10
+ @serializer_klass.new(resources, params).serializable_hash
11
+ elsif @schema
12
+ resources.map { |resource| @schema.serialize(resource, params, context) }
11
13
  end
12
14
  end
13
15
 
14
- def included?(_resources, _params = {})
16
+ def included?(_resources, _params = {}, context = nil)
15
17
  true
16
18
  end
17
19
  end
@@ -3,20 +3,28 @@
3
3
  module FastSerializer
4
4
  module JsonModel
5
5
  class Attribute < Node
6
- def serialize(resource, params = {})
6
+ def serialize(resource, params = {}, context = nil)
7
+ context ||= self
8
+
7
9
  if method.is_a?(Proc)
8
- method.arity.abs == 1 ? method.call(resource) : method.call(resource, params)
10
+
11
+ if method.arity.abs == 1
12
+ context.instance_exec(resource, &method)
13
+ else
14
+ context.instance_exec(resource, params, &method)
15
+ end
16
+
9
17
  else
10
18
  resource.public_send(method)
11
19
  end
12
20
  end
13
21
 
14
- def included?(resource, params)
22
+ def included?(resource, params, context = nil)
15
23
  return true if @opts[:if].nil? && @opts[:unless].nil?
16
24
 
17
25
  cond = @opts[:if] || @opts[:unless]
18
26
 
19
- res = cond.call(resource, params)
27
+ res = context.instance_exec(resource, params, &cond)
20
28
  res = !res unless @opts[:unless].nil?
21
29
 
22
30
  res
@@ -3,13 +3,16 @@
3
3
  module FastSerializer
4
4
  module JsonModel
5
5
  class HasManyRelationship < Relationship
6
- def serialize(resource, params = {})
6
+ def serialize(resource, params = {}, context = nil)
7
7
  collection = resource.public_send(method)
8
8
  return if collection.nil?
9
9
 
10
- collection.map do |relation_resource|
11
- serialization_schema.serialize(relation_resource, params)
10
+ if @serializer_klass
11
+ @serializer_klass.new(collection, params).serializable_hash
12
+ elsif @schema
13
+ collection.map { |resource| @schema.serialize_resource(resource, params) }
12
14
  end
15
+
13
16
  end
14
17
  end
15
18
  end
@@ -3,8 +3,14 @@
3
3
  module FastSerializer
4
4
  module JsonModel
5
5
  class HasOneRelationship < Relationship
6
- def serialize(resource, params = {})
7
- serialization_schema.serialize(resource.public_send(method), params)
6
+ def serialize(resource, params = {}, context = nil)
7
+ relation = resource.public_send(method)
8
+
9
+ if @serializer_klass
10
+ @serializer_klass.new(relation, params).serializable_hash
11
+ elsif @schema
12
+ @schema.serialize_resource(relation, params)
13
+ end
8
14
  end
9
15
  end
10
16
  end
@@ -3,7 +3,7 @@
3
3
  module FastSerializer
4
4
  module JsonModel
5
5
  class Node
6
- attr_accessor :key, :method
6
+ attr_accessor :key, :method, :context
7
7
 
8
8
  def initialize(key: nil, method: nil, opts: {}, **_)
9
9
  @key = key
@@ -11,11 +11,11 @@ module FastSerializer
11
11
  @opts = opts || {}
12
12
  end
13
13
 
14
- def serialize(_resource, _params = {})
14
+ def serialize(_resource, _params = {}, context = nil)
15
15
  raise NotImplementedError
16
16
  end
17
17
 
18
- def included?(_resource, _params = {})
18
+ def included?(_resource, _params = {}, context = nil)
19
19
  raise NotImplementedError
20
20
  end
21
21
  end
@@ -14,18 +14,18 @@ module FastSerializer
14
14
  attributes[attribute.key] = attribute
15
15
  end
16
16
 
17
- def serialize(resource, params = {})
17
+ def serialize(resource, params = {}, context = nil)
18
18
  return if resource.nil?
19
19
 
20
20
  attributes.values.each_with_object({}) do |attribute, res|
21
- next res unless attribute.included?(resource, params)
21
+ next res unless attribute.included?(resource, params, context)
22
22
 
23
- val = attribute.serialize(resource, params)
24
- res[attribute.key] = val if val
23
+ val = attribute.serialize(resource, params, context)
24
+ res[attribute.key] = val
25
25
  end
26
26
  end
27
27
 
28
- def included?(_resource, _params = {})
28
+ def included?(_resource, _params = {}, context = nil)
29
29
  true
30
30
  end
31
31
  end
@@ -5,12 +5,15 @@ module FastSerializer
5
5
  class Relationship < Attribute
6
6
  attr_accessor :serialization_schema
7
7
 
8
- def initialize(key: nil, method: nil, opts: {}, serialization_schema:)
8
+ def initialize(key: nil, method: nil, opts: {}, serializer: nil, schema: nil)
9
9
  super
10
- @serialization_schema = serialization_schema
10
+ @serializer_klass = serializer
11
+ @schema = schema
12
+
13
+ raise ArgumentError, "must provide serializer or schema" if @serializer_klass.nil? && @schema.nil?
11
14
  end
12
15
 
13
- def included?(resource, params)
16
+ def included?(resource, params, context)
14
17
  super(resource, params) && include_relation?(params)
15
18
  end
16
19
 
@@ -1,168 +1,168 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module FastSerializer
4
- class Schema
5
- module InheritanceSupport
6
- def inherited(subclass)
7
- subclass._serialization_schema ||= JsonModel::Object.new
3
+ require 'forwardable'
8
4
 
9
- _serialization_schema.attributes.each do |key, attribute|
10
- subclass._serialization_schema.attributes[key] = attribute
11
- end
12
- end
13
- end
5
+ module FastSerializer
14
6
 
15
- module SchemaInterface
16
- attr_accessor :_root, :_serialization_schema
7
+ class Schema
8
+ attr_accessor :_root, :serialization_schema, :params
17
9
 
18
- def init
19
- @_root ||= nil
20
- @_serialization_schema ||= JsonModel::Object.new
21
- end
10
+ def initialize(params = {})
11
+ @root = nil
12
+ @serialization_schema = JsonModel::Object.new
13
+ @params = (params || {}).symbolize_keys
14
+ @params[:self] = self
22
15
 
23
- # @param [Array] attribute_names
24
- def attributes(*attribute_names)
25
- attribute_names.each do |attribute_name|
26
- _serialization_schema.add_attribute JsonModel::Attribute.new(
27
- key: attribute_name,
28
- method: attribute_name
29
- )
16
+ if @params[:include]
17
+ if @params[:include].any?
18
+ @params[:include] = @params[:include].map(&:to_sym)
30
19
  end
31
20
  end
21
+ end
32
22
 
33
- # @param [String] attribute_name
34
- # @param [Hash] opts - attribute options
35
- # @param [Proc] block - result is used as the attribute value
36
- def attribute(attribute_name, opts = {}, &block)
37
- _serialization_schema.add_attribute JsonModel::Attribute.new(
38
- key: attribute_name,
39
- method: block,
40
- opts: opts
23
+ # @param [Array] attribute_names
24
+ def attributes(*attribute_names)
25
+ attribute_names.each do |attribute_name|
26
+ serialization_schema.add_attribute JsonModel::Attribute.new(
27
+ key: attribute_name,
28
+ method: attribute_name
41
29
  )
42
30
  end
31
+ end
43
32
 
44
- # @param [String] attribute_name
45
- # @param [Hash] opts - attribute options
46
- def has_one(attribute_name, opts = {})
47
- unless opts[:serializer]
48
- raise ArgumentError, 'Serializer is not provided'
49
- end
33
+ # @param [String] attribute_name
34
+ # @param [Hash] opts - attribute options
35
+ # @param [Proc] block - result is used as the attribute value
36
+ def attribute(attribute_name, opts = {}, &block)
37
+ serialization_schema.add_attribute JsonModel::Attribute.new(
38
+ key: attribute_name,
39
+ method: block,
40
+ opts: opts
41
+ )
42
+ end
50
43
 
51
- serialization_schema = opts.delete(:serializer)._serialization_schema
52
- _serialization_schema.add_attribute JsonModel::HasOneRelationship.new(
53
- key: attribute_name,
54
- method: attribute_name,
55
- opts: opts,
56
- serialization_schema: serialization_schema
57
- )
58
- end
44
+ # @param [String] attribute_name
45
+ # @param [Hash] opts - attribute options
46
+ def has_one(attribute_name, opts = {})
47
+ serialization_schema.add_attribute JsonModel::HasOneRelationship.new(
48
+ key: opts.delete(:key) || attribute_name,
49
+ method: opts.delete(:method) || attribute_name,
50
+ opts: opts,
51
+ schema: opts.delete(:schema),
52
+ serializer: opts.delete(:serializer)
53
+ )
54
+ end
59
55
 
60
- alias belongs_to has_one
56
+ alias belongs_to has_one
57
+
58
+ # @param [String] attribute_name
59
+ # @param [Hash] opts - attribute options
60
+ def has_many(attribute_name, opts = {})
61
+ serialization_schema.add_attribute JsonModel::HasManyRelationship.new(
62
+ key: opts.delete(:key) || attribute_name,
63
+ method: opts.delete(:method) || attribute_name,
64
+ opts: opts,
65
+ schema: opts.delete(:schema),
66
+ serializer: opts.delete(:serializer),
67
+ )
68
+ end
61
69
 
62
- # @param [String] attribute_name
63
- # @param [Hash] opts - attribute options
64
- def has_many(attribute_name, opts = {})
65
- unless opts[:serializer]
66
- raise ArgumentError, 'Serializer is not provided'
67
- end
70
+ # @param [String] attribute_name
71
+ # @param [Hash] opts - attribute options
72
+ def list(attribute_name, opts = {})
73
+ serialization_schema.add_attribute JsonModel::Array.new(
74
+ key: attribute_name,
75
+ method: attribute_name,
76
+ opts: opts,
77
+ schema: opts.delete(:schema),
78
+ serializer: opts.delete(:serializer)
79
+ )
80
+ end
68
81
 
69
- serialization_schema = opts.delete(:serializer)._serialization_schema
70
- _serialization_schema.add_attribute JsonModel::HasManyRelationship.new(
71
- key: attribute_name,
72
- method: attribute_name,
73
- opts: opts,
74
- serialization_schema: serialization_schema
75
- )
76
- end
82
+ # @param [String] root_key - a key under which serialization result is nested
83
+ def root(root_key)
84
+ self._root = root_key
85
+ end
77
86
 
78
- # @param [String] attribute_name
79
- # @param [Hash] opts - attribute options
80
- def list(attribute_name, opts = {})
81
- unless opts[:serializer]
82
- raise ArgumentError, 'Serializer is not provided'
83
- end
87
+ def deep_copy
88
+ schema = FastSerializer::Schema.new
89
+ schema.params = params
90
+ schema._root = _root
84
91
 
85
- serialization_schema = opts.delete(:serializer)._serialization_schema
86
- _serialization_schema.add_attribute JsonModel::Array.new(
87
- key: attribute_name,
88
- method: attribute_name,
89
- opts: opts,
90
- serialization_schema: serialization_schema
91
- )
92
+ serialization_schema.attributes.each do |key, attribute|
93
+ schema.serialization_schema.attributes[key] = attribute
92
94
  end
93
95
 
94
- # @param [String] root_key - a key under which serialization result is nested
95
- def root(root_key)
96
- self._root = root_key
97
- end
96
+ schema
98
97
  end
99
98
 
100
- module Serialization
101
- attr_accessor :resource, :params
102
-
103
- def initialize(resource, params = {})
104
- init if respond_to?(:init)
105
- @resource = resource
106
- @params = (params || {}).symbolize_keys
107
- @params[:self] = self
99
+ def serialize_resource(resource, params = {}, context = self)
100
+ _params_dup = self.params.merge(params).symbolize_keys
101
+ meta = _params_dup.delete(:meta)
108
102
 
109
- if @params[:include]
110
- if @params[:include].empty?
111
- @params.delete(:include)
112
- else
113
- @params[:include] = @params[:include].map(&:to_sym)
114
- end
115
- end
116
- end
103
+ is_collection = resource.respond_to?(:size) && !resource.respond_to?(:each_pair)
117
104
 
118
- def serializable_hash
119
- meta = params.delete(:meta)
120
- res = schema.serialize(resource, params)
105
+ _serialization_schema = if is_collection
106
+ JsonModel::Array.new(schema: serialization_schema)
107
+ else
108
+ serialization_schema
109
+ end
121
110
 
122
- root = (_root || params.delete(:root))
111
+ res = _serialization_schema.serialize(resource, _params_dup, context)
112
+ root = (_root || _params_dup.delete(:root))
113
+ res = { root => res } if root && !root.empty?
123
114
 
124
- res = { root => res } if root && !root.empty?
115
+ res[:meta] = meta if res.is_a?(Hash) && meta
125
116
 
126
- res[:meta] = meta if res.is_a?(Hash) && meta
117
+ res
118
+ end
127
119
 
128
- res
129
- end
120
+ def serialize_resource_to_json(resource, params = {}, context = self)
121
+ FastSerializer.config.coder.dump(serialize_resource(resource, params))
122
+ end
130
123
 
131
- def serialized_json
132
- FastSerializer.config.coder.dump(serializable_hash)
133
- end
124
+ module Mixin
134
125
 
135
- private
126
+ module ClassMethods
127
+ attr_accessor :__schema__
136
128
 
137
- def schema
138
- is_collection = resource.respond_to?(:size) && !resource.respond_to?(:each_pair)
129
+ def inherited(subclass)
130
+ subclass.__schema__ = self.__schema__.deep_copy
131
+ end
139
132
 
140
- if is_collection
141
- JsonModel::Array.new(serialization_schema: _serialization_schema)
142
- else
143
- _serialization_schema
133
+ def method_missing(method, *args, &block)
134
+ if __schema__.respond_to?(method)
135
+ __schema__.public_send(method, *args, &block)
136
+ else
137
+ super
138
+ end
144
139
  end
145
140
  end
146
- end
147
141
 
148
- include SchemaInterface
149
- include Serialization
142
+ module InstanceMethods
143
+ attr_accessor :resource, :params
150
144
 
151
- module Mixin
152
- def _serialization_schema
153
- self.class._serialization_schema
154
- end
145
+ def initialize(resource, params = {})
146
+ self.resource = resource
147
+ self.params = params || {}
148
+ end
155
149
 
156
- def _root
157
- self.class._root
150
+ def serializable_hash
151
+ self.class.__schema__.serialize_resource(resource, params, self)
152
+ end
153
+
154
+ def serialized_json
155
+ self.class.__schema__.serialize_resource_to_json(resource, params, self)
156
+ end
157
+
158
+ alias as_json serializable_hash
159
+ alias to_json serialized_json
158
160
  end
159
161
 
160
162
  def self.included(base)
161
- base.include Serialization
162
- base.extend InheritanceSupport
163
- base.extend SchemaInterface
164
-
165
- base.init
163
+ base.extend ClassMethods
164
+ base.include InstanceMethods
165
+ base.__schema__ = FastSerializer::Schema.new
166
166
  end
167
167
  end
168
168
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FastSerializer
4
- VERSION = '0.1.4'
4
+ VERSION = '0.2.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fast_serializer_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeny Stepanov