protobuf-activerecord 3.3.4 → 3.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/protobuf/active_record/middleware/query_cache.rb +12 -8
- data/lib/protobuf/active_record/scope.rb +0 -4
- data/lib/protobuf/active_record/serialization.rb +86 -21
- data/lib/protobuf/active_record/version.rb +1 -1
- data/protobuf-activerecord.gemspec +1 -1
- data/spec/protobuf/active_record/scope_spec.rb +1 -1
- data/spec/protobuf/active_record/serialization_spec.rb +4 -10
- data/spec/support/models/user.rb +2 -0
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6eb619048fc322fd28447c597d45c2b640f9de25
|
4
|
+
data.tar.gz: 2c964967c403700a0fc10050c2d366be88a1767e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d50153357f7874cbff551c4f80a1ce37cc05b2fbfc484a13bae35b0c46ce950f2d6718d9a5c09d21647216cf10295d1a9a62b37f10c8cb0e4a0c16f98d236645
|
7
|
+
data.tar.gz: e864f028ee4a138ae78f2e0930a4fe32a999147c7ef5b4cb00f8871600646ff98b99a3b7ce3063212e9e9844e23df57e1a310837e1ef0acf8d1b18f97b1c84fd
|
@@ -1,27 +1,31 @@
|
|
1
|
+
require "thread"
|
2
|
+
|
1
3
|
module Protobuf
|
2
4
|
module ActiveRecord
|
3
5
|
module Middleware
|
4
6
|
class QueryCache
|
7
|
+
CURRENT_CONNECTION = "_protobuf_active_record_current_connection".freeze
|
8
|
+
|
5
9
|
def initialize(app)
|
6
10
|
@app = app
|
7
11
|
end
|
8
12
|
|
9
13
|
def call(env)
|
10
|
-
|
11
|
-
|
12
|
-
|
14
|
+
connection = ::Thread.current[CURRENT_CONNECTION] = ::ActiveRecord::Base.connection
|
15
|
+
enabled = connection.query_cache_enabled
|
16
|
+
connection.enable_query_cache!
|
13
17
|
|
14
18
|
@app.call(env)
|
15
19
|
ensure
|
16
|
-
restore_query_cache_settings(
|
20
|
+
restore_query_cache_settings(enabled)
|
17
21
|
end
|
18
22
|
|
19
23
|
private
|
20
24
|
|
21
|
-
def restore_query_cache_settings(
|
22
|
-
::
|
23
|
-
::
|
24
|
-
::
|
25
|
+
def restore_query_cache_settings(enabled)
|
26
|
+
::Thread.current[CURRENT_CONNECTION].clear_query_cache
|
27
|
+
::Thread.current[CURRENT_CONNECTION].disable_query_cache! unless enabled
|
28
|
+
::Thread.current[CURRENT_CONNECTION] = nil
|
25
29
|
end
|
26
30
|
end
|
27
31
|
end
|
@@ -95,10 +95,6 @@ module Protobuf
|
|
95
95
|
searchable_fields.each do |field, scope_name|
|
96
96
|
next unless proto.respond_to_and_has_and_present?(field)
|
97
97
|
|
98
|
-
unless self.respond_to?(scope_name)
|
99
|
-
raise SearchScopeError, "Undefined scope :#{scope_name}."
|
100
|
-
end
|
101
|
-
|
102
98
|
search_values = parse_search_values(proto, field)
|
103
99
|
search_relation = search_relation.__send__(scope_name, *search_values)
|
104
100
|
end
|
@@ -42,10 +42,6 @@ module Protobuf
|
|
42
42
|
@_protobuf_field_transformers ||= {}
|
43
43
|
end
|
44
44
|
|
45
|
-
def _protobuf_instance_respond_to_from_cache?(key)
|
46
|
-
_protobuf_respond_to_cache.include?(key)
|
47
|
-
end
|
48
|
-
|
49
45
|
def _protobuf_message_deprecated_fields
|
50
46
|
@_protobuf_message_deprecated_fields ||= begin
|
51
47
|
self.protobuf_message.all_fields.map do |field|
|
@@ -68,10 +64,6 @@ module Protobuf
|
|
68
64
|
end
|
69
65
|
end
|
70
66
|
|
71
|
-
def _protobuf_respond_to_cache
|
72
|
-
@_protobuf_respond_to_cache ||= ::Set.new
|
73
|
-
end
|
74
|
-
|
75
67
|
# Define a field transformation from a record. Accepts a Symbol,
|
76
68
|
# callable, or block that is called with the record being serialized.
|
77
69
|
#
|
@@ -156,6 +148,73 @@ module Protobuf
|
|
156
148
|
|
157
149
|
@protobuf_message
|
158
150
|
end
|
151
|
+
|
152
|
+
def _protobuf_write_convert_to_fields_method(field)
|
153
|
+
is_datetime_time_or_timestamp_column = _protobuf_date_datetime_time_or_timestamp_column?(field)
|
154
|
+
is_date_column = _protobuf_date_column?(field)
|
155
|
+
|
156
|
+
if is_datetime_time_or_timestamp_column
|
157
|
+
if is_date_column
|
158
|
+
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
159
|
+
def _protobuf_active_record_serialize_#{field}
|
160
|
+
value = #{field}
|
161
|
+
return nil if value.nil?
|
162
|
+
|
163
|
+
value.to_time(:utc).to_i
|
164
|
+
end
|
165
|
+
RUBY
|
166
|
+
else
|
167
|
+
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
168
|
+
def _protobuf_active_record_serialize_#{field}
|
169
|
+
value = #{field}
|
170
|
+
return nil if value.nil?
|
171
|
+
|
172
|
+
value.to_i
|
173
|
+
end
|
174
|
+
RUBY
|
175
|
+
end
|
176
|
+
else
|
177
|
+
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
178
|
+
def _protobuf_active_record_serialize_#{field}
|
179
|
+
#{field}
|
180
|
+
end
|
181
|
+
RUBY
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
def _protobuf_write_field_transformer_method(field)
|
186
|
+
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
187
|
+
def _protobuf_active_record_serialize_#{field}
|
188
|
+
self.class._protobuf_field_transformers[:#{field}].call(self)
|
189
|
+
end
|
190
|
+
RUBY
|
191
|
+
end
|
192
|
+
|
193
|
+
def _protobuf_write_nil_method(field)
|
194
|
+
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
195
|
+
def _protobuf_active_record_serialize_#{field}
|
196
|
+
nil
|
197
|
+
end
|
198
|
+
RUBY
|
199
|
+
end
|
200
|
+
|
201
|
+
def _protobuf_write_symbol_transformer_method(field)
|
202
|
+
transformer_method = _protobuf_field_symbol_transformers[field]
|
203
|
+
|
204
|
+
if self.methods.include?(transformer_method)
|
205
|
+
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
206
|
+
def _protobuf_active_record_serialize_#{field}
|
207
|
+
self.class.#{transformer_method}(self)
|
208
|
+
end
|
209
|
+
RUBY
|
210
|
+
else
|
211
|
+
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
212
|
+
def _protobuf_active_record_serialize_#{field}
|
213
|
+
#{transformer_method}
|
214
|
+
end
|
215
|
+
RUBY
|
216
|
+
end
|
217
|
+
end
|
159
218
|
end
|
160
219
|
|
161
220
|
# :nodoc:
|
@@ -220,19 +279,25 @@ module Protobuf
|
|
220
279
|
# in terms of optimization (`while` is slightly faster as no block carried through)
|
221
280
|
while attribute_number < limit
|
222
281
|
field = field_attributes[attribute_number]
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
282
|
+
|
283
|
+
begin
|
284
|
+
hash[field] = __send__("_protobuf_active_record_serialize_#{field}")
|
285
|
+
rescue NoMethodError => error
|
286
|
+
raise unless error.message =~ /_protobuf_active_record_serialize/i
|
287
|
+
|
288
|
+
case
|
289
|
+
when _protobuf_field_symbol_transformers.has_key?(field) then
|
290
|
+
self.class._protobuf_write_symbol_transformer_method(field)
|
291
|
+
when _protobuf_field_transformers.has_key?(field) then
|
292
|
+
self.class._protobuf_write_field_transformer_method(field)
|
293
|
+
when respond_to?(field) then
|
294
|
+
self.class._protobuf_write_convert_to_fields_method(field)
|
295
|
+
else
|
296
|
+
self.class._protobuf_write_nil_method(field)
|
297
|
+
end
|
298
|
+
|
299
|
+
hash[field] = __send__("_protobuf_active_record_serialize_#{field}")
|
300
|
+
end
|
236
301
|
|
237
302
|
attribute_number += 1
|
238
303
|
end
|
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
##
|
22
22
|
# Dependencies
|
23
23
|
#
|
24
|
-
spec.add_dependency "activerecord", "
|
24
|
+
spec.add_dependency "activerecord", "~> 4.2"
|
25
25
|
spec.add_dependency "activesupport", ">= 3.2"
|
26
26
|
spec.add_dependency "heredity", ">= 0.1.1"
|
27
27
|
spec.add_dependency "protobuf", ">= 3.0"
|
@@ -42,7 +42,7 @@ describe Protobuf::ActiveRecord::Scope do
|
|
42
42
|
|
43
43
|
it "raises an exception" do
|
44
44
|
allow(User).to receive(:searchable_fields).and_return({ :email => :by_hullabaloo })
|
45
|
-
expect { User.search_scope(request) }.to raise_exception(/
|
45
|
+
expect { User.search_scope(request) }.to raise_exception(/undefined method .*by_hullabaloo/i)
|
46
46
|
end
|
47
47
|
end
|
48
48
|
end
|
@@ -222,11 +222,6 @@ describe Protobuf::ActiveRecord::Serialization do
|
|
222
222
|
|
223
223
|
context "when a transformer is defined for the field" do
|
224
224
|
let(:fields_from_record) { { :guid => user.guid, :name => user.name, :email => user.email, :email_domain => 'test.co', :password => nil, :nullify => nil } }
|
225
|
-
let(:transformer) { { :email_domain => lambda { |record| record.email.split('@').last } } }
|
226
|
-
|
227
|
-
before {
|
228
|
-
allow(User).to receive(:_protobuf_field_transformers).and_return(transformer)
|
229
|
-
}
|
230
225
|
|
231
226
|
it "gets the field from the transformer" do
|
232
227
|
expect(user.fields_from_record).to eq fields_from_record
|
@@ -236,14 +231,13 @@ describe Protobuf::ActiveRecord::Serialization do
|
|
236
231
|
context "when a transformer is not defined for the field" do
|
237
232
|
let(:fields_from_record) { { :guid => user.guid, :name => user.name, :email => user.email, :email_domain => nil, :password => nil, :nullify => nil } }
|
238
233
|
|
234
|
+
before {
|
235
|
+
allow(user).to receive(:_protobuf_active_record_serialize_email_domain).and_return(nil)
|
236
|
+
}
|
237
|
+
|
239
238
|
it "returns a hash of protobuf fields that this object has getters for" do
|
240
239
|
expect(user.fields_from_record).to eq fields_from_record
|
241
240
|
end
|
242
|
-
|
243
|
-
it "converts attributes values for protobuf messages" do
|
244
|
-
expect(user).to receive(:_protobuf_convert_attributes_to_fields).at_least(:once)
|
245
|
-
user.fields_from_record
|
246
|
-
end
|
247
241
|
end
|
248
242
|
|
249
243
|
context "given options with :include" do
|
data/spec/support/models/user.rb
CHANGED
@@ -10,6 +10,8 @@ class User < ActiveRecord::Base
|
|
10
10
|
attribute_from_proto :last_name, :extract_last_name
|
11
11
|
attribute_from_proto :password, lambda { |proto| proto.password! }
|
12
12
|
|
13
|
+
field_from_record :email_domain, lambda { |record| record.email.split('@').last }
|
14
|
+
|
13
15
|
def self.extract_first_name(proto)
|
14
16
|
if proto.field?(:name)
|
15
17
|
names = proto.name.split(" ")
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: protobuf-activerecord
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.3.
|
4
|
+
version: 3.3.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Hutchison
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-03-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '4.2'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '4.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: activesupport
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -234,7 +234,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
234
234
|
version: '0'
|
235
235
|
requirements: []
|
236
236
|
rubyforge_project:
|
237
|
-
rubygems_version: 2.
|
237
|
+
rubygems_version: 2.6.10
|
238
238
|
signing_key:
|
239
239
|
specification_version: 4
|
240
240
|
summary: Google Protocol Buffers integration for Active Record
|