fast_serializer_ruby 0.1.4 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +0 -1
- data/Gemfile.lock +1 -1
- data/README.md +1 -1
- data/lib/fast_serializer/json_model/array.rb +7 -5
- data/lib/fast_serializer/json_model/attribute.rb +12 -4
- data/lib/fast_serializer/json_model/has_many_relationship.rb +6 -3
- data/lib/fast_serializer/json_model/has_one_relationship.rb +8 -2
- data/lib/fast_serializer/json_model/node.rb +3 -3
- data/lib/fast_serializer/json_model/object.rb +5 -5
- data/lib/fast_serializer/json_model/relationship.rb +6 -3
- data/lib/fast_serializer/schema.rb +125 -125
- data/lib/fast_serializer/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4226b5f0bc68c749226425f93db90d60bfe5b0cba0faa74a77e9cfd24680a73b
|
4
|
+
data.tar.gz: 697304fc0c88cdd9848c974027689c5eb4a0f90daa3e95fb4c2d2e723ceb8040
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad76357998ec5b0de5768057e6dd6191f4cc8f7eba5dfc9170206245570ad725ba822f1bbf5d35dfed5364227f6dd28130aaadc70717158f42d80d906f76f98b
|
7
|
+
data.tar.gz: 2a43e81fe55e7d4a8dd56b56a489030d38f4d810564b332ee0f965b0d16b3152622a5aab97779dfa45b8cfe276640b16da6fd1eb7c81c4a629bbb82939ab6e05
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
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,
|
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 <
|
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
|
-
|
10
|
-
|
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
|
-
|
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 =
|
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
|
-
|
11
|
-
|
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
|
-
|
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
|
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: {},
|
8
|
+
def initialize(key: nil, method: nil, opts: {}, serializer: nil, schema: nil)
|
9
9
|
super
|
10
|
-
@
|
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
|
-
|
4
|
-
class Schema
|
5
|
-
module InheritanceSupport
|
6
|
-
def inherited(subclass)
|
7
|
-
subclass._serialization_schema ||= JsonModel::Object.new
|
3
|
+
require 'forwardable'
|
8
4
|
|
9
|
-
|
10
|
-
subclass._serialization_schema.attributes[key] = attribute
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
5
|
+
module FastSerializer
|
14
6
|
|
15
|
-
|
16
|
-
|
7
|
+
class Schema
|
8
|
+
attr_accessor :_root, :serialization_schema, :params
|
17
9
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
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
|
-
|
24
|
-
|
25
|
-
|
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
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
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
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
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
|
-
|
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
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
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
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
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
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
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
|
-
|
86
|
-
|
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
|
-
|
95
|
-
def root(root_key)
|
96
|
-
self._root = root_key
|
97
|
-
end
|
96
|
+
schema
|
98
97
|
end
|
99
98
|
|
100
|
-
|
101
|
-
|
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
|
-
|
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
|
-
|
119
|
-
|
120
|
-
|
105
|
+
_serialization_schema = if is_collection
|
106
|
+
JsonModel::Array.new(schema: serialization_schema)
|
107
|
+
else
|
108
|
+
serialization_schema
|
109
|
+
end
|
121
110
|
|
122
|
-
|
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
|
-
|
115
|
+
res[:meta] = meta if res.is_a?(Hash) && meta
|
125
116
|
|
126
|
-
|
117
|
+
res
|
118
|
+
end
|
127
119
|
|
128
|
-
|
129
|
-
|
120
|
+
def serialize_resource_to_json(resource, params = {}, context = self)
|
121
|
+
FastSerializer.config.coder.dump(serialize_resource(resource, params))
|
122
|
+
end
|
130
123
|
|
131
|
-
|
132
|
-
FastSerializer.config.coder.dump(serializable_hash)
|
133
|
-
end
|
124
|
+
module Mixin
|
134
125
|
|
135
|
-
|
126
|
+
module ClassMethods
|
127
|
+
attr_accessor :__schema__
|
136
128
|
|
137
|
-
|
138
|
-
|
129
|
+
def inherited(subclass)
|
130
|
+
subclass.__schema__ = self.__schema__.deep_copy
|
131
|
+
end
|
139
132
|
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
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
|
-
|
149
|
-
|
142
|
+
module InstanceMethods
|
143
|
+
attr_accessor :resource, :params
|
150
144
|
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
145
|
+
def initialize(resource, params = {})
|
146
|
+
self.resource = resource
|
147
|
+
self.params = params || {}
|
148
|
+
end
|
155
149
|
|
156
|
-
|
157
|
-
|
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.
|
162
|
-
base.
|
163
|
-
base.
|
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
|