restpack_serializer 0.6.8 → 0.6.13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/ci.yml +29 -0
- data/.ruby-version +1 -1
- data/Gemfile +5 -0
- data/Rakefile +1 -0
- data/lib/restpack_serializer.rb +1 -1
- data/lib/restpack_serializer/serializable.rb +17 -5
- data/lib/restpack_serializer/serializable/side_loading.rb +5 -1
- data/lib/restpack_serializer/version.rb +1 -1
- data/performance/mem.rb +49 -0
- data/restpack_serializer.gemspec +3 -5
- data/spec/fixtures/db.rb +2 -11
- data/spec/serializable/serializer_spec.rb +35 -0
- data/spec/spec_helper.rb +2 -4
- metadata +19 -41
- data/.travis.yml +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5cbdfc54c01006c75a8db9b73a60b7dc713e17257e77991b82bf4b3f3d22b537
|
4
|
+
data.tar.gz: 126c3c857097eb24d9f45aa1d9a1a2b40363f1246edc869350249b6fba30c3f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6002f9e445e71c1d4353b7954421dc80d6e1742ff5444f5701e3612a78a2cbd21b03de9a2347b0d8f534034e75a3d5badfdd75a7a26c037ef7897f394a5f7784
|
7
|
+
data.tar.gz: 7f81b17da0dd9cf8855108a00174c8ac0ce9df55732f87b0a1691f838abf90f18d7711363cdbf8b43326b759350250ef9e84e4dbc08b0e0c93280798205c20fb
|
@@ -0,0 +1,29 @@
|
|
1
|
+
name: CI
|
2
|
+
|
3
|
+
on: [push, pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
test:
|
7
|
+
runs-on: ubuntu-latest
|
8
|
+
|
9
|
+
strategy:
|
10
|
+
fail-fast: false
|
11
|
+
|
12
|
+
matrix:
|
13
|
+
ruby-version:
|
14
|
+
- 2.1
|
15
|
+
- 2.2
|
16
|
+
- 2.3
|
17
|
+
- 2.4
|
18
|
+
- 2.5
|
19
|
+
- 2.6
|
20
|
+
- 2.7
|
21
|
+
- 3.0
|
22
|
+
|
23
|
+
steps:
|
24
|
+
- uses: actions/checkout@v2
|
25
|
+
- uses: ruby/setup-ruby@v1
|
26
|
+
with:
|
27
|
+
ruby-version: ${{ matrix.ruby-version }}
|
28
|
+
bundler-cache: true
|
29
|
+
- run: bundle exec rake test
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.7.2
|
data/Gemfile
CHANGED
data/Rakefile
CHANGED
data/lib/restpack_serializer.rb
CHANGED
@@ -6,7 +6,7 @@ require_relative 'restpack_serializer/serializable'
|
|
6
6
|
require_relative 'restpack_serializer/factory'
|
7
7
|
require_relative 'restpack_serializer/result'
|
8
8
|
|
9
|
-
Kaminari::Hooks.init
|
9
|
+
Kaminari::Hooks.init if defined?(Kaminari::Hooks)
|
10
10
|
|
11
11
|
module RestPack
|
12
12
|
module Serializer
|
@@ -34,7 +34,7 @@ module RestPack
|
|
34
34
|
def as_json(model, context = {})
|
35
35
|
return if model.nil?
|
36
36
|
if model.kind_of?(Array)
|
37
|
-
return model.map { |item| as_json(item, context) }
|
37
|
+
return model.map { |item| self.class.new.as_json(item, context) }
|
38
38
|
end
|
39
39
|
|
40
40
|
apply_whitelist_and_blacklist(context)
|
@@ -57,11 +57,15 @@ module RestPack
|
|
57
57
|
end
|
58
58
|
|
59
59
|
add_custom_attributes(data)
|
60
|
-
add_links(model, data)
|
60
|
+
add_links(model, data) if self.class.has_associations?
|
61
61
|
|
62
62
|
data
|
63
63
|
end
|
64
64
|
|
65
|
+
def to_json(model, context = {})
|
66
|
+
as_json(model, context).to_json
|
67
|
+
end
|
68
|
+
|
65
69
|
def custom_attributes
|
66
70
|
nil
|
67
71
|
end
|
@@ -140,9 +144,13 @@ module RestPack
|
|
140
144
|
end
|
141
145
|
|
142
146
|
def has_user_defined_method?(method_name)
|
143
|
-
user_defined_methods
|
144
|
-
|
145
|
-
|
147
|
+
if user_defined_methods && user_defined_methods.include?(method_name)
|
148
|
+
true
|
149
|
+
elsif superclass.respond_to?(:has_user_defined_method?)
|
150
|
+
superclass.has_user_defined_method?(method_name)
|
151
|
+
else
|
152
|
+
false
|
153
|
+
end
|
146
154
|
end
|
147
155
|
|
148
156
|
def memoized_has_user_defined_method?(method_name)
|
@@ -165,6 +173,10 @@ module RestPack
|
|
165
173
|
new.as_json(model, context)
|
166
174
|
end
|
167
175
|
|
176
|
+
def to_json(model, context = {})
|
177
|
+
new.as_json(model, context).to_json
|
178
|
+
end
|
179
|
+
|
168
180
|
def serialize(models, context = {})
|
169
181
|
models = [models] unless models.kind_of?(Array)
|
170
182
|
|
@@ -44,8 +44,12 @@ module RestPack::Serializer::SideLoading
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
+
def has_associations?
|
48
|
+
@can_includes
|
49
|
+
end
|
50
|
+
|
47
51
|
def associations
|
48
|
-
return [] unless
|
52
|
+
return [] unless has_associations?
|
49
53
|
can_includes.map do |include|
|
50
54
|
association = association_from_include(include)
|
51
55
|
association if supported_association?(association.macro)
|
data/performance/mem.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'memory_profiler'
|
2
|
+
require_relative '../lib/restpack_serializer'
|
3
|
+
|
4
|
+
class SimpleSerializer
|
5
|
+
include RestPack::Serializer
|
6
|
+
attributes :id, :title
|
7
|
+
end
|
8
|
+
|
9
|
+
simple_model = {
|
10
|
+
id: "123",
|
11
|
+
title: 'This is the title',
|
12
|
+
}
|
13
|
+
|
14
|
+
# warmup
|
15
|
+
SimpleSerializer.as_json(simple_model)
|
16
|
+
|
17
|
+
report = MemoryProfiler.report do
|
18
|
+
SimpleSerializer.as_json(simple_model)
|
19
|
+
end
|
20
|
+
|
21
|
+
puts "="*64
|
22
|
+
puts "Simple Serializer:"
|
23
|
+
puts "="*64
|
24
|
+
|
25
|
+
report.pretty_print(detailed_report: false)
|
26
|
+
|
27
|
+
class ComplexSerializer
|
28
|
+
include RestPack::Serializer
|
29
|
+
|
30
|
+
attributes :a, :b, :c, :d, :e, :f, :g, :h, :i, :j, :k, :l, :m, :n, :o, :p, :q, :r, :s, :t
|
31
|
+
end
|
32
|
+
|
33
|
+
complex_model = {
|
34
|
+
a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10,
|
35
|
+
k: 11, l: 12, m: 13, n: 14, o: 15, p: 16, q: 17, r: 18, s: 19, t: 20,
|
36
|
+
}
|
37
|
+
|
38
|
+
# warmup
|
39
|
+
ComplexSerializer.as_json(complex_model)
|
40
|
+
|
41
|
+
report = MemoryProfiler.report do
|
42
|
+
ComplexSerializer.as_json(complex_model)
|
43
|
+
end
|
44
|
+
|
45
|
+
puts "="*64
|
46
|
+
puts "Complex Serializer:"
|
47
|
+
puts "="*64
|
48
|
+
|
49
|
+
report.pretty_print(detailed_report: false)
|
data/restpack_serializer.gemspec
CHANGED
@@ -17,10 +17,9 @@ Gem::Specification.new do |gem|
|
|
17
17
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
18
|
gem.require_paths = ["lib"]
|
19
19
|
|
20
|
-
gem.add_dependency 'activesupport', ['>= 4.0.3', '<
|
21
|
-
gem.add_dependency 'activerecord', ['>= 4.0.3', '<
|
22
|
-
gem.add_dependency 'kaminari', '
|
23
|
-
gem.add_dependency 'kaminari-mongoid', '~> 0.1'
|
20
|
+
gem.add_dependency 'activesupport', ['>= 4.0.3', '< 7.0']
|
21
|
+
gem.add_dependency 'activerecord', ['>= 4.0.3', '< 7.0']
|
22
|
+
gem.add_dependency 'kaminari', ['>= 0.17.0', '< 2.0']
|
24
23
|
|
25
24
|
gem.add_development_dependency 'restpack_gem', '~> 0.0.9'
|
26
25
|
gem.add_development_dependency 'rake', '~> 11.3'
|
@@ -30,5 +29,4 @@ Gem::Specification.new do |gem|
|
|
30
29
|
gem.add_development_dependency 'database_cleaner', '~> 1.5'
|
31
30
|
gem.add_development_dependency 'rspec'
|
32
31
|
gem.add_development_dependency 'bump'
|
33
|
-
gem.add_development_dependency 'protected_attributes_continued', '~> 1.2'
|
34
32
|
end
|
data/spec/fixtures/db.rb
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
require 'sqlite3'
|
2
2
|
require 'active_record'
|
3
|
-
require 'protected_attributes_continued'
|
4
3
|
|
5
4
|
ActiveRecord::Base.establish_connection(
|
6
5
|
:adapter => 'sqlite3',
|
7
6
|
:database => 'test.db'
|
8
7
|
)
|
9
8
|
|
9
|
+
ActiveRecord::Migration.verbose = false
|
10
|
+
|
10
11
|
ActiveRecord::Schema.define(:version => 1) do
|
11
12
|
create_table "artists", :force => true do |t|
|
12
13
|
t.string "name"
|
@@ -66,8 +67,6 @@ end
|
|
66
67
|
|
67
68
|
module MyApp
|
68
69
|
class Artist < ActiveRecord::Base
|
69
|
-
attr_accessible :name, :website
|
70
|
-
|
71
70
|
has_many :albums
|
72
71
|
has_many :songs
|
73
72
|
has_many :payments
|
@@ -76,7 +75,6 @@ module MyApp
|
|
76
75
|
end
|
77
76
|
|
78
77
|
class Album < ActiveRecord::Base
|
79
|
-
attr_accessible :title, :year, :artist
|
80
78
|
scope :classic, -> { where("year < 1950") }
|
81
79
|
|
82
80
|
belongs_to :artist
|
@@ -85,34 +83,27 @@ module MyApp
|
|
85
83
|
end
|
86
84
|
|
87
85
|
class AlbumReview < ActiveRecord::Base
|
88
|
-
attr_accessible :message
|
89
86
|
belongs_to :album
|
90
87
|
end
|
91
88
|
|
92
89
|
class Song < ActiveRecord::Base
|
93
90
|
default_scope -> { order(id: :asc) }
|
94
91
|
|
95
|
-
attr_accessible :title, :artist, :album
|
96
|
-
|
97
92
|
belongs_to :artist
|
98
93
|
belongs_to :album
|
99
94
|
end
|
100
95
|
|
101
96
|
class Payment < ActiveRecord::Base
|
102
|
-
attr_accessible :amount, :artist
|
103
|
-
|
104
97
|
belongs_to :artist
|
105
98
|
belongs_to :fan
|
106
99
|
end
|
107
100
|
|
108
101
|
class Fan < ActiveRecord::Base
|
109
|
-
attr_accessible :name
|
110
102
|
has_many :payments
|
111
103
|
has_many :artists, :through => :albums
|
112
104
|
end
|
113
105
|
|
114
106
|
class Stalker < ActiveRecord::Base
|
115
|
-
attr_accessible :name
|
116
107
|
has_and_belongs_to_many :artists
|
117
108
|
end
|
118
109
|
end
|
@@ -79,6 +79,27 @@ describe RestPack::Serializer do
|
|
79
79
|
end
|
80
80
|
end
|
81
81
|
|
82
|
+
context "serializer instance variables" do
|
83
|
+
class MemoizingSerializer
|
84
|
+
include RestPack::Serializer
|
85
|
+
|
86
|
+
attributes :id, :memoized_id
|
87
|
+
|
88
|
+
def memoized_id
|
89
|
+
@memoized_id ||= id
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
it "does not reuse instance variable values" do
|
94
|
+
people = [Person.new(id: 123), Person.new(id: 456)]
|
95
|
+
serialized = MemoizingSerializer.as_json(people)
|
96
|
+
expect(serialized).to eq([
|
97
|
+
{ id: "123", memoized_id: "123" },
|
98
|
+
{ id: "456", memoized_id: "456" }
|
99
|
+
])
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
82
103
|
class PersonSerializer
|
83
104
|
include RestPack::Serializer
|
84
105
|
attributes :id, :name, :description, :href, :admin_info, :string_keys
|
@@ -292,6 +313,20 @@ describe RestPack::Serializer do
|
|
292
313
|
end
|
293
314
|
end
|
294
315
|
|
316
|
+
describe "to_json" do
|
317
|
+
context "class method" do
|
318
|
+
it "delegates to as_json" do
|
319
|
+
expect(PersonSerializer.as_json(person).to_json).to eq(PersonSerializer.to_json(person))
|
320
|
+
end
|
321
|
+
end
|
322
|
+
|
323
|
+
context "instance method" do
|
324
|
+
it "delegates to as_json" do
|
325
|
+
expect(serializer.as_json(person).to_json).to eq(serializer.to_json(person))
|
326
|
+
end
|
327
|
+
end
|
328
|
+
end
|
329
|
+
|
295
330
|
describe "#model_class" do
|
296
331
|
it "extracts the Model name from the Serializer name" do
|
297
332
|
expect(PersonSerializer.model_class).to eq(Person)
|
data/spec/spec_helper.rb
CHANGED
@@ -5,6 +5,8 @@ require './spec/fixtures/serializers'
|
|
5
5
|
require './spec/support/factory'
|
6
6
|
require 'database_cleaner'
|
7
7
|
require 'coveralls'
|
8
|
+
|
9
|
+
Coveralls::Output.silent = true unless ENV["CI"]
|
8
10
|
Coveralls.wear!
|
9
11
|
FactoryGirl.find_definitions
|
10
12
|
|
@@ -20,10 +22,6 @@ RSpec.configure do |config|
|
|
20
22
|
DatabaseCleaner.strategy = :transaction
|
21
23
|
end
|
22
24
|
|
23
|
-
config.before(:each, :js => true) do
|
24
|
-
DatabaseCleaner.strategy = :truncation
|
25
|
-
end
|
26
|
-
|
27
25
|
config.before(:each) do
|
28
26
|
DatabaseCleaner.start
|
29
27
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restpack_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.13
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gavin Joyce
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: 4.0.3
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '
|
22
|
+
version: '7.0'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,7 +29,7 @@ dependencies:
|
|
29
29
|
version: 4.0.3
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
32
|
+
version: '7.0'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: activerecord
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -39,7 +39,7 @@ dependencies:
|
|
39
39
|
version: 4.0.3
|
40
40
|
- - "<"
|
41
41
|
- !ruby/object:Gem::Version
|
42
|
-
version: '
|
42
|
+
version: '7.0'
|
43
43
|
type: :runtime
|
44
44
|
prerelease: false
|
45
45
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -49,35 +49,27 @@ dependencies:
|
|
49
49
|
version: 4.0.3
|
50
50
|
- - "<"
|
51
51
|
- !ruby/object:Gem::Version
|
52
|
-
version: '
|
52
|
+
version: '7.0'
|
53
53
|
- !ruby/object:Gem::Dependency
|
54
54
|
name: kaminari
|
55
55
|
requirement: !ruby/object:Gem::Requirement
|
56
56
|
requirements:
|
57
|
-
- - "
|
57
|
+
- - ">="
|
58
58
|
- !ruby/object:Gem::Version
|
59
59
|
version: 0.17.0
|
60
|
+
- - "<"
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '2.0'
|
60
63
|
type: :runtime
|
61
64
|
prerelease: false
|
62
65
|
version_requirements: !ruby/object:Gem::Requirement
|
63
66
|
requirements:
|
64
|
-
- - "
|
67
|
+
- - ">="
|
65
68
|
- !ruby/object:Gem::Version
|
66
69
|
version: 0.17.0
|
67
|
-
-
|
68
|
-
name: kaminari-mongoid
|
69
|
-
requirement: !ruby/object:Gem::Requirement
|
70
|
-
requirements:
|
71
|
-
- - "~>"
|
72
|
-
- !ruby/object:Gem::Version
|
73
|
-
version: '0.1'
|
74
|
-
type: :runtime
|
75
|
-
prerelease: false
|
76
|
-
version_requirements: !ruby/object:Gem::Requirement
|
77
|
-
requirements:
|
78
|
-
- - "~>"
|
70
|
+
- - "<"
|
79
71
|
- !ruby/object:Gem::Version
|
80
|
-
version: '0
|
72
|
+
version: '2.0'
|
81
73
|
- !ruby/object:Gem::Dependency
|
82
74
|
name: restpack_gem
|
83
75
|
requirement: !ruby/object:Gem::Requirement
|
@@ -190,20 +182,6 @@ dependencies:
|
|
190
182
|
- - ">="
|
191
183
|
- !ruby/object:Gem::Version
|
192
184
|
version: '0'
|
193
|
-
- !ruby/object:Gem::Dependency
|
194
|
-
name: protected_attributes_continued
|
195
|
-
requirement: !ruby/object:Gem::Requirement
|
196
|
-
requirements:
|
197
|
-
- - "~>"
|
198
|
-
- !ruby/object:Gem::Version
|
199
|
-
version: '1.2'
|
200
|
-
type: :development
|
201
|
-
prerelease: false
|
202
|
-
version_requirements: !ruby/object:Gem::Requirement
|
203
|
-
requirements:
|
204
|
-
- - "~>"
|
205
|
-
- !ruby/object:Gem::Version
|
206
|
-
version: '1.2'
|
207
185
|
description: Model serialization, paging, side-loading and filtering
|
208
186
|
email:
|
209
187
|
- gavinjoyce@gmail.com
|
@@ -211,10 +189,10 @@ executables: []
|
|
211
189
|
extensions: []
|
212
190
|
extra_rdoc_files: []
|
213
191
|
files:
|
192
|
+
- ".github/workflows/ci.yml"
|
214
193
|
- ".gitignore"
|
215
194
|
- ".rspec"
|
216
195
|
- ".ruby-version"
|
217
|
-
- ".travis.yml"
|
218
196
|
- Gemfile
|
219
197
|
- Guardfile
|
220
198
|
- LICENSE
|
@@ -235,6 +213,7 @@ files:
|
|
235
213
|
- lib/restpack_serializer/serializable/single.rb
|
236
214
|
- lib/restpack_serializer/serializable/sortable.rb
|
237
215
|
- lib/restpack_serializer/version.rb
|
216
|
+
- performance/mem.rb
|
238
217
|
- performance/perf.rb
|
239
218
|
- restpack_serializer.gemspec
|
240
219
|
- spec/factory/factory_spec.rb
|
@@ -259,7 +238,7 @@ files:
|
|
259
238
|
homepage: https://github.com/RestPack
|
260
239
|
licenses: []
|
261
240
|
metadata: {}
|
262
|
-
post_install_message:
|
241
|
+
post_install_message:
|
263
242
|
rdoc_options: []
|
264
243
|
require_paths:
|
265
244
|
- lib
|
@@ -274,9 +253,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
274
253
|
- !ruby/object:Gem::Version
|
275
254
|
version: '0'
|
276
255
|
requirements: []
|
277
|
-
|
278
|
-
|
279
|
-
signing_key:
|
256
|
+
rubygems_version: 3.1.4
|
257
|
+
signing_key:
|
280
258
|
specification_version: 4
|
281
259
|
summary: Model serialization, paging, side-loading and filtering
|
282
260
|
test_files:
|
data/.travis.yml
DELETED