rails_json_serializer 3.0.1 → 3.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/serializer/configuration.rb +15 -1
- data/lib/serializer/model_serializer.rb +66 -17
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a17108258803bac91fbf2506e02b2953fe1d49c4ae3758acea6a6b8671a5cf0
|
4
|
+
data.tar.gz: f8e2ad23101fe3ba7a9253d6dbeec9d82b1c5f9392ce2df8c47db74326921858
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f95e665cd9adfc8c9900ba981d75af1b2448cb8c23baa41454b63aaeaf49e5fc04846ab60d18bbc2991491d3a7b310666a863ba830fad71fee18225a88db0f38
|
7
|
+
data.tar.gz: 8f7b66cce88ba721b58ce24a76d7a75791f61054ab0da38de23a495aa1f1d20c1d93b344cc4b393dc2479727c9d589bdfd4de807563ad8cf3465239aa4011901
|
@@ -1,12 +1,26 @@
|
|
1
|
+
require "zlib"
|
2
|
+
|
1
3
|
module Serializer
|
2
4
|
class Configuration
|
3
|
-
attr_accessor :enable_includes, :default_cache_time, :disable_model_caching, :debug
|
5
|
+
attr_accessor :enable_includes, :default_cache_time, :disable_model_caching, :debug, :cache_key
|
6
|
+
attr_accessor :compress, :compressor, :decompressor
|
4
7
|
|
5
8
|
def initialize
|
6
9
|
@enable_includes = true
|
7
10
|
@default_cache_time = 360
|
8
11
|
@disable_model_caching = false
|
9
12
|
@debug = false
|
13
|
+
@compress = false
|
14
|
+
@cache_key = Proc.new { |class_name, query_name, object_id| "#{class_name}_____#{query_name}___#{object_id}" }
|
15
|
+
@compressor = Proc.new { |incoming_data| Base64.encode64(Zlib::Deflate.deflate(incoming_data.to_json)) }
|
16
|
+
|
17
|
+
# have to use 'temp_val', or else there's an issue with the libraries. Won't work as a 1-liner
|
18
|
+
@decompressor = Proc.new do |outgoing_data|
|
19
|
+
temp_val1 = Base64.decode64(outgoing_data)
|
20
|
+
temp_val2 = Zlib::Inflate.inflate(temp_val1)
|
21
|
+
JSON.parse(temp_val2)
|
22
|
+
end
|
23
|
+
|
10
24
|
end
|
11
25
|
end
|
12
26
|
end
|
@@ -4,6 +4,8 @@ module ModelSerializer
|
|
4
4
|
# START CLASS EVAL
|
5
5
|
klass.class_eval do
|
6
6
|
|
7
|
+
|
8
|
+
# I don't think we need to check for classname + serialiers. We are now including via: `include ModelSerializer` in classes
|
7
9
|
# Rails 5 has autoloading issues with modules. They will show as not defined, when available.
|
8
10
|
if Rails.env.development?
|
9
11
|
begin
|
@@ -23,10 +25,31 @@ module ModelSerializer
|
|
23
25
|
# Inject class methods, will have access to those queries on the class.
|
24
26
|
klass.send(:extend, serializer_klass)
|
25
27
|
|
28
|
+
# Class method to clear the cache of objects without having to instantiate them.
|
29
|
+
def self.clear_serializer_cache id_or_ids
|
30
|
+
if !id_or_ids.is_a?(Array)
|
31
|
+
id_or_ids = [id_or_ids]
|
32
|
+
end
|
33
|
+
id_or_ids.each do |object_id|
|
34
|
+
self::SERIALIZER_QUERY_KEYS_CACHE.each do |query_name|
|
35
|
+
cache_key = Serializer.configuration.cache_key.call(self.name, query_name, object_id)
|
36
|
+
Rails.logger.debug "(class) CLEARING SERIALIZER CACHE: #{cache_key}" if Serializer.configuration.debug
|
37
|
+
Rails.cache.delete(cache_key)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
26
42
|
# no need to define it if inheriting class has defined it OR has been manually overridden.
|
27
|
-
# CLASS
|
28
|
-
klass.send(:define_singleton_method, :serializer) do |opts = {}|
|
43
|
+
# CLASS METHOD - :serializer
|
44
|
+
klass.send(:define_singleton_method, :serializer) do |opts = {}|
|
45
|
+
# puts "CLASS METHOD - SERIALIZER - OPTS: #{opts.inspect}"
|
29
46
|
query = opts[:json_query_override].present? ? self.send(opts[:json_query_override], opts) : serializer_query(opts)
|
47
|
+
|
48
|
+
# if as_json arg, then add to as_json query
|
49
|
+
if opts[:disable_caching] == true
|
50
|
+
query[:disable_caching] = true
|
51
|
+
end
|
52
|
+
|
30
53
|
if Serializer.configuration.enable_includes && query[:include].present? && !opts[:skip_eager_loading]
|
31
54
|
includes(generate_includes_from_json_query(query)).as_json(query)
|
32
55
|
else
|
@@ -107,29 +130,47 @@ module ModelSerializer
|
|
107
130
|
return super(options)
|
108
131
|
end
|
109
132
|
options[:ran_serialization] = true
|
133
|
+
|
110
134
|
# Not caching records that don't have IDs.
|
111
135
|
if !Serializer.configuration.disable_model_caching && self.id && options[:cache_key].present? && !(options.key?(:cache_for) && options[:cache_for].nil?)
|
112
|
-
cache_key =
|
113
|
-
|
114
|
-
|
115
|
-
|
136
|
+
cache_key = Serializer.configuration.cache_key.call(self.class.name, options[:cache_key], self.id)
|
137
|
+
|
138
|
+
if Rails.cache.exist?(cache_key) && options[:disable_caching] != true
|
139
|
+
Rails.logger.debug "Serializer: Cache reading #{cache_key}" if Serializer.configuration.debug
|
140
|
+
# Rails.logger.debug(options.inspect) if Serializer.configuration.debug
|
141
|
+
outgoing_data = Rails.cache.read(cache_key)
|
142
|
+
if (options.key?(:compress) && options[:compress] == true) || (!options.key?(:compress) && Serializer.configuration.compress)
|
143
|
+
outgoing_data = Serializer.configuration.decompressor.call(outgoing_data)
|
144
|
+
end
|
145
|
+
return outgoing_data
|
116
146
|
else
|
117
147
|
data = super(options)
|
118
148
|
data = self.class.as_json_associations_alias_fix(options, data)
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
149
|
+
|
150
|
+
if options[:disable_caching] != true
|
151
|
+
# compress data
|
152
|
+
cachable_data = data
|
153
|
+
if (options.key?(:compress) && options[:compress] == true) || (!options.key?(:compress) && Serializer.configuration.compress)
|
154
|
+
cachable_data = Serializer.configuration.compressor.call(data)
|
155
|
+
end
|
156
|
+
begin
|
157
|
+
Rails.logger.debug "Serializer: Caching #{cache_key} for #{(options[:cache_for] || Serializer.configuration.default_cache_time)} minutes." if Serializer.configuration.debug
|
158
|
+
Rails.cache.write(cache_key, cachable_data, expires_in: (options[:cache_for] || Serializer.configuration.default_cache_time).minute)
|
159
|
+
rescue Exception => e
|
160
|
+
Rails.logger.error "Serializer: Internal Server Error on #{self.class}#as_json ID: #{self.id} for cache key: #{cache_key}"
|
161
|
+
Rails.logger.error e.class
|
162
|
+
Rails.logger.error e.message
|
163
|
+
Rails.logger.error e.backtrace
|
164
|
+
end
|
165
|
+
else
|
166
|
+
Rails.logger.debug "Serializer: Cache reading/writing for #{cache_key} is disabled" if Serializer.configuration.debug
|
127
167
|
end
|
168
|
+
|
128
169
|
return data
|
129
170
|
end
|
130
171
|
else
|
131
172
|
if Serializer.configuration.debug && !Serializer.configuration.disable_model_caching && self.id && options[:cache_key].present? && options.key?(:cache_for) && options[:cache_for].nil?
|
132
|
-
Rails.logger.
|
173
|
+
Rails.logger.debug "Serializer: Caching #{cache_key} NOT caching due to `cache_for: nil`"
|
133
174
|
end
|
134
175
|
data = super(options)
|
135
176
|
data = self.class.as_json_associations_alias_fix(options, data)
|
@@ -137,9 +178,17 @@ module ModelSerializer
|
|
137
178
|
end
|
138
179
|
end
|
139
180
|
|
181
|
+
# INSTANCE METHOD - :serializer
|
140
182
|
if !klass.method_defined?(:serializer)
|
141
183
|
klass.send(:define_method, :serializer) do |opts = {}|
|
184
|
+
# puts "INSTANCE METHOD - SERIALIZER - OPTS: #{opts.inspect}"
|
142
185
|
query = opts[:json_query_override].present? ? self.class.send(opts[:json_query_override], opts) : self.class.serializer_query(opts)
|
186
|
+
|
187
|
+
# if as_json arg, then add to as_json query
|
188
|
+
if opts[:disable_caching] == true
|
189
|
+
query[:disable_caching] = true
|
190
|
+
end
|
191
|
+
|
143
192
|
if Serializer.configuration.enable_includes && query[:include].present? && self.class.column_names.include?('id') && self.id.present? && !opts[:skip_eager_loading] && self.respond_to?(:persisted?) && self.persisted?
|
144
193
|
# It's an extra SQL call, but most likely worth it to pre-load associations
|
145
194
|
self.class.includes(self.class.generate_includes_from_json_query(query)).find(self.id).as_json(query)
|
@@ -153,8 +202,8 @@ module ModelSerializer
|
|
153
202
|
klass.send(:define_method, :clear_serializer_cache) do
|
154
203
|
if self.class.const_defined?("SERIALIZER_QUERY_KEYS_CACHE")
|
155
204
|
self.class::SERIALIZER_QUERY_KEYS_CACHE.each do |query_name|
|
156
|
-
cache_key =
|
157
|
-
Rails.logger.
|
205
|
+
cache_key = Serializer.configuration.cache_key.call(self.class.name, query_name, self.id)
|
206
|
+
Rails.logger.debug "Serializer: CLEARING CACHE KEY: #{cache_key}" if Serializer.configuration.debug
|
158
207
|
Rails.cache.delete(cache_key)
|
159
208
|
end
|
160
209
|
return true
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_json_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0
|
4
|
+
version: 3.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- benjamin.dana.software.dev@gmail.com
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '5.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: zlib
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rails
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -136,7 +150,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
136
150
|
- !ruby/object:Gem::Version
|
137
151
|
version: '0'
|
138
152
|
requirements: []
|
139
|
-
rubygems_version: 3.
|
153
|
+
rubygems_version: 3.1.4
|
140
154
|
signing_key:
|
141
155
|
specification_version: 4
|
142
156
|
summary: An ActiveRecord JSON Serializer with supported caching and eager-loading
|