protobuf-activerecord 3.6.0 → 3.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +28 -0
  3. data/.travis.yml +2 -5
  4. data/Gemfile +1 -1
  5. data/Rakefile +10 -6
  6. data/lib/protobuf-activerecord.rb +15 -13
  7. data/lib/protobuf/active_record/attribute_methods.rb +2 -2
  8. data/lib/protobuf/active_record/columns.rb +4 -4
  9. data/lib/protobuf/active_record/mass_assignment_security.rb +3 -3
  10. data/lib/protobuf/active_record/mass_assignment_security/persistence.rb +3 -3
  11. data/lib/protobuf/active_record/mass_assignment_security/transformation.rb +1 -1
  12. data/lib/protobuf/active_record/middleware/connection_management_async.rb +9 -4
  13. data/lib/protobuf/active_record/model.rb +10 -10
  14. data/lib/protobuf/active_record/persistence.rb +1 -1
  15. data/lib/protobuf/active_record/scope.rb +2 -2
  16. data/lib/protobuf/active_record/serialization.rb +14 -23
  17. data/lib/protobuf/active_record/transformation.rb +9 -10
  18. data/lib/protobuf/active_record/transformer.rb +1 -3
  19. data/lib/protobuf/active_record/validations.rb +2 -2
  20. data/lib/protobuf/active_record/version.rb +1 -1
  21. data/protobuf-activerecord.gemspec +9 -7
  22. data/spec/protobuf/active_record/columns_spec.rb +6 -6
  23. data/spec/protobuf/active_record/nested_attributes_spec.rb +1 -2
  24. data/spec/protobuf/active_record/persistence_spec.rb +3 -3
  25. data/spec/protobuf/active_record/scope_spec.rb +6 -7
  26. data/spec/protobuf/active_record/serialization_spec.rb +22 -14
  27. data/spec/protobuf/active_record/transformation_spec.rb +19 -19
  28. data/spec/protobuf/active_record/transformer_spec.rb +13 -13
  29. data/spec/support/db.rb +1 -1
  30. data/spec/support/db/setup.rb +3 -3
  31. data/spec/support/definitions/messages.proto +11 -7
  32. data/spec/support/models.rb +2 -2
  33. data/spec/support/models/user.rb +7 -1
  34. data/spec/support/protobuf/messages.pb.rb +2 -0
  35. metadata +18 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b0baf0983fc1d7f24b7269e151478bea798a8f1193037541df17a8818b2ba455
4
- data.tar.gz: d5f4537f6c7d8967d268435f88872f5f1632ba6ca1fdfd318917a349aaef1879
3
+ metadata.gz: c25dac0d341a98ef1a2063256abf9d20b2752f58280dfe32bd45a4e273fec2d0
4
+ data.tar.gz: 77d0019eb2a66d0938beb272b773354f1d4fe55faf0b1390c908a44092dd4937
5
5
  SHA512:
6
- metadata.gz: 6d20b5632121f432b90ab19b52bee9ccd75c39ad8473e3b8f97ecbf87bf8d84297581b64a5b6831050f9dea1a4b7b553e30431049cb186dd8be35f12b045fdcc
7
- data.tar.gz: a32ced82a52061de22ac52f236df76f35c38870edf8b28693d2a4ae2d093274684a3224fcc181b893a0bf214f65319fe29a73410caf2316b2357694b05052cb9
6
+ metadata.gz: 0febfd5d96839e78688ad1b9700a07063f37a1b6f3f9f8e0e8d643a65541192f88d36c6fb8cd28b219539d9c3a02e67cf57f6e5bfc5f7e3846f9c5aff3b79c39
7
+ data.tar.gz: b534f6c7b313dac85bf4002f425d29e3be14b027ddbba2ea4b8be7b9e0187bfe400b327593d961f7c6b73276e5a6ebd08163473236c1f811a2746881f764a5d8
@@ -0,0 +1,28 @@
1
+ # Styles are inherited from Mad Rubocop
2
+
3
+ inherit_gem:
4
+ mad_rubocop: .rubocop.yml
5
+
6
+ # Styles that are modified from the defaults
7
+ AllCops:
8
+ TargetRubyVersion: 2.5
9
+ Exclude:
10
+ - "**/*.pb.rb"
11
+
12
+ Style/BlockDelimiters:
13
+ Description: >-
14
+ Avoid using {...} for multi-line blocks (multiline chaining is always ugly).
15
+ Prefer {...} over do...end for single-line blocks.
16
+ Exclude:
17
+ - "spec/**/*"
18
+
19
+ Style/HashSyntax:
20
+ Description: >-
21
+ Prefer Ruby 1.8 hash syntax { :a => 1, :b => 2 } over 1.9 syntax { a: 1, b: 2 }.
22
+ EnforcedStyle: hash_rockets
23
+ Exclude:
24
+ - "Gemfile"
25
+
26
+ Layout/SpaceAroundOperators:
27
+ Exclude:
28
+ - "*.gemspec"
@@ -1,8 +1,5 @@
1
1
  ---
2
2
  language: ruby
3
3
  rvm:
4
- - 2.2.2
5
- # - jruby
6
- sudo: false
7
- # uncomment this line if your project needs to run something other than `rake`:
8
- # script: bundle exec rspec spec
4
+ - 2.3
5
+ - jruby
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source 'https://rubygems.org'
1
+ source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in protobuf-activerecord.gemspec
4
4
  gemspec
data/Rakefile CHANGED
@@ -1,20 +1,24 @@
1
1
  require "bundler/gem_tasks"
2
+ require "protobuf/tasks"
2
3
  require "rspec/core/rake_task"
4
+ require "rubocop/rake_task"
5
+
6
+ desc "Run cops"
7
+ ::RuboCop::RakeTask.new(:rubocop)
3
8
 
4
9
  desc "Run specs"
5
- RSpec::Core::RakeTask.new(:spec)
10
+ ::RSpec::Core::RakeTask.new(:spec)
6
11
 
7
- desc "Run specs (default)"
8
- task :default => :spec
12
+ desc "Run cops and specs (default)"
13
+ task :default => [:rubocop, :spec]
9
14
 
10
15
  desc "Remove protobuf definitions that have been compiled"
11
16
  task :clean do
12
- FileUtils.rm(Dir.glob("spec/support/protobuf/**/*.proto"))
17
+ ::FileUtils.rm(Dir.glob("spec/support/protobuf/**/*.proto"))
13
18
  puts "Cleaned"
14
19
  end
15
20
 
16
21
  desc "Compile spec/support protobuf definitions"
17
22
  task :compile, [] => :clean do
18
- cmd = "protoc --ruby_out=spec/support/protobuf --proto_path=spec/support/definitions spec/support/definitions/*.proto"
19
- sh(cmd)
23
+ ::Rake::Task["protobuf:compile"].invoke("", "spec/support/definitions", "spec/support/protobuf")
20
24
  end
@@ -1,22 +1,23 @@
1
- require 'active_record'
2
- require 'active_support/concern'
3
- require 'heredity'
4
- require 'protobuf'
1
+ # rubocop:disable Naming/FileName
2
+ require "active_record"
3
+ require "active_support/concern"
4
+ require "heredity"
5
+ require "protobuf"
5
6
 
6
7
  # To support Rails 4 apps that use Mass-Assignment Security, attempt to
7
8
  # load the protected_attributes gem. If it's not present, move along...
8
9
  begin
9
- require 'protected_attributes'
10
- rescue LoadError
10
+ require "protected_attributes"
11
+ rescue LoadError # rubocop:disable Lint/HandleExceptions
11
12
  # These aren't the droids you're looking for... move along.
12
13
  end
13
14
 
14
- require 'protobuf/active_record/config'
15
- require 'protobuf/active_record/middleware/connection_management'
16
- require 'protobuf/active_record/middleware/connection_management_async'
17
- require 'protobuf/active_record/middleware/query_cache'
18
- require 'protobuf/active_record/model'
19
- require 'protobuf/active_record/version'
15
+ require "protobuf/active_record/config"
16
+ require "protobuf/active_record/middleware/connection_management"
17
+ require "protobuf/active_record/middleware/connection_management_async"
18
+ require "protobuf/active_record/middleware/query_cache"
19
+ require "protobuf/active_record/model"
20
+ require "protobuf/active_record/version"
20
21
 
21
22
  module Protobuf
22
23
  module ActiveRecord
@@ -29,4 +30,5 @@ module Protobuf
29
30
  end
30
31
  end
31
32
 
32
- require 'protobuf/active_record/railtie' if defined?(Rails)
33
+ require "protobuf/active_record/railtie" if defined?(Rails)
34
+ # rubocop:enable Naming/FileName
@@ -1,4 +1,4 @@
1
- require 'active_support/concern'
1
+ require "active_support/concern"
2
2
 
3
3
  module Protobuf
4
4
  module ActiveRecord
@@ -22,7 +22,7 @@ module Protobuf
22
22
  end
23
23
  end
24
24
 
25
- def fetch_field_alias_from_record(attribute, field_aliasd)
25
+ def fetch_field_alias_from_record(attribute, _field_aliasd)
26
26
  lambda do |record|
27
27
  value = record.__send__(field_alias)
28
28
 
@@ -1,6 +1,6 @@
1
- require 'set'
2
- require 'active_support/concern'
3
- require 'thread'
1
+ require "set"
2
+ require "active_support/concern"
3
+ require "thread"
4
4
 
5
5
  module Protobuf
6
6
  module ActiveRecord
@@ -63,7 +63,7 @@ module Protobuf
63
63
  return if _protobuf_mapped_columns?
64
64
 
65
65
  @_protobuf_columns = {}
66
- @_protobuf_column_types = ::Hash.new { |h,k| h[k] = ::Set.new }
66
+ @_protobuf_column_types = ::Hash.new { |h, k| h[k] = ::Set.new }
67
67
  @_protobuf_date_datetime_time_or_timestamp_column = ::Set.new
68
68
 
69
69
  columns.map do |column|
@@ -1,6 +1,6 @@
1
- require 'active_support/concern'
2
- require 'protobuf/active_record/mass_assignment_security/persistence'
3
- require 'protobuf/active_record/mass_assignment_security/transformation'
1
+ require "active_support/concern"
2
+ require "protobuf/active_record/mass_assignment_security/persistence"
3
+ require "protobuf/active_record/mass_assignment_security/transformation"
4
4
 
5
5
  module Protobuf
6
6
  module ActiveRecord
@@ -1,4 +1,4 @@
1
- require 'active_support/concern'
1
+ require "active_support/concern"
2
2
 
3
3
  module Protobuf
4
4
  module ActiveRecord
@@ -8,14 +8,14 @@ module Protobuf
8
8
 
9
9
  module ClassMethods
10
10
  # :nodoc:
11
- def create(attributes, options = {}, &block)
11
+ def create(attributes, options = {})
12
12
  attributes = attributes_from_proto(attributes) if attributes.is_a?(::Protobuf::Message)
13
13
 
14
14
  super(attributes, options)
15
15
  end
16
16
 
17
17
  # :nodoc:
18
- def create!(attributes, options = {}, &block)
18
+ def create!(attributes, options = {})
19
19
  attributes = attributes_from_proto(attributes) if attributes.is_a?(::Protobuf::Message)
20
20
 
21
21
  super(attributes, options)
@@ -1,4 +1,4 @@
1
- require 'active_support/concern'
1
+ require "active_support/concern"
2
2
 
3
3
  module Protobuf
4
4
  module ActiveRecord
@@ -12,10 +12,11 @@ module Protobuf
12
12
  START_MUTEX.synchronize do
13
13
  return if timed_task_started.true?
14
14
 
15
- timed_task = ::Concurrent::TimerTask.new(
16
- :execution_interval => ::Protobuf::ActiveRecord.config.connection_reaping_interval,
17
- :timeout_interval => ::Protobuf::ActiveRecord.config.connection_reaping_timeout_interval) do
18
-
15
+ args = {
16
+ :execution_interval => ::Protobuf::ActiveRecord.config.connection_reaping_interval,
17
+ :timeout_interval => ::Protobuf::ActiveRecord.config.connection_reaping_timeout_interval
18
+ }
19
+ timed_task = ::Concurrent::TimerTask.new(args) do
19
20
  ::ActiveRecord::Base.clear_active_connections!
20
21
  end
21
22
 
@@ -37,6 +38,8 @@ module Protobuf
37
38
  @app = app
38
39
  end
39
40
 
41
+ # rubocop:disable Lint/DuplicateMethods
42
+ # rubocop:disable Lint/NestedMethodDefinition
40
43
  def call(env)
41
44
  def call(env)
42
45
  ::ActiveRecord::Base.connection_pool.with_connection do
@@ -47,6 +50,8 @@ module Protobuf
47
50
  self.class.start_timed_task!
48
51
  call(env)
49
52
  end
53
+ # rubocop:enable Lint/NestedMethodDefinition
54
+ # rubocop:enable Lint/DuplicateMethods
50
55
 
51
56
  timed_task_started
52
57
  end
@@ -1,13 +1,13 @@
1
- require 'protobuf/active_record/attribute_methods'
2
- require 'protobuf/active_record/columns'
3
- require 'protobuf/active_record/errors'
4
- require 'protobuf/active_record/mass_assignment_security'
5
- require 'protobuf/active_record/nested_attributes'
6
- require 'protobuf/active_record/persistence'
7
- require 'protobuf/active_record/scope'
8
- require 'protobuf/active_record/serialization'
9
- require 'protobuf/active_record/transformation'
10
- require 'protobuf/active_record/validations'
1
+ require "protobuf/active_record/attribute_methods"
2
+ require "protobuf/active_record/columns"
3
+ require "protobuf/active_record/errors"
4
+ require "protobuf/active_record/mass_assignment_security"
5
+ require "protobuf/active_record/nested_attributes"
6
+ require "protobuf/active_record/persistence"
7
+ require "protobuf/active_record/scope"
8
+ require "protobuf/active_record/serialization"
9
+ require "protobuf/active_record/transformation"
10
+ require "protobuf/active_record/validations"
11
11
 
12
12
  module Protobuf
13
13
  module ActiveRecord
@@ -1,4 +1,4 @@
1
- require 'active_support/concern'
1
+ require "active_support/concern"
2
2
 
3
3
  module Protobuf
4
4
  module ActiveRecord
@@ -1,4 +1,4 @@
1
- require 'active_support/concern'
1
+ require "active_support/concern"
2
2
 
3
3
  module Protobuf
4
4
  module ActiveRecord
@@ -73,7 +73,7 @@ module Protobuf
73
73
  end
74
74
  end
75
75
 
76
- values = [ value ].flatten
76
+ values = [value].flatten
77
77
  values.map!(&:to_i) if proto.class.get_field(field, true).enum?
78
78
  values
79
79
  end
@@ -1,5 +1,5 @@
1
- require 'set'
2
- require 'active_support/concern'
1
+ require "set"
2
+ require "active_support/concern"
3
3
 
4
4
  module Protobuf
5
5
  module ActiveRecord
@@ -13,10 +13,6 @@ module Protobuf
13
13
  :_protobuf_field_options,
14
14
  :protobuf_message
15
15
  end
16
-
17
- private :_protobuf_field_symbol_transformers
18
- private :_protobuf_field_transformers
19
- private :_protobuf_message
20
16
  end
21
17
 
22
18
  module ClassMethods
@@ -169,8 +165,6 @@ module Protobuf
169
165
 
170
166
  if value
171
167
  value.to_time(:utc).to_i
172
- else
173
- nil
174
168
  end
175
169
  rescue NameError # Has happened when field is not on model or ignored from db
176
170
  return nil
@@ -185,11 +179,7 @@ module Protobuf
185
179
  def call(selph)
186
180
  value = selph.__send__(@field)
187
181
 
188
- if value
189
- value.to_i
190
- else
191
- nil
192
- end
182
+ value&.to_i
193
183
  rescue NameError # Has happened when field is not on model or ignored from db
194
184
  return nil
195
185
  end
@@ -227,14 +217,15 @@ module Protobuf
227
217
  end
228
218
 
229
219
  class NilMethodCaller
230
- def initialize; end
220
+ def initialize
221
+ end
231
222
 
232
- def call(selph)
223
+ def call(_selph)
233
224
  nil
234
225
  end
235
226
  end
236
227
 
237
- def _protobuf_nil_object(field)
228
+ def _protobuf_nil_object(_field)
238
229
  NilMethodCaller.new
239
230
  end
240
231
 
@@ -250,7 +241,7 @@ module Protobuf
250
241
  end
251
242
 
252
243
  def _protobuf_symbol_transformer_object(field)
253
- FieldSymbolTransformerCaller.new(self, _protobuf_symbol_transformer_object[field])
244
+ FieldSymbolTransformerCaller.new(self, _protobuf_field_symbol_transformers[field])
254
245
  end
255
246
  end
256
247
 
@@ -259,8 +250,8 @@ module Protobuf
259
250
  options = _normalize_options(options)
260
251
 
261
252
  fields = _filtered_fields(options)
262
- fields &= [ options[:only] ].flatten if options[:only].present?
263
- fields -= [ options[:except] ].flatten if options[:except].present?
253
+ fields &= [options[:only]].flatten if options[:only].present?
254
+ fields -= [options[:except]].flatten if options[:except].present?
264
255
 
265
256
  fields
266
257
  end
@@ -312,7 +303,7 @@ module Protobuf
312
303
 
313
304
  # Already flattened / compacted / uniqued ... unless we must include
314
305
  if options[:include].present?
315
- field_attributes.concat([ options[:include] ].flatten)
306
+ field_attributes.concat([options[:include]].flatten)
316
307
  field_attributes.compact!
317
308
  field_attributes.uniq!
318
309
  end
@@ -336,9 +327,9 @@ module Protobuf
336
327
  def _protobuf_field_objects(field)
337
328
  self.class._protobuf_field_objects[field] ||= begin
338
329
  case
339
- when _protobuf_field_symbol_transformers.has_key?(field) then
330
+ when _protobuf_field_symbol_transformers.key?(field) then
340
331
  self.class._protobuf_symbol_transformer_object(field)
341
- when _protobuf_field_transformers.has_key?(field) then
332
+ when _protobuf_field_transformers.key?(field) then
342
333
  self.class._protobuf_field_transformer_object(field)
343
334
  when respond_to?(field) then
344
335
  if _is_collection_association?(field)
@@ -369,7 +360,7 @@ module Protobuf
369
360
 
370
361
  # :nodoc:
371
362
  def to_proto(options = {})
372
- raise MessageNotDefined.new(self.class) if _protobuf_message.nil?
363
+ raise MessageNotDefined, self.class if _protobuf_message.nil?
373
364
 
374
365
  fields = self.fields_from_record(options)
375
366
  _protobuf_message.new(fields)
@@ -1,6 +1,6 @@
1
- require 'active_support/concern'
2
- require 'heredity/inheritable_class_instance_variables'
3
- require 'protobuf/active_record/transformer'
1
+ require "active_support/concern"
2
+ require "heredity/inheritable_class_instance_variables"
3
+ require "protobuf/active_record/transformer"
4
4
 
5
5
  module Protobuf
6
6
  module ActiveRecord
@@ -28,7 +28,7 @@ module Protobuf
28
28
  # :nodoc:
29
29
  def _filter_attribute_fields(proto)
30
30
  fields = proto.to_hash
31
- fields.select! do |key, value|
31
+ fields.select! do |key, _value|
32
32
  field = proto.class.get_field(key, true)
33
33
  proto.field?(key) && !field.repeated?
34
34
  end
@@ -38,7 +38,7 @@ module Protobuf
38
38
  attribute_fields = filtered_attributes.inject({}) do |hash, column_name|
39
39
  symbolized_column = column_name.to_sym
40
40
 
41
- if fields.has_key?(symbolized_column) || _protobuf_attribute_transformers.has_key?(symbolized_column)
41
+ if fields.key?(symbolized_column) || _protobuf_attribute_transformers.key?(symbolized_column)
42
42
  hash[symbolized_column] = fields[symbolized_column]
43
43
  end
44
44
 
@@ -47,9 +47,9 @@ module Protobuf
47
47
 
48
48
  _protobuf_nested_attributes.each do |attribute_name|
49
49
  nested_attribute_name = "#{attribute_name}_attributes".to_sym
50
- value = if proto.has_field?(nested_attribute_name)
50
+ value = if proto.field?(nested_attribute_name)
51
51
  proto.__send__(nested_attribute_name)
52
- elsif proto.has_field?(attribute_name)
52
+ elsif proto.field?(attribute_name)
53
53
  proto.__send__(attribute_name)
54
54
  end
55
55
 
@@ -121,7 +121,7 @@ module Protobuf
121
121
 
122
122
  if options[:nullify_on]
123
123
  field = protobuf_message.get_field(:nullify)
124
- unless field && field.is_a?(::Protobuf::Field::StringField) && field.repeated?
124
+ unless field&.is_a?(::Protobuf::Field::StringField) && field&.repeated?
125
125
  ::Protobuf::Logging.logger.warn "Message: #{protobuf_message} is not compatible with :nullify_on option"
126
126
  end
127
127
  end
@@ -130,7 +130,6 @@ module Protobuf
130
130
  _protobuf_attribute_transformers[attribute.to_sym] = transformer
131
131
  end
132
132
 
133
-
134
133
  # Creates a hash of attributes from a given protobuf message.
135
134
  #
136
135
  # It converts and transforms field values using the field converters and
@@ -140,7 +139,7 @@ module Protobuf
140
139
  attribute_fields = _filter_attribute_fields(proto)
141
140
 
142
141
  attributes = attribute_fields.inject({}) do |hash, (key, value)|
143
- if _protobuf_attribute_transformers.has_key?(key)
142
+ if _protobuf_attribute_transformers.key?(key)
144
143
  transformer = _protobuf_attribute_transformers[key]
145
144
  attribute = transformer.call(proto)
146
145
  hash[key] = attribute unless attribute.nil?
@@ -8,9 +8,7 @@ module Protobuf
8
8
  @options = options
9
9
  end
10
10
 
11
- def call(proto)
12
- callable.call(proto)
13
- end
11
+ delegate :call, :to => :callable
14
12
 
15
13
  def nullify?(proto)
16
14
  return false unless options[:nullify_on]
@@ -1,4 +1,4 @@
1
- require 'active_support/concern'
1
+ require "active_support/concern"
2
2
 
3
3
  module Protobuf
4
4
  module ActiveRecord
@@ -40,4 +40,4 @@ module Protobuf
40
40
  end
41
41
  end
42
42
  end
43
- end
43
+ end
@@ -1,5 +1,5 @@
1
1
  module Protobuf
2
2
  module ActiveRecord
3
- VERSION = "3.6.0"
3
+ VERSION = "3.6.1"
4
4
  end
5
5
  end
@@ -1,7 +1,8 @@
1
1
  # -*- encoding: utf-8 -*-
2
- lib = File.expand_path('../lib', __FILE__)
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'protobuf/active_record/version'
5
+ require "protobuf/active_record/version"
5
6
 
6
7
  Gem::Specification.new do |spec|
7
8
  spec.name = "protobuf-activerecord"
@@ -9,12 +10,12 @@ Gem::Specification.new do |spec|
9
10
  spec.authors = ["Adam Hutchison"]
10
11
  spec.email = ["liveh2o@gmail.com"]
11
12
  spec.homepage = "http://github.com/liveh2o/protobuf-activerecord"
12
- spec.summary = %q{Google Protocol Buffers integration for Active Record}
13
- spec.description = %q{Provides the ability to create Active Record objects from Protocol Buffer messages and vice versa.}
14
- spec.license = 'MIT'
13
+ spec.summary = "Google Protocol Buffers integration for Active Record"
14
+ spec.description = "Provides the ability to create Active Record objects from Protocol Buffer messages and vice versa."
15
+ spec.license = "MIT"
15
16
 
16
- spec.files = `git ls-files`.split($/)
17
- spec.executables = spec.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
18
+ spec.executables = spec.files.grep(%r{^bin/}).map { |f| File.basename(f) }
18
19
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
20
  spec.require_paths = ["lib"]
20
21
 
@@ -31,6 +32,7 @@ Gem::Specification.new do |spec|
31
32
  # Development dependencies
32
33
  #
33
34
  spec.add_development_dependency "benchmark-ips"
35
+ spec.add_development_dependency "mad_rubocop"
34
36
  spec.add_development_dependency "rake"
35
37
  spec.add_development_dependency "rspec", ">= 3.3.0"
36
38
  spec.add_development_dependency "rspec-pride", ">= 3.1.0"
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  describe Protobuf::ActiveRecord::Columns do
4
4
  describe "._protobuf_map_columns" do
@@ -31,10 +31,10 @@ describe Protobuf::ActiveRecord::Columns do
31
31
  end
32
32
 
33
33
  context "column type predicates" do
34
- before { allow(User).to receive(:_protobuf_column_types).and_return(Hash.new) }
34
+ before { allow(User).to receive(:_protobuf_column_types).and_return({}) }
35
35
 
36
36
  describe "._protobuf_date_column?" do
37
- before { User._protobuf_column_types[:date] = [ :foo_date ] }
37
+ before { User._protobuf_column_types[:date] = [:foo_date] }
38
38
 
39
39
  context "when the column type is :date" do
40
40
  it "is true" do
@@ -50,7 +50,7 @@ describe Protobuf::ActiveRecord::Columns do
50
50
  end
51
51
 
52
52
  describe "._protobuf_datetime_column?" do
53
- before { User._protobuf_column_types[:datetime] = [ :foo_datetime ] }
53
+ before { User._protobuf_column_types[:datetime] = [:foo_datetime] }
54
54
 
55
55
  context "when the column type is :datetime" do
56
56
  it "is true" do
@@ -66,7 +66,7 @@ describe Protobuf::ActiveRecord::Columns do
66
66
  end
67
67
 
68
68
  describe "._protobuf_time_column?" do
69
- before { User._protobuf_column_types[:time] = [ :foo_time ] }
69
+ before { User._protobuf_column_types[:time] = [:foo_time] }
70
70
 
71
71
  context "when the column type is :time" do
72
72
  it "is true" do
@@ -82,7 +82,7 @@ describe Protobuf::ActiveRecord::Columns do
82
82
  end
83
83
 
84
84
  describe "._protobuf_timestamp_column?" do
85
- before { User._protobuf_column_types[:timestamp] = [ :foo_timestamp ] }
85
+ before { User._protobuf_column_types[:timestamp] = [:foo_timestamp] }
86
86
 
87
87
  context "when the column type is :timestamp" do
88
88
  it "is true" do
@@ -2,7 +2,7 @@ require "spec_helper"
2
2
 
3
3
  describe Protobuf::ActiveRecord::NestedAttributes do
4
4
  let(:user_message) {
5
- UserMessage.new(:name => "foo bar", :email => "foo@test.co", :photos => [{ url: "https://test.co/test.png" }])
5
+ UserMessage.new(:name => "foo bar", :email => "foo@test.co", :photos => [{ :url => "https://test.co/test.png" }])
6
6
  }
7
7
 
8
8
  describe "._filter_attribute_fields", :aggregate_failures => true do
@@ -12,7 +12,6 @@ describe Protobuf::ActiveRecord::NestedAttributes do
12
12
  end
13
13
 
14
14
  context "when" do
15
-
16
15
  end
17
16
  end
18
17
 
@@ -1,9 +1,9 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  describe Protobuf::ActiveRecord::Persistence do
4
4
  let(:user) { User.new(user_attributes) }
5
- let(:user_attributes) { { :first_name => 'foo', :last_name => 'bar', :email => 'foo@test.co' } }
6
- let(:proto_hash) { { :name => 'foo bar', :email => 'foo@test.co' } }
5
+ let(:user_attributes) { { :first_name => "foo", :last_name => "bar", :email => "foo@test.co" } }
6
+ let(:proto_hash) { { :name => "foo bar", :email => "foo@test.co" } }
7
7
  let(:proto) { UserMessage.new(proto_hash) }
8
8
 
9
9
  describe ".create" do
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  describe Protobuf::ActiveRecord::Scope do
4
4
  before do
@@ -12,16 +12,15 @@ describe Protobuf::ActiveRecord::Scope do
12
12
  User.instance_variable_set("@_upsert_keys", [])
13
13
  end
14
14
 
15
-
16
15
  describe ".search_scope" do
17
16
  let(:request) { UserSearchMessage.new(:guid => ["foo"], :email => ["foo@test.co"]) }
18
17
 
19
18
  before {
20
- allow(User).to receive(:searchable_field_parsers).and_return({ :email => proc { |val| val } })
19
+ allow(User).to receive(:searchable_field_parsers).and_return(:email => proc { |val| val })
21
20
  }
22
21
 
23
22
  it "builds scopes for searchable fields" do
24
- allow(User).to receive(:searchable_fields).and_return({ :email => :by_email })
23
+ allow(User).to receive(:searchable_fields).and_return(:email => :by_email)
25
24
  expect(User.search_scope(request)).to eq User.by_email("foo@test.co")
26
25
  end
27
26
 
@@ -33,7 +32,7 @@ describe Protobuf::ActiveRecord::Scope do
33
32
  let(:request) { UserSearchMessage.new(:email => ["foo@test.co"]) }
34
33
 
35
34
  it "doesn't build a scope from that field" do
36
- allow(User).to receive(:searchable_fields).and_return({ :email => :by_email })
35
+ allow(User).to receive(:searchable_fields).and_return(:email => :by_email)
37
36
  expect(User.search_scope(request)).to eq User.by_email("foo@test.co")
38
37
  end
39
38
  end
@@ -42,7 +41,7 @@ describe Protobuf::ActiveRecord::Scope do
42
41
  let(:request) { UserSearchMessage.new(:email => ["foo@test.co"]) }
43
42
 
44
43
  it "raises an exception" do
45
- allow(User).to receive(:searchable_fields).and_return({ :email => :by_hullabaloo })
44
+ allow(User).to receive(:searchable_fields).and_return(:email => :by_hullabaloo)
46
45
  expect { User.search_scope(request) }.to raise_exception(/undefined method .*by_hullabaloo/i)
47
46
  end
48
47
  end
@@ -92,7 +91,7 @@ describe Protobuf::ActiveRecord::Scope do
92
91
  let(:proto) { UserSearchMessage.new(:guid => ["foo"]) }
93
92
 
94
93
  context "and the parser does not respond to :to_sym" do
95
- let(:parser) { double('parser') }
94
+ let(:parser) { double("parser") }
96
95
 
97
96
  it "passes the value to the parser" do
98
97
  expect(parser).to receive(:call).with(["foo"])
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  # Used to test calling #to_proto when no protobuf message is configured.
4
4
  class UnconfiguredUser
@@ -24,10 +24,10 @@ describe Protobuf::ActiveRecord::Serialization do
24
24
  end
25
25
 
26
26
  context "when the given transformer is callable" do
27
- let(:callable) { lambda { |proto| nil } }
27
+ let(:callable) { lambda { |_proto| nil } }
28
28
 
29
29
  before {
30
- allow(User).to receive(:_protobuf_field_transformers).and_return(Hash.new)
30
+ allow(User).to receive(:_protobuf_field_transformers).and_return({})
31
31
  User.field_from_record :account_id, callable
32
32
  }
33
33
 
@@ -61,7 +61,7 @@ describe Protobuf::ActiveRecord::Serialization do
61
61
  end
62
62
 
63
63
  context "when protobuf_message is defined" do
64
- let(:attributes) { Hash.new }
64
+ let(:attributes) { {} }
65
65
  let(:user) { User.new(attributes) }
66
66
 
67
67
  before { User.protobuf_message(protobuf_message) }
@@ -70,40 +70,48 @@ describe Protobuf::ActiveRecord::Serialization do
70
70
  context "when options has :only" do
71
71
  it "only returns the given field(s)" do
72
72
  fields = user._filter_field_attributes(:only => :name)
73
- expect(fields).to eq [ :name ]
73
+ expect(fields).to eq [:name]
74
74
  end
75
75
  end
76
76
 
77
77
  context "when options has :except" do
78
78
  it "returns all except the given field(s)" do
79
79
  fields = user._filter_field_attributes(:except => :name)
80
- expect(fields).to match_array([ :guid, :email, :email_domain, :password, :nullify, :photos ])
80
+ expect(fields).to match_array(
81
+ [:guid, :email, :email_domain, :password, :nullify, :photos, :created_at, :updated_at]
82
+ )
81
83
  end
82
84
  end
83
85
  end
84
86
 
85
87
  describe "#_filtered_fields" do
86
88
  it "returns protobuf fields" do
87
- expect(user._filtered_fields).to match_array([ :guid, :name, :email, :email_domain, :password, :nullify, :photos ])
89
+ expect(user._filtered_fields).to match_array(
90
+ [:guid, :name, :email, :email_domain, :password, :nullify, :photos, :created_at, :updated_at]
91
+ )
88
92
  end
89
93
 
90
94
  context "given :deprecated => false" do
91
95
  it "filters all deprecated fields" do
92
96
  fields = user._filtered_fields(:deprecated => false)
93
- expect(fields).to match_array([ :guid, :name, :email, :password, :nullify, :photos ])
97
+ expect(fields).to match_array(
98
+ [:guid, :name, :email, :password, :nullify, :photos, :created_at, :updated_at]
99
+ )
94
100
  end
95
101
 
96
- context 'and :include => :email_domain' do
97
- it 'includes deprecated fields that have been explicitly specified' do
102
+ context "and :include => :email_domain" do
103
+ it "includes deprecated fields that have been explicitly specified" do
98
104
  fields = user._filtered_fields(:deprecated => false, :include => :email_domain)
99
- expect(fields).to match_array([ :guid, :name, :email, :email_domain, :password, :nullify, :photos ])
105
+ expect(fields).to match_array(
106
+ [:guid, :name, :email, :email_domain, :password, :nullify, :photos, :created_at, :updated_at]
107
+ )
100
108
  end
101
109
  end
102
110
  end
103
111
  end
104
112
 
105
113
  describe "#_normalize_options" do
106
- let(:options) { { :only => [ :name ] } }
114
+ let(:options) { { :only => [:name] } }
107
115
 
108
116
  context "given empty options" do
109
117
  before { User.protobuf_message(protobuf_message, options) }
@@ -132,7 +140,7 @@ describe Protobuf::ActiveRecord::Serialization do
132
140
  end
133
141
 
134
142
  context "given options with :except" do
135
- let(:options) { { :except => [ :name ] } }
143
+ let(:options) { { :except => [:name] } }
136
144
 
137
145
  before { User.protobuf_message(protobuf_message, {}) }
138
146
 
@@ -155,7 +163,7 @@ describe Protobuf::ActiveRecord::Serialization do
155
163
 
156
164
  context "when a transformer is defined for the field" do
157
165
  it "gets the field from the transformer" do
158
- expect(user.fields_from_record[:email_domain]).to eq('test.co')
166
+ expect(user.fields_from_record[:email_domain]).to eq("test.co")
159
167
  end
160
168
  end
161
169
 
@@ -1,9 +1,9 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  describe Protobuf::ActiveRecord::Transformation do
4
4
  let(:user) { User.new(user_attributes) }
5
- let(:user_attributes) { { :first_name => 'foo', :last_name => 'bar', :email => 'foo@test.co' } }
6
- let(:proto_hash) { { :name => 'foo bar', :email => 'foo@test.co' } }
5
+ let(:user_attributes) { { :first_name => "foo", :last_name => "bar", :email => "foo@test.co" } }
6
+ let(:proto_hash) { { :name => "foo bar", :email => "foo@test.co" } }
7
7
  let(:proto) { UserMessage.new(proto_hash) }
8
8
 
9
9
  describe "._filter_attribute_fields" do
@@ -14,18 +14,18 @@ describe Protobuf::ActiveRecord::Transformation do
14
14
 
15
15
  it "filters repeated fields" do
16
16
  attribute_fields = User._filter_attribute_fields(proto)
17
- expect(attribute_fields.has_key?(:tags)).to be false
17
+ expect(attribute_fields.key?(:tags)).to be false
18
18
  end
19
19
 
20
20
  it "includes attributes that aren't fields, but have attribute transformers" do
21
- allow(User).to receive(:_protobuf_attribute_transformers).and_return({ :account_id => :fetch_account_id })
21
+ allow(User).to receive(:_protobuf_attribute_transformers).and_return(:account_id => :fetch_account_id)
22
22
  attribute_fields = User._filter_attribute_fields(proto)
23
- expect(attribute_fields.has_key?(:account_id)).to be true
23
+ expect(attribute_fields.key?(:account_id)).to be true
24
24
  end
25
25
 
26
26
  it "includes fields that aren't attributes, but have attribute transformers" do
27
27
  attribute_fields = User._filter_attribute_fields(proto)
28
- expect(attribute_fields.has_key?(:password)).to be true
28
+ expect(attribute_fields.key?(:password)).to be true
29
29
  end
30
30
  end
31
31
 
@@ -108,7 +108,7 @@ describe Protobuf::ActiveRecord::Transformation do
108
108
  end
109
109
 
110
110
  describe ".attributes_from_proto" do
111
- let(:callable) { lambda { |proto| 1 } }
111
+ let(:callable) { lambda { |_proto| 1 } }
112
112
  let(:transformer) { ::Protobuf::ActiveRecord::Transformer.new(callable) }
113
113
 
114
114
  context "when a transformer is defined for the attribute" do
@@ -119,12 +119,12 @@ describe Protobuf::ActiveRecord::Transformation do
119
119
  end
120
120
 
121
121
  context "when a transformer is a callable that returns nil" do
122
- let(:callable) { lambda { |proto| nil } }
122
+ let(:callable) { lambda { |_proto| nil } }
123
123
 
124
124
  before do
125
125
  transformers = User._protobuf_attribute_transformers
126
126
  allow(User).to receive(:_protobuf_attribute_transformers).and_return(
127
- {:account_id => transformer}.merge(transformers)
127
+ { :account_id => transformer }.merge(transformers)
128
128
  )
129
129
  end
130
130
 
@@ -134,15 +134,15 @@ describe Protobuf::ActiveRecord::Transformation do
134
134
  end
135
135
  end
136
136
 
137
- context 'when the transformer has a nullify_on option' do
138
- let(:callable) { lambda { |proto| nil } }
137
+ context "when the transformer has a nullify_on option" do
138
+ let(:callable) { lambda { |_proto| nil } }
139
139
  let(:transformer) { ::Protobuf::ActiveRecord::Transformer.new(callable, :nullify_on => :account_id) }
140
- let(:proto_hash) { { :name => 'foo bar', :email => 'foo@test.co', :nullify => [:account_id] } }
140
+ let(:proto_hash) { { :name => "foo bar", :email => "foo@test.co", :nullify => [:account_id] } }
141
141
 
142
142
  before do
143
143
  transformers = User._protobuf_attribute_transformers
144
144
  allow(User).to receive(:_protobuf_attribute_transformers).and_return(
145
- {:account_id => transformer}.merge(transformers)
145
+ { :account_id => transformer }.merge(transformers)
146
146
  )
147
147
  end
148
148
 
@@ -156,7 +156,7 @@ describe Protobuf::ActiveRecord::Transformation do
156
156
  before do
157
157
  transformers = User._protobuf_attribute_transformers
158
158
  allow(User).to receive(:_protobuf_attribute_transformers).and_return(
159
- {:account_id => transformer}.merge(transformers)
159
+ { :account_id => transformer }.merge(transformers)
160
160
  )
161
161
  end
162
162
 
@@ -168,7 +168,7 @@ describe Protobuf::ActiveRecord::Transformation do
168
168
 
169
169
  context "when a transformer is not defined for the attribute" do
170
170
  before {
171
- allow(User).to receive(:_protobuf_convert_fields_to_attributes) do |key, value|
171
+ allow(User).to receive(:_protobuf_convert_fields_to_attributes) do |_key, value|
172
172
  value
173
173
  end
174
174
  }
@@ -182,7 +182,7 @@ describe Protobuf::ActiveRecord::Transformation do
182
182
 
183
183
  describe ".attribute_from_proto" do
184
184
  context "when the given transformer is a symbol" do
185
- let(:callable) { lambda { |value| User.__send__(:extract_first_name) } }
185
+ let(:callable) { lambda { |_value| User.__send__(:extract_first_name) } }
186
186
 
187
187
  before { User.attribute_from_proto :first_name, :extract_first_name }
188
188
 
@@ -199,9 +199,9 @@ describe Protobuf::ActiveRecord::Transformation do
199
199
  end
200
200
 
201
201
  context "when the given transformer is callable" do
202
- let(:callable) { lambda { |proto| nil } }
202
+ let(:callable) { lambda { |_proto| nil } }
203
203
 
204
- before { allow(User).to receive(:_protobuf_attribute_transformers).and_return(Hash.new) }
204
+ before { allow(User).to receive(:_protobuf_attribute_transformers).and_return({}) }
205
205
 
206
206
  it "adds the given converter to the list of protobuf field transformers" do
207
207
  User.attribute_from_proto :account_id, callable
@@ -1,39 +1,39 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  describe ::Protobuf::ActiveRecord::Transformer do
4
4
  let(:callable) { lambda { |proto| proto.name } }
5
- let(:proto) { ::UserMessage.new(:name => 'test', :nullify => ['name']) }
5
+ let(:proto) { ::UserMessage.new(:name => "test", :nullify => ["name"]) }
6
6
  let(:options) { {} }
7
7
 
8
8
  subject { described_class.new(callable, options) }
9
9
 
10
- describe '#call' do
11
- it 'calls the callable' do
10
+ describe "#call" do
11
+ it "calls the callable" do
12
12
  result = subject.call(proto)
13
- expect(result).to eq('test')
13
+ expect(result).to eq("test")
14
14
  end
15
15
  end
16
16
 
17
- describe '#nullify?' do
18
- context 'no nullify_on set' do
19
- it 'returns false' do
17
+ describe "#nullify?" do
18
+ context "no nullify_on set" do
19
+ it "returns false" do
20
20
  expect(subject.nullify?(proto)).to eq(false)
21
21
  end
22
22
  end
23
23
 
24
- context 'nullify_on name' do
24
+ context "nullify_on name" do
25
25
  let(:options) { { :nullify_on => :name } }
26
26
 
27
- context 'invalid message' do
27
+ context "invalid message" do
28
28
  let(:proto) { ::UserSearchMessage.new }
29
29
 
30
- it 'returns false' do
30
+ it "returns false" do
31
31
  expect(subject.nullify?(proto)).to eq(false)
32
32
  end
33
33
  end
34
34
 
35
- context 'valid message' do
36
- it 'returns true' do
35
+ context "valid message" do
36
+ it "returns true" do
37
37
  expect(subject.nullify?(proto)).to eq(true)
38
38
  end
39
39
  end
@@ -1 +1 @@
1
- require 'support/db/setup'
1
+ require "support/db/setup"
@@ -1,4 +1,4 @@
1
- require 'active_record'
1
+ require "active_record"
2
2
 
3
3
  ActiveRecord::Base.establish_connection(
4
4
  :adapter => "sqlite3",
@@ -14,7 +14,7 @@ ActiveRecord::Schema.define(:version => 1) do
14
14
  t.string :url
15
15
  t.integer :user_id
16
16
 
17
- t.timestamps null: false
17
+ t.timestamps :null => false
18
18
  end
19
19
 
20
20
  create_table :users do |t|
@@ -24,6 +24,6 @@ ActiveRecord::Schema.define(:version => 1) do
24
24
  t.string :email
25
25
  t.integer :account_id
26
26
 
27
- t.timestamps null: false
27
+ t.timestamps :null => false
28
28
  end
29
29
  end
@@ -1,16 +1,20 @@
1
+ syntax = "proto3";
2
+
1
3
  message PhotoMessage {
2
- optional string url = 1;
3
- optional int64 user_guid = 2;
4
+ string url = 1;
5
+ int64 user_guid = 2;
4
6
  }
5
7
 
6
8
  message UserMessage {
7
- optional string guid = 1;
8
- optional string name = 2;
9
- optional string email = 3;
10
- optional string email_domain = 4 [deprecated = true];
11
- optional string password = 5;
9
+ string guid = 1;
10
+ string name = 2;
11
+ string email = 3;
12
+ string email_domain = 4 [deprecated = true];
13
+ string password = 5;
12
14
  repeated string nullify = 6;
13
15
  repeated PhotoMessage photos = 7;
16
+ int64 created_at = 8;
17
+ int64 updated_at = 9;
14
18
  }
15
19
 
16
20
  message UserSearchMessage {
@@ -1,2 +1,2 @@
1
- require 'support/models/photo'
2
- require 'support/models/user'
1
+ require "support/models/photo"
2
+ require "support/models/user"
@@ -16,7 +16,8 @@ class User < ActiveRecord::Base
16
16
  attribute_from_proto :last_name, :extract_last_name
17
17
  attribute_from_proto :password, lambda { |proto| proto.password! }
18
18
 
19
- field_from_record :email_domain, lambda { |record| record.email.split('@').last }
19
+ field_from_record :email_domain, lambda { |record| record.email.split("@").last }
20
+ field_from_record :password, :password_transformer
20
21
 
21
22
  def self.extract_first_name(proto)
22
23
  if proto.field?(:name)
@@ -37,6 +38,11 @@ class User < ActiveRecord::Base
37
38
  last_name
38
39
  end
39
40
 
41
+ def self.password_transformer(user)
42
+ # Simple way to test field transformers that call methods
43
+ user.password
44
+ end
45
+
40
46
  def token
41
47
  "key"
42
48
  end
@@ -30,6 +30,8 @@ class UserMessage
30
30
  optional :string, :password, 5
31
31
  repeated :string, :nullify, 6
32
32
  repeated ::PhotoMessage, :photos, 7
33
+ optional :int64, :created_at, 8
34
+ optional :int64, :updated_at, 9
33
35
  end
34
36
 
35
37
  class UserSearchMessage
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.6.0
4
+ version: 3.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Hutchison
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-15 00:00:00.000000000 Z
11
+ date: 2019-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: mad_rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: rake
99
113
  requirement: !ruby/object:Gem::Requirement
@@ -202,6 +216,7 @@ extra_rdoc_files: []
202
216
  files:
203
217
  - ".gitignore"
204
218
  - ".rspec"
219
+ - ".rubocop.yml"
205
220
  - ".travis.yml"
206
221
  - Gemfile
207
222
  - LICENSE.txt
@@ -263,7 +278,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
263
278
  - !ruby/object:Gem::Version
264
279
  version: '0'
265
280
  requirements: []
266
- rubygems_version: 3.0.1
281
+ rubygems_version: 3.0.2
267
282
  signing_key:
268
283
  specification_version: 4
269
284
  summary: Google Protocol Buffers integration for Active Record