protobuf-activerecord 3.5.1 → 3.6.0

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
- SHA1:
3
- metadata.gz: 827bc732cf495b289b6580b9310b54d599d9a75b
4
- data.tar.gz: 31fd6c04948278ca2d2cdcac5f7d6a0cfface6c8
2
+ SHA256:
3
+ metadata.gz: b0baf0983fc1d7f24b7269e151478bea798a8f1193037541df17a8818b2ba455
4
+ data.tar.gz: d5f4537f6c7d8967d268435f88872f5f1632ba6ca1fdfd318917a349aaef1879
5
5
  SHA512:
6
- metadata.gz: 8fa7a80773a2071c3768b6dcfbd032d2cd2cae6e777fbf45563047a84c55cf3c6ded0ce861e1c0e8b1120a87abbdae00e6f1549579fc772ffcd39de7b81ea4d7
7
- data.tar.gz: 432524aadcb200fbc52c2eeb099dd29020b197658c30c5257d1f99748924ef04672f278e5c3ec27b9cfd4d81fc5b9541a7c653e40ddabf9d863263a8074a853a
6
+ metadata.gz: 6d20b5632121f432b90ab19b52bee9ccd75c39ad8473e3b8f97ecbf87bf8d84297581b64a5b6831050f9dea1a4b7b553e30431049cb186dd8be35f12b045fdcc
7
+ data.tar.gz: a32ced82a52061de22ac52f236df76f35c38870edf8b28693d2a4ae2d093274684a3224fcc181b893a0bf214f65319fe29a73410caf2316b2357694b05052cb9
@@ -20,6 +20,10 @@ module Protobuf
20
20
  end
21
21
 
22
22
  module ClassMethods
23
+ def _protobuf_field_objects
24
+ @_protobuf_field_objects ||= {}
25
+ end
26
+
23
27
  def _protobuf_field_options
24
28
  @_protobuf_field_options ||= {}
25
29
  end
@@ -139,96 +143,115 @@ module Protobuf
139
143
  @protobuf_message
140
144
  end
141
145
 
142
- def _protobuf_write_collection_assocation_method(field)
143
- self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
144
- def _protobuf_active_record_serialize_#{field}
145
- value = #{field}.to_a # Terminate the association
146
- rescue NameError # Has happened when field is not on model or ignored from db
147
- return nil
146
+ class CollectionAssociationCaller
147
+ def initialize(method_name)
148
+ @method_name = method_name
149
+ end
150
+
151
+ def call(selph)
152
+ selph.__send__(@method_name).to_a
153
+ rescue NameError # Has happened when field is not on model or ignored from db
154
+ return nil
155
+ end
156
+ end
157
+
158
+ def _protobuf_collection_association_object(field)
159
+ CollectionAssociationCaller.new(field)
160
+ end
161
+
162
+ class DateCaller
163
+ def initialize(field)
164
+ @field = field
165
+ end
166
+
167
+ def call(selph)
168
+ value = selph.__send__(@field)
169
+
170
+ if value
171
+ value.to_time(:utc).to_i
172
+ else
173
+ nil
148
174
  end
149
- RUBY
175
+ rescue NameError # Has happened when field is not on model or ignored from db
176
+ return nil
177
+ end
178
+ end
179
+
180
+ class DateTimeCaller
181
+ def initialize(field)
182
+ @field = field
183
+ end
184
+
185
+ def call(selph)
186
+ value = selph.__send__(@field)
187
+
188
+ if value
189
+ value.to_i
190
+ else
191
+ nil
192
+ end
193
+ rescue NameError # Has happened when field is not on model or ignored from db
194
+ return nil
195
+ end
196
+ end
197
+
198
+ class NoConversionCaller
199
+ def initialize(field)
200
+ @field = field
201
+ end
202
+
203
+ def call(selph)
204
+ selph.__send__(@field)
205
+ rescue NameError # Has happened when field is not on model or ignored from db
206
+ return nil
207
+ end
150
208
  end
151
209
 
152
- def _protobuf_write_convert_to_fields_method(field)
210
+ def _protobuf_convert_to_fields_object(field)
153
211
  is_datetime_time_or_timestamp_column = _protobuf_date_datetime_time_or_timestamp_column?(field)
154
212
  is_date_column = _protobuf_date_column?(field)
155
213
 
156
214
  if is_datetime_time_or_timestamp_column
157
215
  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
- rescue NameError # Has happened when field is not on model or ignored from db
165
- return nil
166
- end
167
- RUBY
216
+ DateCaller.new(field)
168
217
  else
169
- self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
170
- def _protobuf_active_record_serialize_#{field}
171
- value = #{field}
172
- return nil if value.nil?
173
-
174
- value.to_i
175
- rescue NameError # Has happened when field is not on model or ignored from db
176
- return nil
177
- end
178
- RUBY
218
+ DateTimeCaller.new(field)
179
219
  end
180
220
  else
181
- self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
182
- def _protobuf_active_record_serialize_#{field}
183
- #{field}
184
- rescue NameError # Has happened when field is not on model or ignored from db
185
- return nil
186
- end
187
- RUBY
221
+ NoConversionCaller.new(field)
188
222
  end
189
223
  end
190
224
 
191
- def _protobuf_write_field_transformer_method(field)
192
- self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
193
- def _protobuf_active_record_serialize_#{field}
194
- self.class._protobuf_field_transformers[:#{field}].call(self)
195
- end
196
- RUBY
225
+ def _protobuf_field_transformer_object(field)
226
+ _protobuf_field_transformers[field]
197
227
  end
198
228
 
199
- def _protobuf_write_nil_method(field)
200
- self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
201
- def _protobuf_active_record_serialize_#{field}
202
- nil
203
- end
204
- RUBY
229
+ class NilMethodCaller
230
+ def initialize; end
231
+
232
+ def call(selph)
233
+ nil
234
+ end
205
235
  end
206
236
 
207
- def _protobuf_write_symbol_transformer_method(field)
208
- transformer_method = _protobuf_field_symbol_transformers[field]
237
+ def _protobuf_nil_object(field)
238
+ NilMethodCaller.new
239
+ end
209
240
 
210
- if self.respond_to?(transformer_method, true)
211
- if self.respond_to?(transformer_method, false)
212
- self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
213
- def _protobuf_active_record_serialize_#{field}
214
- self.class.#{transformer_method}(self)
215
- end
216
- RUBY
217
- else
218
- self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
219
- def _protobuf_active_record_serialize_#{field}
220
- self.class.__send__(:#{transformer_method}, self)
221
- end
222
- RUBY
223
- end
224
- else
225
- self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
226
- def _protobuf_active_record_serialize_#{field}
227
- #{transformer_method}
228
- end
229
- RUBY
241
+ class FieldSymbolTransformerCaller
242
+ def initialize(instance_class, method_name)
243
+ @instance_class = instance_class
244
+ @method_name = method_name
245
+ end
246
+
247
+ def call(selph)
248
+ @instance_class.__send__(@method_name, selph)
230
249
  end
231
250
  end
251
+
252
+ def _protobuf_symbol_transformer_object(field)
253
+ FieldSymbolTransformerCaller.new(self, _protobuf_symbol_transformer_object[field])
254
+ end
232
255
  end
233
256
 
234
257
  # :nodoc:
@@ -301,34 +324,32 @@ module Protobuf
301
324
  # in terms of optimization (`while` is slightly faster as no block carried through)
302
325
  while attribute_number < limit
303
326
  field = field_attributes[attribute_number]
327
+ field_object = _protobuf_field_objects(field)
328
+ hash[field] = field_object.call(self)
329
+ attribute_number += 1
330
+ end
304
331
 
305
- begin
306
- hash[field] = __send__("_protobuf_active_record_serialize_#{field}")
307
- rescue NoMethodError => error
308
- raise unless error.message =~ /_protobuf_active_record_serialize/i
309
-
310
- case
311
- when _protobuf_field_symbol_transformers.has_key?(field) then
312
- self.class._protobuf_write_symbol_transformer_method(field)
313
- when _protobuf_field_transformers.has_key?(field) then
314
- self.class._protobuf_write_field_transformer_method(field)
315
- when respond_to?(field) then
316
- if _is_collection_association?(field)
317
- self.class._protobuf_write_collection_assocation_method(field)
318
- else
319
- self.class._protobuf_write_convert_to_fields_method(field)
320
- end
332
+ hash
333
+ end
334
+
335
+ # :nodoc:
336
+ def _protobuf_field_objects(field)
337
+ self.class._protobuf_field_objects[field] ||= begin
338
+ case
339
+ when _protobuf_field_symbol_transformers.has_key?(field) then
340
+ self.class._protobuf_symbol_transformer_object(field)
341
+ when _protobuf_field_transformers.has_key?(field) then
342
+ self.class._protobuf_field_transformer_object(field)
343
+ when respond_to?(field) then
344
+ if _is_collection_association?(field)
345
+ self.class._protobuf_collection_association_object(field)
321
346
  else
322
- self.class._protobuf_write_nil_method(field)
347
+ self.class._protobuf_convert_to_fields_object(field)
323
348
  end
324
-
325
- hash[field] = __send__("_protobuf_active_record_serialize_#{field}")
349
+ else
350
+ self.class._protobuf_nil_object(field)
326
351
  end
327
-
328
- attribute_number += 1
329
352
  end
330
-
331
- hash
332
353
  end
333
354
 
334
355
  # :nodoc:
@@ -1,5 +1,5 @@
1
1
  module Protobuf
2
2
  module ActiveRecord
3
- VERSION = "3.5.1"
3
+ VERSION = "3.6.0"
4
4
  end
5
5
  end
@@ -30,6 +30,7 @@ Gem::Specification.new do |spec|
30
30
  ##
31
31
  # Development dependencies
32
32
  #
33
+ spec.add_development_dependency "benchmark-ips"
33
34
  spec.add_development_dependency "rake"
34
35
  spec.add_development_dependency "rspec", ">= 3.3.0"
35
36
  spec.add_development_dependency "rspec-pride", ">= 3.1.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protobuf-activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.5.1
4
+ version: 3.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Hutchison
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-09 00:00:00.000000000 Z
11
+ date: 2019-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: benchmark-ips
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: rake
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -249,8 +263,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
249
263
  - !ruby/object:Gem::Version
250
264
  version: '0'
251
265
  requirements: []
252
- rubyforge_project:
253
- rubygems_version: 2.6.10
266
+ rubygems_version: 3.0.1
254
267
  signing_key:
255
268
  specification_version: 4
256
269
  summary: Google Protocol Buffers integration for Active Record