restpack_serializer 0.6.10 → 0.6.11

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: 60afb1f28b3e59bc186b2efb8b5223b7d786821d541a561d735a0bfa4242c6e7
4
- data.tar.gz: ca2792a37310881b422b1a32529685c2097f90966c3e2e1e60cc8b389fb3bb8d
3
+ metadata.gz: bc9e5ce33121a450eefdd7af1aba1f70d6bc6a5d013f544b553dee69f22db41d
4
+ data.tar.gz: 259cca75737c8c32da93b489d9162cc49807bf7deafd2fcf86b06e508779a93a
5
5
  SHA512:
6
- metadata.gz: dfaee9ec499864a7937726f54035ced857bbfd18a173946cec88d7a9984eec90a4618f9762c238e752cb4bf4fe57ca6c3017dabb74c00d081de2491c23d034d4
7
- data.tar.gz: ff864cb5c5488f451fcfb33d42c2e0ed61d0a02cea28c50395afacaa8430ec34e1c1b347c08cd901d6f60c4a037af71b86aa56ea8b40d622e5c0ccc2f523fa4d
6
+ metadata.gz: 9b507e519351d23fbfa540950f90b2f7a625d04d313d80baf5e500c67a63498ceb44efac4c356e91eaf074a3df1f56b19819fffff9daf299a956b15813272a5f
7
+ data.tar.gz: 2748b0bc4dae717f07697326e61d5f9077dcc55075cf0b5416dee5c172da810565dfb2d6a65bb31dac0196510483839be3e5245f835d4c75b5c4d0b52998d5b3
data/Gemfile CHANGED
@@ -3,3 +3,4 @@ source 'https://rubygems.org'
3
3
  gemspec
4
4
 
5
5
  gem 'coveralls', require: false
6
+ gem 'memory_profiler', require: false
data/Rakefile CHANGED
@@ -4,4 +4,5 @@ RestPack::Gem::Tasks.load_tasks
4
4
  desc "Run some performance tests"
5
5
  task :perf do
6
6
  require_relative 'performance/perf.rb'
7
+ require_relative 'performance/mem.rb'
7
8
  end
@@ -57,11 +57,15 @@ module RestPack
57
57
  end
58
58
 
59
59
  add_custom_attributes(data)
60
- add_links(model, data) unless self.class.associations.empty?
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 = self.user_defined_methods || []
144
- return true if user_defined_methods.include?(method_name)
145
- return self.superclass.try(:has_user_defined_method?, method_name)
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 @can_includes
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)
@@ -1,5 +1,5 @@
1
1
  module RestPack
2
2
  module Serializer
3
- VERSION = '0.6.10'
3
+ VERSION = '0.6.11'
4
4
  end
5
5
  end
@@ -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)
@@ -20,7 +20,6 @@ Gem::Specification.new do |gem|
20
20
  gem.add_dependency 'activesupport', ['>= 4.0.3', '< 6.1']
21
21
  gem.add_dependency 'activerecord', ['>= 4.0.3', '< 6.1']
22
22
  gem.add_dependency 'kaminari', '~> 0.17.0'
23
- gem.add_dependency 'kaminari-mongoid', '~> 0.1'
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
@@ -1,6 +1,5 @@
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',
@@ -66,8 +65,6 @@ end
66
65
 
67
66
  module MyApp
68
67
  class Artist < ActiveRecord::Base
69
- attr_accessible :name, :website
70
-
71
68
  has_many :albums
72
69
  has_many :songs
73
70
  has_many :payments
@@ -76,7 +73,6 @@ module MyApp
76
73
  end
77
74
 
78
75
  class Album < ActiveRecord::Base
79
- attr_accessible :title, :year, :artist
80
76
  scope :classic, -> { where("year < 1950") }
81
77
 
82
78
  belongs_to :artist
@@ -85,34 +81,27 @@ module MyApp
85
81
  end
86
82
 
87
83
  class AlbumReview < ActiveRecord::Base
88
- attr_accessible :message
89
84
  belongs_to :album
90
85
  end
91
86
 
92
87
  class Song < ActiveRecord::Base
93
88
  default_scope -> { order(id: :asc) }
94
89
 
95
- attr_accessible :title, :artist, :album
96
-
97
90
  belongs_to :artist
98
91
  belongs_to :album
99
92
  end
100
93
 
101
94
  class Payment < ActiveRecord::Base
102
- attr_accessible :amount, :artist
103
-
104
95
  belongs_to :artist
105
96
  belongs_to :fan
106
97
  end
107
98
 
108
99
  class Fan < ActiveRecord::Base
109
- attr_accessible :name
110
100
  has_many :payments
111
101
  has_many :artists, :through => :albums
112
102
  end
113
103
 
114
104
  class Stalker < ActiveRecord::Base
115
- attr_accessible :name
116
105
  has_and_belongs_to_many :artists
117
106
  end
118
107
  end
@@ -313,6 +313,20 @@ describe RestPack::Serializer do
313
313
  end
314
314
  end
315
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
+
316
330
  describe "#model_class" do
317
331
  it "extracts the Model name from the Serializer name" do
318
332
  expect(PersonSerializer.model_class).to eq(Person)
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.10
4
+ version: 0.6.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gavin Joyce
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-21 00:00:00.000000000 Z
11
+ date: 2020-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -64,20 +64,6 @@ dependencies:
64
64
  - - "~>"
65
65
  - !ruby/object:Gem::Version
66
66
  version: 0.17.0
67
- - !ruby/object:Gem::Dependency
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
- - - "~>"
79
- - !ruby/object:Gem::Version
80
- version: '0.1'
81
67
  - !ruby/object:Gem::Dependency
82
68
  name: restpack_gem
83
69
  requirement: !ruby/object:Gem::Requirement
@@ -190,20 +176,6 @@ dependencies:
190
176
  - - ">="
191
177
  - !ruby/object:Gem::Version
192
178
  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
179
  description: Model serialization, paging, side-loading and filtering
208
180
  email:
209
181
  - gavinjoyce@gmail.com
@@ -235,6 +207,7 @@ files:
235
207
  - lib/restpack_serializer/serializable/single.rb
236
208
  - lib/restpack_serializer/serializable/sortable.rb
237
209
  - lib/restpack_serializer/version.rb
210
+ - performance/mem.rb
238
211
  - performance/perf.rb
239
212
  - restpack_serializer.gemspec
240
213
  - spec/factory/factory_spec.rb
@@ -274,8 +247,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
274
247
  - !ruby/object:Gem::Version
275
248
  version: '0'
276
249
  requirements: []
277
- rubyforge_project:
278
- rubygems_version: 2.7.6.2
250
+ rubygems_version: 3.0.3
279
251
  signing_key:
280
252
  specification_version: 4
281
253
  summary: Model serialization, paging, side-loading and filtering