fast_serializer 1.1.1 → 1.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +46 -0
- data/MIT_LICENSE +1 -1
- data/README.md +31 -1
- data/VERSION +1 -1
- data/fast_serializer.gemspec +29 -19
- data/lib/fast_serializer/array_serializer.rb +13 -5
- data/lib/fast_serializer/cache/active_support_cache.rb +8 -6
- data/lib/fast_serializer/cache.rb +14 -0
- data/lib/fast_serializer/serialization_context.rb +17 -2
- data/lib/fast_serializer/serialized_field.rb +32 -11
- data/lib/fast_serializer/serializer.rb +56 -29
- data/lib/fast_serializer.rb +14 -12
- metadata +18 -67
- data/.gitignore +0 -17
- data/HISTORY.md +0 -25
- data/Rakefile +0 -18
- data/spec/array_serializer_spec.rb +0 -67
- data/spec/cache/active_support_cache_spec.rb +0 -24
- data/spec/fast_serializer_spec.rb +0 -40
- data/spec/serialization_context_spec.rb +0 -32
- data/spec/serialized_field_spec.rb +0 -71
- data/spec/serializer_spec.rb +0 -177
- data/spec/spec_helper.rb +0 -18
- data/spec/support/test_models.rb +0 -74
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module FastSerializer
|
2
4
|
# Models can include this module to define themselves as serializers. A serializer is used to wrap
|
3
5
|
# an object and output a hash version of that object suitable for serialization to JSON or other formats.
|
@@ -55,7 +57,6 @@ module FastSerializer
|
|
55
57
|
#
|
56
58
|
# Serializing a nil object will result in nil rather than an empty hash.
|
57
59
|
module Serializer
|
58
|
-
|
59
60
|
def self.included(base)
|
60
61
|
base.extend(ClassMethods)
|
61
62
|
base.extend(ArrayHelper) unless base.is_a?(FastSerializer::ArraySerializer)
|
@@ -100,19 +101,29 @@ module FastSerializer
|
|
100
101
|
#
|
101
102
|
# Subclasses will inherit all of their parent classes serialized fields. Subclasses can override fields
|
102
103
|
# defined on the parent class by simply defining them again.
|
104
|
+
#
|
105
|
+
# @param fields [Array<Symbol, Hash>] the fields to serialize. If the last argument is a hash, it will be
|
106
|
+
# treated as options for the serialized fields.
|
107
|
+
# @return [void]
|
103
108
|
def serialize(*fields)
|
104
109
|
options = {}
|
105
110
|
if fields.size > 1 && fields.last.is_a?(Hash)
|
106
|
-
|
111
|
+
fields.last.each do |key, value|
|
112
|
+
options[key.to_sym] = value
|
113
|
+
end
|
107
114
|
fields = fields[0, fields.size - 1]
|
108
115
|
end
|
109
|
-
as = options
|
110
|
-
optional = options.
|
111
|
-
delegate = options.
|
112
|
-
enumerable = options.
|
113
|
-
serializer = options
|
114
|
-
serializer_options = options
|
115
|
-
condition = options
|
116
|
+
as = options.delete(:as)
|
117
|
+
optional = options.delete(:optional) || false
|
118
|
+
delegate = options.delete(:delegate) || true
|
119
|
+
enumerable = options.delete(:enumerable) || false
|
120
|
+
serializer = options.delete(:serializer)
|
121
|
+
serializer_options = options.delete(:serializer_options)
|
122
|
+
condition = options.delete(:if)
|
123
|
+
|
124
|
+
unless options.empty?
|
125
|
+
raise ArgumentError.new("Unsupported serialize options: #{options.keys.join(", ")}")
|
126
|
+
end
|
116
127
|
|
117
128
|
if as && fields.size > 1
|
118
129
|
raise ArgumentError.new("Cannot specify :as argument with multiple fields to serialize")
|
@@ -120,8 +131,8 @@ module FastSerializer
|
|
120
131
|
|
121
132
|
fields.each do |field|
|
122
133
|
name = as
|
123
|
-
if name.nil? && field.to_s.end_with?("?"
|
124
|
-
name = field.to_s.chomp("?"
|
134
|
+
if name.nil? && field.to_s.end_with?("?")
|
135
|
+
name = field.to_s.chomp("?")
|
125
136
|
end
|
126
137
|
|
127
138
|
field = field.to_sym
|
@@ -136,6 +147,8 @@ module FastSerializer
|
|
136
147
|
|
137
148
|
# Remove a field from being serialized. This can be useful in subclasses if they need to remove a
|
138
149
|
# field defined by the parent class.
|
150
|
+
#
|
151
|
+
# @param fields [Array<Symbol>] the fields to remove
|
139
152
|
def remove(*fields)
|
140
153
|
remove_fields = fields.collect(&:to_sym)
|
141
154
|
field_list = []
|
@@ -153,6 +166,10 @@ module FastSerializer
|
|
153
166
|
#
|
154
167
|
# You can also specify the cache time to live (ttl) in seconds and the cache implementation to use.
|
155
168
|
# Both of these values are inherited on subclasses.
|
169
|
+
#
|
170
|
+
# @param cacheable [Boolean] pass false if the serializer is not cacheable
|
171
|
+
# @param ttl [Numeric] the time to live in seconds for a cacheable serializer
|
172
|
+
# @param cache [FastSerializer::Cache] the cache implementation to use for a cacheable serializer
|
156
173
|
def cacheable(cacheable = true, ttl: nil, cache: nil)
|
157
174
|
@cacheable = cacheable
|
158
175
|
self.cache_ttl = ttl if ttl
|
@@ -160,6 +177,8 @@ module FastSerializer
|
|
160
177
|
end
|
161
178
|
|
162
179
|
# Return true if the serializer class is cacheable.
|
180
|
+
#
|
181
|
+
# @return [Boolean]
|
163
182
|
def cacheable?
|
164
183
|
unless defined?(@cacheable)
|
165
184
|
@cacheable = superclass.cacheable? if superclass.respond_to?(:cacheable?)
|
@@ -168,22 +187,27 @@ module FastSerializer
|
|
168
187
|
end
|
169
188
|
|
170
189
|
# Return the time to live in seconds for a cacheable serializer.
|
190
|
+
#
|
191
|
+
# @return [Numeric]
|
171
192
|
def cache_ttl
|
172
193
|
if defined?(@cache_ttl)
|
173
194
|
@cache_ttl
|
174
195
|
elsif superclass.respond_to?(:cache_ttl)
|
175
196
|
superclass.cache_ttl
|
176
|
-
else
|
177
|
-
nil
|
178
197
|
end
|
179
198
|
end
|
180
199
|
|
181
200
|
# Set the time to live on a cacheable serializer.
|
201
|
+
#
|
202
|
+
# @param value [Numeric] the time to live in seconds
|
203
|
+
# @return [void]
|
182
204
|
def cache_ttl=(value)
|
183
205
|
@cache_ttl = value
|
184
206
|
end
|
185
207
|
|
186
208
|
# Get the cache implemtation used to store cacheable serializers.
|
209
|
+
#
|
210
|
+
# @return [FastSerializer::Cache]
|
187
211
|
def cache
|
188
212
|
if defined?(@cache)
|
189
213
|
@cache
|
@@ -195,6 +219,9 @@ module FastSerializer
|
|
195
219
|
end
|
196
220
|
|
197
221
|
# Set the cache implementation used to store cacheable serializers.
|
222
|
+
#
|
223
|
+
# @param cache [FastSerializer::Cache]
|
224
|
+
# @return [void]
|
198
225
|
def cache=(cache)
|
199
226
|
if defined?(ActiveSupport::Cache::Store) && cache.is_a?(ActiveSupport::Cache::Store)
|
200
227
|
cache = Cache::ActiveSupportCache.new(cache)
|
@@ -214,6 +241,8 @@ module FastSerializer
|
|
214
241
|
end
|
215
242
|
|
216
243
|
# Return a list of the SerializedFields defined for the class.
|
244
|
+
#
|
245
|
+
# @return [Array<FastSerializer::SerializedField>]
|
217
246
|
def serializable_fields
|
218
247
|
unless defined?(@serializable_fields) && @serializable_fields
|
219
248
|
fields = superclass.send(:serializable_fields).dup if superclass.respond_to?(:serializable_fields)
|
@@ -234,17 +263,17 @@ module FastSerializer
|
|
234
263
|
private include_method_name
|
235
264
|
condition = include_method_name
|
236
265
|
end
|
237
|
-
|
266
|
+
|
238
267
|
field = SerializedField.new(name, optional: optional, serializer: serializer, serializer_options: serializer_options, enumerable: enumerable, condition: condition)
|
239
268
|
|
240
269
|
# Add the field to the frozen list of fields.
|
241
270
|
field_list = []
|
242
271
|
added = false
|
243
272
|
serializable_fields.each do |existing_field|
|
244
|
-
if existing_field.name == name
|
245
|
-
|
273
|
+
field_list << if existing_field.name == name
|
274
|
+
field
|
246
275
|
else
|
247
|
-
|
276
|
+
existing_field
|
248
277
|
end
|
249
278
|
end
|
250
279
|
field_list << field unless added
|
@@ -253,14 +282,14 @@ module FastSerializer
|
|
253
282
|
|
254
283
|
# Define a delegate method name +attribute+ that invokes the +field+ method on the wrapped object.
|
255
284
|
def define_delegate(attribute, field)
|
256
|
-
define_method(attribute){ object.send(field) }
|
285
|
+
define_method(attribute) { object.send(field) }
|
257
286
|
end
|
258
287
|
end
|
259
|
-
|
288
|
+
|
260
289
|
module ArrayHelper
|
261
290
|
# Helper method to serialize an array of values using this serializer.
|
262
291
|
def array(values, options = nil)
|
263
|
-
options = (options ? options.merge(:
|
292
|
+
options = (options ? options.merge(serializer: self) : {serializer: self})
|
264
293
|
FastSerializer::ArraySerializer.new(values, options)
|
265
294
|
end
|
266
295
|
end
|
@@ -287,14 +316,12 @@ module FastSerializer
|
|
287
316
|
# Serialize the wrapped object into a format suitable for passing to a JSON parser.
|
288
317
|
def as_json(*args)
|
289
318
|
return nil unless object
|
290
|
-
|
291
|
-
@_serialized = (cacheable? ? load_from_cache : load_hash).freeze
|
292
|
-
end
|
319
|
+
@_serialized ||= (cacheable? ? load_from_cache : load_hash).freeze
|
293
320
|
@_serialized
|
294
321
|
end
|
295
322
|
|
296
|
-
|
297
|
-
|
323
|
+
alias_method :to_hash, :as_json
|
324
|
+
alias_method :to_h, :as_json
|
298
325
|
|
299
326
|
# Convert the wrapped object to JSON format.
|
300
327
|
def to_json(options = {})
|
@@ -353,14 +380,14 @@ module FastSerializer
|
|
353
380
|
SerializationContext.use do
|
354
381
|
self.class.serializable_fields.each do |field|
|
355
382
|
name = field.name
|
356
|
-
|
383
|
+
|
357
384
|
if field.optional?
|
358
|
-
next unless include_fields
|
385
|
+
next unless include_fields&.include?(name)
|
359
386
|
end
|
360
387
|
next if excluded_fields && excluded_fields[name] == true
|
361
388
|
condition = field.condition
|
362
389
|
next if condition && !send(condition)
|
363
|
-
|
390
|
+
|
364
391
|
value = field.serialize(send(name), serializer_options(name))
|
365
392
|
hash[name] = value
|
366
393
|
end
|
@@ -411,7 +438,7 @@ module FastSerializer
|
|
411
438
|
end
|
412
439
|
hash_key
|
413
440
|
elsif options.is_a?(Enumerable)
|
414
|
-
options.collect{|option| options_cache_key(option)}
|
441
|
+
options.collect { |option| options_cache_key(option) }
|
415
442
|
else
|
416
443
|
options
|
417
444
|
end
|
data/lib/fast_serializer.rb
CHANGED
@@ -1,22 +1,22 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "json"
|
4
|
+
require "time"
|
5
|
+
require "date"
|
4
6
|
|
5
7
|
module FastSerializer
|
6
|
-
require_relative
|
7
|
-
require_relative
|
8
|
-
require_relative
|
9
|
-
require_relative
|
10
|
-
require_relative
|
11
|
-
require_relative
|
8
|
+
require_relative "fast_serializer/cache"
|
9
|
+
require_relative "fast_serializer/cache/active_support_cache"
|
10
|
+
require_relative "fast_serializer/serialization_context"
|
11
|
+
require_relative "fast_serializer/serialized_field"
|
12
|
+
require_relative "fast_serializer/serializer"
|
13
|
+
require_relative "fast_serializer/array_serializer"
|
12
14
|
|
13
15
|
class << self
|
14
16
|
@cache = nil
|
15
17
|
|
16
18
|
# Get the global cache implementation used for storing cacheable serializers.
|
17
|
-
|
18
|
-
@cache
|
19
|
-
end
|
19
|
+
attr_reader :cache
|
20
20
|
|
21
21
|
# Set the global cache implementation used for storing cacheable serializers.
|
22
22
|
# The cache implementation should implement the +fetch+ method as defined in
|
@@ -24,6 +24,8 @@ module FastSerializer
|
|
24
24
|
#
|
25
25
|
# In a Rails app, you can initialize the cache by simply passing in the value :rails
|
26
26
|
# to use the default Rails.cache. You can also directly pass in an ActiveSupportCache::Store.
|
27
|
+
#
|
28
|
+
# @param cache [FastSerializer::Cache, ActiveSupport::Cache::Store, Symbol] the cache to use
|
27
29
|
def cache=(cache)
|
28
30
|
if cache == :rails
|
29
31
|
cache = Cache::ActiveSupportCache.new(Rails.cache)
|
metadata
CHANGED
@@ -1,38 +1,23 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fast_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
- We Heart It
|
8
7
|
- Brian Durand
|
9
|
-
autorequire:
|
8
|
+
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2023-11-11 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
17
|
-
requirements:
|
18
|
-
- - "~>"
|
19
|
-
- !ruby/object:Gem::Version
|
20
|
-
version: '1.3'
|
21
|
-
type: :development
|
22
|
-
prerelease: false
|
23
|
-
version_requirements: !ruby/object:Gem::Requirement
|
24
|
-
requirements:
|
25
|
-
- - "~>"
|
26
|
-
- !ruby/object:Gem::Version
|
27
|
-
version: '1.3'
|
28
|
-
- !ruby/object:Gem::Dependency
|
29
|
-
name: rake
|
14
|
+
name: redis
|
30
15
|
requirement: !ruby/object:Gem::Requirement
|
31
16
|
requirements:
|
32
17
|
- - ">="
|
33
18
|
- !ruby/object:Gem::Version
|
34
19
|
version: '0'
|
35
|
-
type: :
|
20
|
+
type: :runtime
|
36
21
|
prerelease: false
|
37
22
|
version_requirements: !ruby/object:Gem::Requirement
|
38
23
|
requirements:
|
@@ -40,47 +25,29 @@ dependencies:
|
|
40
25
|
- !ruby/object:Gem::Version
|
41
26
|
version: '0'
|
42
27
|
- !ruby/object:Gem::Dependency
|
43
|
-
name:
|
44
|
-
requirement: !ruby/object:Gem::Requirement
|
45
|
-
requirements:
|
46
|
-
- - "~>"
|
47
|
-
- !ruby/object:Gem::Version
|
48
|
-
version: '3.0'
|
49
|
-
type: :development
|
50
|
-
prerelease: false
|
51
|
-
version_requirements: !ruby/object:Gem::Requirement
|
52
|
-
requirements:
|
53
|
-
- - "~>"
|
54
|
-
- !ruby/object:Gem::Version
|
55
|
-
version: '3.0'
|
56
|
-
- !ruby/object:Gem::Dependency
|
57
|
-
name: active_support
|
28
|
+
name: bundler
|
58
29
|
requirement: !ruby/object:Gem::Requirement
|
59
30
|
requirements:
|
60
31
|
- - ">="
|
61
32
|
- !ruby/object:Gem::Version
|
62
|
-
version: '
|
33
|
+
version: '0'
|
63
34
|
type: :development
|
64
35
|
prerelease: false
|
65
36
|
version_requirements: !ruby/object:Gem::Requirement
|
66
37
|
requirements:
|
67
38
|
- - ">="
|
68
39
|
- !ruby/object:Gem::Version
|
69
|
-
version: '
|
70
|
-
description:
|
71
|
-
many optimizations under the hood.
|
40
|
+
version: '0'
|
41
|
+
description:
|
72
42
|
email:
|
73
|
-
- dev@weheartit.com
|
74
43
|
- bbdurand@gmail.com
|
75
44
|
executables: []
|
76
45
|
extensions: []
|
77
46
|
extra_rdoc_files: []
|
78
47
|
files:
|
79
|
-
-
|
80
|
-
- HISTORY.md
|
48
|
+
- CHANGELOG.md
|
81
49
|
- MIT_LICENSE
|
82
50
|
- README.md
|
83
|
-
- Rakefile
|
84
51
|
- VERSION
|
85
52
|
- fast_serializer.gemspec
|
86
53
|
- lib/fast_serializer.rb
|
@@ -90,19 +57,11 @@ files:
|
|
90
57
|
- lib/fast_serializer/serialization_context.rb
|
91
58
|
- lib/fast_serializer/serialized_field.rb
|
92
59
|
- lib/fast_serializer/serializer.rb
|
93
|
-
|
94
|
-
- spec/cache/active_support_cache_spec.rb
|
95
|
-
- spec/fast_serializer_spec.rb
|
96
|
-
- spec/serialization_context_spec.rb
|
97
|
-
- spec/serialized_field_spec.rb
|
98
|
-
- spec/serializer_spec.rb
|
99
|
-
- spec/spec_helper.rb
|
100
|
-
- spec/support/test_models.rb
|
101
|
-
homepage: https://github.com/weheartit/fast_serializer
|
60
|
+
homepage: https://github.com/bdurand/fast_serializer
|
102
61
|
licenses:
|
103
62
|
- MIT
|
104
63
|
metadata: {}
|
105
|
-
post_install_message:
|
64
|
+
post_install_message:
|
106
65
|
rdoc_options: []
|
107
66
|
require_paths:
|
108
67
|
- lib
|
@@ -110,24 +69,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
110
69
|
requirements:
|
111
70
|
- - ">="
|
112
71
|
- !ruby/object:Gem::Version
|
113
|
-
version: '
|
72
|
+
version: '2.5'
|
114
73
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
74
|
requirements:
|
116
75
|
- - ">="
|
117
76
|
- !ruby/object:Gem::Version
|
118
77
|
version: '0'
|
119
78
|
requirements: []
|
120
|
-
|
121
|
-
|
122
|
-
signing_key:
|
79
|
+
rubygems_version: 3.4.12
|
80
|
+
signing_key:
|
123
81
|
specification_version: 4
|
124
|
-
summary: Super fast object serialization for API's
|
125
|
-
|
126
|
-
|
127
|
-
- spec/cache/active_support_cache_spec.rb
|
128
|
-
- spec/fast_serializer_spec.rb
|
129
|
-
- spec/serialization_context_spec.rb
|
130
|
-
- spec/serialized_field_spec.rb
|
131
|
-
- spec/serializer_spec.rb
|
132
|
-
- spec/spec_helper.rb
|
133
|
-
- spec/support/test_models.rb
|
82
|
+
summary: Super fast object serialization for API's combining a simple DSL with many
|
83
|
+
optimizations under the hood.
|
84
|
+
test_files: []
|
data/.gitignore
DELETED
data/HISTORY.md
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
### 1.1.1
|
2
|
-
|
3
|
-
* Add `array` class method to serializers.
|
4
|
-
|
5
|
-
### 1.1.0
|
6
|
-
|
7
|
-
* Add helper method for scope option.
|
8
|
-
|
9
|
-
* Pass serialization options to child serializers.
|
10
|
-
|
11
|
-
* Add `if` option to conditionally include fields.
|
12
|
-
|
13
|
-
* Better cache keys handling for more complex objects.
|
14
|
-
|
15
|
-
### 1.0.2
|
16
|
-
|
17
|
-
* Better integration with ActiveSupport caching.
|
18
|
-
|
19
|
-
### 1.0.1
|
20
|
-
|
21
|
-
* Compatibility with change to fetch_multi in ActiveSupport 4.2.
|
22
|
-
|
23
|
-
### 1.0.0
|
24
|
-
|
25
|
-
* Initial release
|
data/Rakefile
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
require "bundler/gem_tasks"
|
2
|
-
|
3
|
-
desc 'Default: run unit tests.'
|
4
|
-
task :default => :test
|
5
|
-
|
6
|
-
desc 'RVM likes to call it tests'
|
7
|
-
task :tests => :test
|
8
|
-
|
9
|
-
begin
|
10
|
-
require 'rspec'
|
11
|
-
require 'rspec/core/rake_task'
|
12
|
-
desc 'Run the unit tests'
|
13
|
-
RSpec::Core::RakeTask.new(:test)
|
14
|
-
rescue LoadError
|
15
|
-
task :test do
|
16
|
-
STDERR.puts "You must have rspec >= 2.0 installed to run the tests"
|
17
|
-
end
|
18
|
-
end
|
@@ -1,67 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe FastSerializer::ArraySerializer do
|
4
|
-
|
5
|
-
it "should serialize an array of regular objects" do
|
6
|
-
array = [1, 2, 3]
|
7
|
-
serializer = FastSerializer::ArraySerializer.new(array)
|
8
|
-
expect(serializer.as_json).to eq array
|
9
|
-
end
|
10
|
-
|
11
|
-
it "should serialize any Enumerable" do
|
12
|
-
hash = {:a => 1, :b => 2}
|
13
|
-
serializer = FastSerializer::ArraySerializer.new(hash)
|
14
|
-
expect(serializer.as_json).to eq hash.to_a
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should serializer an array of objects using a specific serializer" do
|
18
|
-
model_1 = SimpleModel.new(:id => 1, :name => "foo")
|
19
|
-
model_2 = SimpleModel.new(:id => 2, :name => "bar")
|
20
|
-
serializer = FastSerializer::ArraySerializer.new([model_1, model_2], :serializer => SimpleSerializer)
|
21
|
-
expect(JSON.load(serializer.to_json)).to eq [
|
22
|
-
{"id" => 1, "name" => "foo", "validated" => false},
|
23
|
-
{"id" => 2, "name" => "bar", "validated" => false}
|
24
|
-
]
|
25
|
-
end
|
26
|
-
|
27
|
-
it "should serializer an array of objects using a specific serializer with options" do
|
28
|
-
model_1 = SimpleModel.new(:id => 1, :name => "foo")
|
29
|
-
model_2 = SimpleModel.new(:id => 2, :name => "bar")
|
30
|
-
serializer = FastSerializer::ArraySerializer.new([model_1, model_2], :serializer => SimpleSerializer, :serializer_options => {:include => :description})
|
31
|
-
expect(JSON.load(serializer.to_json)).to eq [
|
32
|
-
{"id" => 1, "name" => "foo", "validated" => false, "description" => nil},
|
33
|
-
{"id" => 2, "name" => "bar", "validated" => false, "description" => nil}
|
34
|
-
]
|
35
|
-
end
|
36
|
-
|
37
|
-
it "should be able to use the array helper method on a serializer to serialize an array of objects" do
|
38
|
-
model_1 = SimpleModel.new(:id => 1, :name => "foo")
|
39
|
-
model_2 = SimpleModel.new(:id => 2, :name => "bar")
|
40
|
-
array_serializer = FastSerializer::ArraySerializer.new([model_1, model_2], :serializer => SimpleSerializer, :serializer_options => {:include => :description})
|
41
|
-
helper_serializer = SimpleSerializer.array([model_1, model_2], :serializer_options => {:include => :description})
|
42
|
-
expect(array_serializer.to_json).to eq helper_serializer.to_json
|
43
|
-
end
|
44
|
-
|
45
|
-
it "should not respond to_hash methods" do
|
46
|
-
array = [1, 2, 3]
|
47
|
-
serializer = FastSerializer::ArraySerializer.new(array)
|
48
|
-
expect(serializer.respond_to?(:to_hash)).to eq false
|
49
|
-
expect(serializer.respond_to?(:to_h)).to eq false
|
50
|
-
end
|
51
|
-
|
52
|
-
it "should respond to to_a" do
|
53
|
-
array = [1, 2, 3]
|
54
|
-
serializer = FastSerializer::ArraySerializer.new(array)
|
55
|
-
expect(serializer.to_a).to eq array
|
56
|
-
end
|
57
|
-
|
58
|
-
it "should pull cacheable serializers from a cache" do
|
59
|
-
model_1 = SimpleModel.new(:id => 1, :name => "foo")
|
60
|
-
model_2 = SimpleModel.new(:id => 2, :name => "bar")
|
61
|
-
serializer = FastSerializer::ArraySerializer.new([model_1, model_2], serializer: CachedSerializer)
|
62
|
-
expect(serializer.cacheable?).to eq true
|
63
|
-
already_cached_json = CachedSerializer.new(model_1).as_json
|
64
|
-
expect(serializer.as_json.collect(&:object_id)).to eq [already_cached_json.object_id, CachedSerializer.new(model_2).as_json.object_id]
|
65
|
-
end
|
66
|
-
|
67
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe FastSerializer::Cache::ActiveSupportCache do
|
4
|
-
|
5
|
-
it "should fetch from an ActiveSupport cache store" do
|
6
|
-
cache_store = ActiveSupport::Cache::MemoryStore.new
|
7
|
-
cache = FastSerializer::Cache::ActiveSupportCache.new(cache_store)
|
8
|
-
serializer = SimpleSerializer.new(SimpleModel.new(:id => 1))
|
9
|
-
|
10
|
-
expect(cache.fetch(serializer, 60){|s| s.as_json} ).to eq serializer.as_json
|
11
|
-
expect(cache.fetch(serializer, 60){ raise "boom" }).to eq serializer.as_json
|
12
|
-
end
|
13
|
-
|
14
|
-
it "should fetch multiple from an ActiveSupport cache store" do
|
15
|
-
cache_store = ActiveSupport::Cache::MemoryStore.new
|
16
|
-
cache = FastSerializer::Cache::ActiveSupportCache.new(cache_store)
|
17
|
-
s1 = SimpleSerializer.new(SimpleModel.new(:id => 1))
|
18
|
-
s2 = SimpleSerializer.new(SimpleModel.new(:id => 2))
|
19
|
-
|
20
|
-
expect(cache.fetch_all([s1, s2], 60){|s| s.as_json} ).to eq [s1.as_json, s2.as_json]
|
21
|
-
expect(cache.fetch_all([s1, s2], 60){ raise "boom" }).to eq [s1.as_json, s2.as_json]
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe FastSerializer do
|
4
|
-
|
5
|
-
it "should be able to set and get a global cache" do
|
6
|
-
expect(FastSerializer.cache).to eq nil
|
7
|
-
begin
|
8
|
-
cache = TestCache.new
|
9
|
-
FastSerializer.cache = cache
|
10
|
-
expect(FastSerializer.cache).to eq cache
|
11
|
-
ensure
|
12
|
-
FastSerializer.cache = nil
|
13
|
-
end
|
14
|
-
expect(FastSerializer.cache).to eq nil
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should set the cache to Rails.cache with the value :rails" do
|
18
|
-
begin
|
19
|
-
rails = double(:cache => :rails_cache)
|
20
|
-
stub_const("Rails", rails)
|
21
|
-
FastSerializer.cache = :rails
|
22
|
-
expect(FastSerializer.cache).to be_a FastSerializer::Cache::ActiveSupportCache
|
23
|
-
expect(FastSerializer.cache.cache).to eq :rails_cache
|
24
|
-
ensure
|
25
|
-
FastSerializer.cache = nil
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
it "should set the cache with an ActiveSupport cache" do
|
30
|
-
begin
|
31
|
-
cache_store = ActiveSupport::Cache::MemoryStore.new
|
32
|
-
FastSerializer.cache = cache_store
|
33
|
-
expect(FastSerializer.cache).to be_a FastSerializer::Cache::ActiveSupportCache
|
34
|
-
expect(FastSerializer.cache.cache).to eq cache_store
|
35
|
-
ensure
|
36
|
-
FastSerializer.cache = nil
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe FastSerializer::SerializationContext do
|
4
|
-
|
5
|
-
it "should get a single context only within a block" do
|
6
|
-
expect(FastSerializer::SerializationContext.current).to eq nil
|
7
|
-
FastSerializer::SerializationContext.use do
|
8
|
-
context = FastSerializer::SerializationContext.current
|
9
|
-
expect(FastSerializer::SerializationContext.current).to_not eq nil
|
10
|
-
FastSerializer::SerializationContext.use do
|
11
|
-
expect(FastSerializer::SerializationContext.current).to eq context
|
12
|
-
end
|
13
|
-
expect(FastSerializer::SerializationContext.current).to eq context
|
14
|
-
end
|
15
|
-
expect(FastSerializer::SerializationContext.current).to eq nil
|
16
|
-
end
|
17
|
-
|
18
|
-
it "should create serializers and reload them from cache with the same object and options" do
|
19
|
-
context = FastSerializer::SerializationContext.new
|
20
|
-
object = SimpleModel.new(:id => 1, :name => "foo")
|
21
|
-
|
22
|
-
serializer = context.load(SimpleSerializer, object, :count => 1)
|
23
|
-
expect(serializer).to be_a SimpleSerializer
|
24
|
-
expect(serializer.object).to eq object
|
25
|
-
expect(serializer.options).to eq(:count => 1)
|
26
|
-
|
27
|
-
expect(context.load(SimpleSerializer, object, :count => 1).object_id).to eq serializer.object_id
|
28
|
-
expect(context.load(SimpleSerializer, SimpleModel.new(:id => 2, :name => "bar"), :count => 1).object_id).to_not eq serializer.object_id
|
29
|
-
expect(context.load(SimpleSerializer, object, :count => 2).object_id).to_not eq serializer.object_id
|
30
|
-
end
|
31
|
-
|
32
|
-
end
|