diff --git a/.rubocop.yml b/.rubocop.yml index 7bd329e..0749450 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,28 +1,7 @@ -# Styles are inherited from Mad Rubocop +require: + # Standard's config uses custom cops, so it must be loaded + - standard inherit_gem: - mad_rubocop: .rubocop.yml - -# Styles that are modified from the defaults -AllCops: - TargetRubyVersion: 2.5 - Exclude: - - "**/*.pb.rb" - -Style/BlockDelimiters: - Description: >- - Avoid using {...} for multi-line blocks (multiline chaining is always ugly). - Prefer {...} over do...end for single-line blocks. - Exclude: - - "spec/**/*" - -Style/HashSyntax: - Description: >- - Prefer Ruby 1.8 hash syntax { :a => 1, :b => 2 } over 1.9 syntax { a: 1, b: 2 }. - EnforcedStyle: hash_rockets - Exclude: - - "Gemfile" - -Layout/SpaceAroundOperators: - Exclude: - - "*.gemspec" + standard: config/base.yml + standard-performance: config.base.yml diff --git a/.standard.yml b/.standard.yml new file mode 100644 index 0000000..7dd7c69 --- /dev/null +++ b/.standard.yml @@ -0,0 +1,2 @@ +parallel: true # default: false +ruby_version: 3.0 # default: RUBY_VERSION diff --git a/Rakefile b/Rakefile index 12dc4b1..28b53f6 100644 --- a/Rakefile +++ b/Rakefile @@ -1,16 +1,13 @@ require "bundler/gem_tasks" require "protobuf/tasks" require "rspec/core/rake_task" -require "rubocop/rake_task" - -desc "Run cops" -::RuboCop::RakeTask.new(:rubocop) +require "standard/rake" desc "Run specs" ::RSpec::Core::RakeTask.new(:spec) desc "Run cops and specs (default)" -task :default => [:rubocop, :spec] +task default: [:standard, :spec] desc "Remove protobuf definitions that have been compiled" task :clean do diff --git a/lib/protobuf/active_record/attribute_methods.rb b/lib/protobuf/active_record/attribute_methods.rb index 8b880c4..1183989 100644 --- a/lib/protobuf/active_record/attribute_methods.rb +++ b/lib/protobuf/active_record/attribute_methods.rb @@ -18,7 +18,7 @@ def fetch_attribute_alias_from_proto(attribute, field_alias) value = proto.__send__(:"#{field_alias}!") value ||= proto.__send__(:"#{attribute}!") if proto.respond_to?(attribute) - self._protobuf_convert_fields_to_attributes(attribute, value) + _protobuf_convert_fields_to_attributes(attribute, value) end end @@ -26,7 +26,7 @@ def fetch_field_alias_from_record(attribute, _field_aliasd) lambda do |record| value = record.__send__(field_alias) - self._protobuf_convert_attributes_to_fields(attribute, value) + _protobuf_convert_attributes_to_fields(attribute, value) end end end diff --git a/lib/protobuf/active_record/columns.rb b/lib/protobuf/active_record/columns.rb index 9afc47e..690fbf2 100644 --- a/lib/protobuf/active_record/columns.rb +++ b/lib/protobuf/active_record/columns.rb @@ -1,6 +1,5 @@ require "set" require "active_support/concern" -require "thread" module Protobuf module ActiveRecord @@ -14,9 +13,9 @@ module Columns include ::Heredity inheritable_attributes :_protobuf_columns, - :_protobuf_column_types, - :_protobuf_date_datetime_time_or_timestamp_column, - :_protobuf_mapped_columns + :_protobuf_column_types, + :_protobuf_date_datetime_time_or_timestamp_column, + :_protobuf_mapped_columns end module ClassMethods diff --git a/lib/protobuf/active_record/middleware/connection_management_async.rb b/lib/protobuf/active_record/middleware/connection_management_async.rb index a5fec93..81a2ae5 100644 --- a/lib/protobuf/active_record/middleware/connection_management_async.rb +++ b/lib/protobuf/active_record/middleware/connection_management_async.rb @@ -1,5 +1,4 @@ require "concurrent" -require "thread" module Protobuf module ActiveRecord @@ -13,8 +12,8 @@ def self.start_timed_task! return if timed_task_started.true? args = { - :execution_interval => ::Protobuf::ActiveRecord.config.connection_reaping_interval, - :timeout_interval => ::Protobuf::ActiveRecord.config.connection_reaping_timeout_interval + execution_interval: ::Protobuf::ActiveRecord.config.connection_reaping_interval, + timeout_interval: ::Protobuf::ActiveRecord.config.connection_reaping_timeout_interval } timed_task = ::Concurrent::TimerTask.new(args) do ::ActiveRecord::Base.clear_active_connections! diff --git a/lib/protobuf/active_record/middleware/query_cache.rb b/lib/protobuf/active_record/middleware/query_cache.rb index 3fcbd41..ed7844a 100644 --- a/lib/protobuf/active_record/middleware/query_cache.rb +++ b/lib/protobuf/active_record/middleware/query_cache.rb @@ -1,5 +1,3 @@ -require "thread" - module Protobuf module ActiveRecord module Middleware @@ -20,7 +18,7 @@ def call(env) restore_query_cache_settings(enabled) end - private + private def restore_query_cache_settings(enabled) ::Thread.current[CURRENT_CONNECTION].clear_query_cache diff --git a/lib/protobuf/active_record/scope.rb b/lib/protobuf/active_record/scope.rb index 18778ca..d762f8f 100644 --- a/lib/protobuf/active_record/scope.rb +++ b/lib/protobuf/active_record/scope.rb @@ -42,11 +42,11 @@ module ClassMethods # def field_scope(field, options = {}) scope_name = if options.include?(:scope) - options[:scope] - else - # When no scope is defined, assume the scope is the field, prefixed with `by_` - :"by_#{field}" - end + options[:scope] + else + # When no scope is defined, assume the scope is the field, prefixed with `by_` + :"by_#{field}" + end searchable_fields[field] = scope_name searchable_field_parsers[field] = options[:parser] if options[:parser] @@ -56,7 +56,7 @@ def field_scope(field, options = {}) # use `all` instead of `scoped`. # :noapi: def model_scope - ::ActiveRecord::VERSION::MAJOR >= 4 ? all : scoped + (::ActiveRecord::VERSION::MAJOR >= 4) ? all : scoped end # :noapi: @@ -66,10 +66,10 @@ def parse_search_values(proto, field) if searchable_field_parsers[field] parser = searchable_field_parsers[field] - if parser.respond_to?(:to_sym) - value = self.__send__(parser.to_sym, value) + value = if parser.respond_to?(:to_sym) + __send__(parser.to_sym, value) else - value = parser.call(value) + parser.call(value) end end @@ -99,7 +99,7 @@ def search_scope(proto) search_relation = search_relation.__send__(scope_name, *search_values) end - return search_relation + search_relation end # :noapi: diff --git a/lib/protobuf/active_record/serialization.rb b/lib/protobuf/active_record/serialization.rb index b330c11..3dc6c36 100644 --- a/lib/protobuf/active_record/serialization.rb +++ b/lib/protobuf/active_record/serialization.rb @@ -9,9 +9,9 @@ module Serialization included do class << self attr_writer :_protobuf_field_symbol_transformers, - :_protobuf_field_transformers, - :_protobuf_field_options, - :protobuf_message + :_protobuf_field_transformers, + :_protobuf_field_options, + :protobuf_message end end @@ -33,24 +33,20 @@ def _protobuf_field_transformers end def _protobuf_message_deprecated_fields - @_protobuf_message_deprecated_fields ||= begin - self.protobuf_message.all_fields.map do |field| - next if field.nil? - next unless field.deprecated? + @_protobuf_message_deprecated_fields ||= protobuf_message.all_fields.map do |field| + next if field.nil? + next unless field.deprecated? - field.name.to_sym - end + field.name.to_sym end end def _protobuf_message_non_deprecated_fields - @_protobuf_message_non_deprecated_fields ||= begin - self.protobuf_message.all_fields.map do |field| - next if field.nil? - next if field.deprecated? + @_protobuf_message_non_deprecated_fields ||= protobuf_message.all_fields.map do |field| + next if field.nil? + next if field.deprecated? - field.name.to_sym - end + field.name.to_sym end end @@ -147,7 +143,7 @@ def initialize(method_name) def call(selph) selph.__send__(@method_name).to_a rescue NameError # Has happened when field is not on model or ignored from db - return nil + nil end end @@ -167,7 +163,7 @@ def call(selph) value.to_time(:utc).to_i end rescue NameError # Has happened when field is not on model or ignored from db - return nil + nil end end @@ -181,7 +177,7 @@ def call(selph) value&.to_i rescue NameError # Has happened when field is not on model or ignored from db - return nil + nil end end @@ -193,7 +189,7 @@ def initialize(field) def call(selph) selph.__send__(@field) rescue NameError # Has happened when field is not on model or ignored from db - return nil + nil end end @@ -325,21 +321,21 @@ def fields_from_record(options = {}) # :nodoc: def _protobuf_field_objects(field) - self.class._protobuf_field_objects[field] ||= begin - case - when _protobuf_field_symbol_transformers.key?(field) then - self.class._protobuf_symbol_transformer_object(field) - when _protobuf_field_transformers.key?(field) then - self.class._protobuf_field_transformer_object(field) - when respond_to?(field) then - if _is_collection_association?(field) - self.class._protobuf_collection_association_object(field) - else - self.class._protobuf_convert_to_fields_object(field) - end + self.class._protobuf_field_objects[field] ||= if _protobuf_field_symbol_transformers.key?(field) + + self.class._protobuf_symbol_transformer_object(field) + elsif _protobuf_field_transformers.key?(field) + + self.class._protobuf_field_transformer_object(field) + elsif respond_to?(field) + + if _is_collection_association?(field) + self.class._protobuf_collection_association_object(field) else - self.class._protobuf_nil_object(field) + self.class._protobuf_convert_to_fields_object(field) end + else + self.class._protobuf_nil_object(field) end end @@ -362,7 +358,7 @@ def _protobuf_message def to_proto(options = {}) raise MessageNotDefined, self.class if _protobuf_message.nil? - fields = self.fields_from_record(options) + fields = fields_from_record(options) _protobuf_message.new(fields) end end diff --git a/lib/protobuf/active_record/transformation.rb b/lib/protobuf/active_record/transformation.rb index 5f91984..521cf81 100644 --- a/lib/protobuf/active_record/transformation.rb +++ b/lib/protobuf/active_record/transformation.rb @@ -35,23 +35,21 @@ def _filter_attribute_fields(proto) filtered_attributes = _filtered_attributes + _protobuf_attribute_transformers.keys - attribute_fields = filtered_attributes.inject({}) do |hash, column_name| + attribute_fields = filtered_attributes.each_with_object({}) do |column_name, hash| symbolized_column = column_name.to_sym if fields.key?(symbolized_column) || _protobuf_attribute_transformers.key?(symbolized_column) hash[symbolized_column] = fields[symbolized_column] end - - hash end _protobuf_nested_attributes.each do |attribute_name| - nested_attribute_name = "#{attribute_name}_attributes".to_sym + nested_attribute_name = :"#{attribute_name}_attributes" value = if proto.field?(nested_attribute_name) - proto.__send__(nested_attribute_name) - elsif proto.field?(attribute_name) - proto.__send__(attribute_name) - end + proto.__send__(nested_attribute_name) + elsif proto.field?(attribute_name) + proto.__send__(attribute_name) + end next unless value attribute_fields[nested_attribute_name] = value @@ -64,7 +62,7 @@ def _filter_attribute_fields(proto) # # :nodoc: def _filtered_attributes - return self.attribute_names + attribute_names end # :nodoc: @@ -72,18 +70,19 @@ def _protobuf_convert_fields_to_attributes(key, value) return nil if value.nil? return value unless _protobuf_date_datetime_time_or_timestamp_column?(key) - value = case - when _protobuf_datetime_column?(key) then - convert_int64_to_datetime(value) - when _protobuf_timestamp_column?(key) then - convert_int64_to_time(value) - when _protobuf_time_column?(key) then - convert_int64_to_time(value) - when _protobuf_date_column?(key) then - convert_int64_to_date(value) - end - - return value + if _protobuf_datetime_column?(key) + + convert_int64_to_datetime(value) + elsif _protobuf_timestamp_column?(key) + + convert_int64_to_time(value) + elsif _protobuf_time_column?(key) + + convert_int64_to_time(value) + elsif _protobuf_date_column?(key) + + convert_int64_to_date(value) + end end # Define an attribute transformation from protobuf. Accepts a Symbol, @@ -113,7 +112,7 @@ def attribute_from_proto(attribute, *args, &block) symbol_or_block = args.first || block if symbol_or_block.is_a?(Symbol) - callable = lambda { |value| self.__send__(symbol_or_block, value) } + callable = lambda { |value| __send__(symbol_or_block, value) } else raise AttributeTransformerError unless symbol_or_block.respond_to?(:call) callable = symbol_or_block @@ -138,7 +137,7 @@ def attribute_from_proto(attribute, *args, &block) def attributes_from_proto(proto) attribute_fields = _filter_attribute_fields(proto) - attributes = attribute_fields.inject({}) do |hash, (key, value)| + attributes = attribute_fields.each_with_object({}) do |(key, value), hash| if _protobuf_attribute_transformers.key?(key) transformer = _protobuf_attribute_transformers[key] attribute = transformer.call(proto) @@ -147,8 +146,6 @@ def attributes_from_proto(proto) else hash[key] = _protobuf_convert_fields_to_attributes(key, value) end - - hash end return attributes unless proto.field?(:nullify) && proto.nullify.is_a?(Array) diff --git a/lib/protobuf/active_record/transformer.rb b/lib/protobuf/active_record/transformer.rb index c3ba750..dac0721 100644 --- a/lib/protobuf/active_record/transformer.rb +++ b/lib/protobuf/active_record/transformer.rb @@ -8,7 +8,7 @@ def initialize(callable, options = {}) @options = options end - delegate :call, :to => :callable + delegate :call, to: :callable def nullify?(proto) return false unless options[:nullify_on] diff --git a/protobuf-activerecord.gemspec b/protobuf-activerecord.gemspec index 2acbf15..79e56e6 100644 --- a/protobuf-activerecord.gemspec +++ b/protobuf-activerecord.gemspec @@ -1,22 +1,19 @@ -# -*- encoding: utf-8 -*- - lib = File.expand_path("../lib", __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require "protobuf/active_record/version" Gem::Specification.new do |spec| - spec.name = "protobuf-activerecord" - spec.version = Protobuf::ActiveRecord::VERSION - spec.authors = ["Adam Hutchison"] - spec.email = ["liveh2o@gmail.com"] - spec.homepage = "http://github.com/liveh2o/protobuf-activerecord" - spec.summary = "Google Protocol Buffers integration for Active Record" - spec.description = "Provides the ability to create Active Record objects from Protocol Buffer messages and vice versa." - spec.license = "MIT" + spec.name = "protobuf-activerecord" + spec.version = Protobuf::ActiveRecord::VERSION + spec.authors = ["Adam Hutchison"] + spec.email = ["liveh2o@gmail.com"] + spec.homepage = "http://github.com/liveh2o/protobuf-activerecord" + spec.summary = "Google Protocol Buffers integration for Active Record" + spec.description = "Provides the ability to create Active Record objects from Protocol Buffer messages and vice versa." + spec.license = "MIT" - spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR) - spec.executables = spec.files.grep(%r{^bin/}).map { |f| File.basename(f) } - spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) + spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR) + spec.executables = spec.files.grep(%r{^bin/}).map { |f| File.basename(f) } spec.require_paths = ["lib"] ## @@ -32,12 +29,12 @@ Gem::Specification.new do |spec| # Development dependencies # spec.add_development_dependency "benchmark-ips" - spec.add_development_dependency "mad_rubocop" spec.add_development_dependency "rake" spec.add_development_dependency "rspec", ">= 3.3.0" spec.add_development_dependency "rspec-pride", ">= 3.1.0" spec.add_development_dependency "pry-nav" spec.add_development_dependency "simplecov" + spec.add_development_dependency "standard" if ENV["PLATFORM"] == "java" || ::RUBY_PLATFORM == "java" spec.platform = "java" diff --git a/spec/protobuf/active_record/columns_spec.rb b/spec/protobuf/active_record/columns_spec.rb index 9ad8b1b..4725e2b 100644 --- a/spec/protobuf/active_record/columns_spec.rb +++ b/spec/protobuf/active_record/columns_spec.rb @@ -4,17 +4,15 @@ describe "._protobuf_map_columns" do context "when the class has a table" do let(:expected_column_names) { - User.columns.inject({}) do |hash, column| + User.columns.each_with_object({}) do |column, hash| hash[column.name.to_sym] = column - hash end } let(:expected_column_types) { - User.columns.inject({}) do |hash, column| + User.columns.each_with_object({}) do |column, hash| hash[column.type.to_sym] ||= ::Set.new hash[column.type.to_sym] << column.name.to_sym - hash end } diff --git a/spec/protobuf/active_record/nested_attributes_spec.rb b/spec/protobuf/active_record/nested_attributes_spec.rb index dd8472f..1a02a72 100644 --- a/spec/protobuf/active_record/nested_attributes_spec.rb +++ b/spec/protobuf/active_record/nested_attributes_spec.rb @@ -2,10 +2,10 @@ describe Protobuf::ActiveRecord::NestedAttributes do let(:user_message) { - UserMessage.new(:name => "foo bar", :email => "foo@test.co", :photos => [{ :url => "https://test.co/test.png" }]) + UserMessage.new(name: "foo bar", email: "foo@test.co", photos: [{url: "https://test.co/test.png"}]) } - describe "._filter_attribute_fields", :aggregate_failures => true do + describe "._filter_attribute_fields", aggregate_failures: true do it "includes nested attributes" do attribute_fields = User._filter_attribute_fields(user_message) expect(attribute_fields[:photos_attributes]).to eq(user_message.photos) @@ -17,7 +17,7 @@ describe ".new" do context "when a model accepts nested attributes" do - it "transforms nested attributes", :aggregate_failures => true do + it "transforms nested attributes", aggregate_failures: true do user_message.photos.each do |photo_message| expect(Photo).to receive(:attributes_from_proto).with(photo_message).and_call_original end diff --git a/spec/protobuf/active_record/persistence_spec.rb b/spec/protobuf/active_record/persistence_spec.rb index abacc3b..0b1f1ed 100644 --- a/spec/protobuf/active_record/persistence_spec.rb +++ b/spec/protobuf/active_record/persistence_spec.rb @@ -2,8 +2,8 @@ describe Protobuf::ActiveRecord::Persistence do let(:user) { User.new(user_attributes) } - let(:user_attributes) { { :first_name => "foo", :last_name => "bar", :email => "foo@test.co" } } - let(:proto_hash) { { :name => "foo bar", :email => "foo@test.co" } } + let(:user_attributes) { {first_name: "foo", last_name: "bar", email: "foo@test.co"} } + let(:proto_hash) { {name: "foo bar", email: "foo@test.co"} } let(:proto) { UserMessage.new(proto_hash) } describe ".create" do diff --git a/spec/protobuf/active_record/scope_spec.rb b/spec/protobuf/active_record/scope_spec.rb index 8fd7304..76b6b72 100644 --- a/spec/protobuf/active_record/scope_spec.rb +++ b/spec/protobuf/active_record/scope_spec.rb @@ -1,26 +1,34 @@ require "spec_helper" +class TheEnum < ::Protobuf::Enum + define :VALUE, 1 +end + +class TheMessage < ::Protobuf::Message + optional TheEnum, :the_enum_value, 1 +end + describe Protobuf::ActiveRecord::Scope do before do - @field_parsers = User.instance_variable_get("@_searchable_field_parsers") - @fields = User.instance_variable_get("@_searchable_fields") + @field_parsers = User.instance_variable_get(:@_searchable_field_parsers) + @fields = User.instance_variable_get(:@_searchable_fields) end after do - User.instance_variable_set("@_searchable_field_parsers", @field_parsers) - User.instance_variable_set("@_searchable_fields", @fields) - User.instance_variable_set("@_upsert_keys", []) + User.instance_variable_set(:@_searchable_field_parsers, @field_parsers) + User.instance_variable_set(:@_searchable_fields, @fields) + User.instance_variable_set(:@_upsert_keys, []) end describe ".search_scope" do - let(:request) { UserSearchMessage.new(:guid => ["foo"], :email => ["foo@test.co"]) } + let(:request) { UserSearchMessage.new(guid: ["foo"], email: ["foo@test.co"]) } before { - allow(User).to receive(:searchable_field_parsers).and_return(:email => proc { |val| val }) + allow(User).to receive(:searchable_field_parsers).and_return(email: proc { |val| val }) } it "builds scopes for searchable fields" do - allow(User).to receive(:searchable_fields).and_return(:email => :by_email) + allow(User).to receive(:searchable_fields).and_return(email: :by_email) expect(User.search_scope(request)).to eq User.by_email("foo@test.co") end @@ -29,19 +37,19 @@ end context "when a searchable field does not have a value" do - let(:request) { UserSearchMessage.new(:email => ["foo@test.co"]) } + let(:request) { UserSearchMessage.new(email: ["foo@test.co"]) } it "doesn't build a scope from that field" do - allow(User).to receive(:searchable_fields).and_return(:email => :by_email) + allow(User).to receive(:searchable_fields).and_return(email: :by_email) expect(User.search_scope(request)).to eq User.by_email("foo@test.co") end end context "when a searchable field uses a non-existant scope" do - let(:request) { UserSearchMessage.new(:email => ["foo@test.co"]) } + let(:request) { UserSearchMessage.new(email: ["foo@test.co"]) } it "raises an exception" do - allow(User).to receive(:searchable_fields).and_return(:email => :by_hullabaloo) + allow(User).to receive(:searchable_fields).and_return(email: :by_hullabaloo) expect { User.search_scope(request) }.to raise_exception(/undefined method .*by_hullabaloo/i) end end @@ -57,7 +65,7 @@ context "when :scope is defined" do it "defines the given field as searchable using the given :scope" do - User.field_scope :guid, :scope => :custom_scope + User.field_scope :guid, scope: :custom_scope expect(User.searchable_fields[:guid]).to eq :custom_scope end end @@ -71,7 +79,7 @@ context "when :parser is defined" do it "defines the given field as parseable using the given :parser" do - User.field_scope :guid, :parser => :parser + User.field_scope :guid, parser: :parser expect(User.searchable_field_parsers[:guid]).to eq :parser end end @@ -79,16 +87,16 @@ describe ".parse_search_values" do it "converts single values to collections" do - proto = UserMessage.new(:email => "the.email@test.in") + proto = UserMessage.new(email: "the.email@test.in") User.field_scope :email expect(User.parse_search_values(proto, :email)).to eq ["the.email@test.in"] end context "when a field parser is defined" do - before { User.field_scope :guid, :parser => parser } + before { User.field_scope :guid, parser: parser } - let(:proto) { UserSearchMessage.new(:guid => ["foo"]) } + let(:proto) { UserSearchMessage.new(guid: ["foo"]) } context "and the parser does not respond to :to_sym" do let(:parser) { double("parser") } @@ -102,15 +110,7 @@ context "when the field is an enum" do it "maps values to integers" do - TheEnum = Class.new(::Protobuf::Enum) do - define :VALUE, 1 - end - - TheMessage = Class.new(::Protobuf::Message) do - optional TheEnum, :the_enum_value, 1 - end - - proto = TheMessage.new(:the_enum_value => TheEnum::VALUE) + proto = TheMessage.new(the_enum_value: TheEnum::VALUE) expect(User.parse_search_values(proto, :the_enum_value)[0]).to be 1 end end @@ -132,7 +132,7 @@ describe ".for_upsert" do let(:guid) { "USR-1" } - let(:proto) { ::UserMessage.new(:guid => guid) } + let(:proto) { ::UserMessage.new(guid: guid) } before do ::User.delete_all @@ -156,7 +156,7 @@ end context "existing record" do - before { ::User.create(:guid => guid) } + before { ::User.create(guid: guid) } after { ::User.delete_all } it "returns the existing record" do @@ -168,7 +168,7 @@ describe ".upsert" do let(:guid) { "USR-1" } - let(:proto) { ::UserMessage.new(:guid => guid, :email => "bar") } + let(:proto) { ::UserMessage.new(guid: guid, email: "bar") } before do ::User.delete_all @@ -184,7 +184,7 @@ end context "existing record" do - before { ::User.create(:guid => guid, :email => "foo") } + before { ::User.create(guid: guid, email: "foo") } after { ::User.delete_all } it "updates the existing record" do diff --git a/spec/protobuf/active_record/serialization_spec.rb b/spec/protobuf/active_record/serialization_spec.rb index d7e09fb..82d9fc9 100644 --- a/spec/protobuf/active_record/serialization_spec.rb +++ b/spec/protobuf/active_record/serialization_spec.rb @@ -24,7 +24,7 @@ class UnconfiguredUser end context "when the given transformer is callable" do - let(:callable) { lambda { |_proto| nil } } + let(:callable) { lambda { |_proto| } } before { allow(User).to receive(:_protobuf_field_transformers).and_return({}) @@ -38,7 +38,7 @@ class UnconfiguredUser end describe ".protobuf_message" do - let(:options) { { :only => [] } } + let(:options) { {only: []} } before { User.protobuf_message(protobuf_message, options) } after { User.protobuf_message(protobuf_message, {}) } @@ -69,14 +69,14 @@ class UnconfiguredUser describe "#_filter_field_attributes" do context "when options has :only" do it "only returns the given field(s)" do - fields = user._filter_field_attributes(:only => :name) + fields = user._filter_field_attributes(only: :name) expect(fields).to eq [:name] end end context "when options has :except" do it "returns all except the given field(s)" do - fields = user._filter_field_attributes(:except => :name) + fields = user._filter_field_attributes(except: :name) expect(fields).to match_array( [:guid, :email, :email_domain, :password, :nullify, :photos, :created_at, :updated_at] ) @@ -93,7 +93,7 @@ class UnconfiguredUser context "given :deprecated => false" do it "filters all deprecated fields" do - fields = user._filtered_fields(:deprecated => false) + fields = user._filtered_fields(deprecated: false) expect(fields).to match_array( [:guid, :name, :email, :password, :nullify, :photos, :created_at, :updated_at] ) @@ -101,7 +101,7 @@ class UnconfiguredUser context "and :include => :email_domain" do it "includes deprecated fields that have been explicitly specified" do - fields = user._filtered_fields(:deprecated => false, :include => :email_domain) + fields = user._filtered_fields(deprecated: false, include: :email_domain) expect(fields).to match_array( [:guid, :name, :email, :email_domain, :password, :nullify, :photos, :created_at, :updated_at] ) @@ -111,7 +111,7 @@ class UnconfiguredUser end describe "#_normalize_options" do - let(:options) { { :only => [:name] } } + let(:options) { {only: [:name]} } context "given empty options" do before { User.protobuf_message(protobuf_message, options) } @@ -140,7 +140,7 @@ class UnconfiguredUser end context "given options with :except" do - let(:options) { { :except => [:name] } } + let(:options) { {except: [:name]} } before { User.protobuf_message(protobuf_message, {}) } @@ -154,10 +154,10 @@ class UnconfiguredUser describe "#fields_from_record" do let(:attributes) { { - :guid => "foo", - :first_name => "bar", - :last_name => "baz", - :email => "foo@test.co" + guid: "foo", + first_name: "bar", + last_name: "baz", + email: "foo@test.co" } } @@ -169,7 +169,7 @@ class UnconfiguredUser context "given options with :include" do it "adds the given field to the list of serialized fields" do - fields = user.fields_from_record(:include => :token) + fields = user.fields_from_record(include: :token) expect(fields).to include(:token) end end @@ -178,7 +178,7 @@ class UnconfiguredUser let(:user) { User.create(attributes) } it "terminates the association proxy" do - fields = user.fields_from_record(:include => :photos) + fields = user.fields_from_record(include: :photos) expect(fields[:photos]).to be_an(Array) end end @@ -187,7 +187,7 @@ class UnconfiguredUser describe "#to_proto" do context "when a protobuf message is configured" do let(:proto) { protobuf_message.new(proto_hash) } - let(:proto_hash) { { :name => "foo" } } + let(:proto_hash) { {name: "foo"} } before { allow(user).to receive(:fields_from_record).and_return(proto_hash) } diff --git a/spec/protobuf/active_record/transformation_spec.rb b/spec/protobuf/active_record/transformation_spec.rb index b60374e..ee4ef2e 100644 --- a/spec/protobuf/active_record/transformation_spec.rb +++ b/spec/protobuf/active_record/transformation_spec.rb @@ -2,8 +2,8 @@ describe Protobuf::ActiveRecord::Transformation do let(:user) { User.new(user_attributes) } - let(:user_attributes) { { :first_name => "foo", :last_name => "bar", :email => "foo@test.co" } } - let(:proto_hash) { { :name => "foo bar", :email => "foo@test.co" } } + let(:user_attributes) { {first_name: "foo", last_name: "bar", email: "foo@test.co"} } + let(:proto_hash) { {name: "foo bar", email: "foo@test.co"} } let(:proto) { UserMessage.new(proto_hash) } describe "._filter_attribute_fields" do @@ -18,7 +18,7 @@ end it "includes attributes that aren't fields, but have attribute transformers" do - allow(User).to receive(:_protobuf_attribute_transformers).and_return(:account_id => :fetch_account_id) + allow(User).to receive(:_protobuf_attribute_transformers).and_return(account_id: :fetch_account_id) attribute_fields = User._filter_attribute_fields(proto) expect(attribute_fields.key?(:account_id)).to be true end @@ -119,12 +119,12 @@ end context "when a transformer is a callable that returns nil" do - let(:callable) { lambda { |_proto| nil } } + let(:callable) { lambda { |_proto| } } before do transformers = User._protobuf_attribute_transformers allow(User).to receive(:_protobuf_attribute_transformers).and_return( - { :account_id => transformer }.merge(transformers) + {account_id: transformer}.merge(transformers) ) end @@ -135,20 +135,20 @@ end context "when the transformer has a nullify_on option" do - let(:callable) { lambda { |_proto| nil } } - let(:transformer) { ::Protobuf::ActiveRecord::Transformer.new(callable, :nullify_on => :account_id) } - let(:proto_hash) { { :name => "foo bar", :email => "foo@test.co", :nullify => [:account_id] } } + let(:callable) { lambda { |_proto| } } + let(:transformer) { ::Protobuf::ActiveRecord::Transformer.new(callable, nullify_on: :account_id) } + let(:proto_hash) { {name: "foo bar", email: "foo@test.co", nullify: [:account_id]} } before do transformers = User._protobuf_attribute_transformers allow(User).to receive(:_protobuf_attribute_transformers).and_return( - { :account_id => transformer }.merge(transformers) + {account_id: transformer}.merge(transformers) ) end it "does not set the attribute" do attribute_fields = User.attributes_from_proto(proto) - expect(attribute_fields).to include(:account_id => nil) + expect(attribute_fields).to include(account_id: nil) end end @@ -156,13 +156,13 @@ before do transformers = User._protobuf_attribute_transformers allow(User).to receive(:_protobuf_attribute_transformers).and_return( - { :account_id => transformer }.merge(transformers) + {account_id: transformer}.merge(transformers) ) end it "sets the attribute" do attribute_fields = User.attributes_from_proto(proto) - expect(attribute_fields).to eq user_attributes.merge(:account_id => 1) + expect(attribute_fields).to eq user_attributes.merge(account_id: 1) end end @@ -199,7 +199,7 @@ end context "when the given transformer is callable" do - let(:callable) { lambda { |_proto| nil } } + let(:callable) { lambda { |_proto| } } before { allow(User).to receive(:_protobuf_attribute_transformers).and_return({}) } diff --git a/spec/protobuf/active_record/transformer_spec.rb b/spec/protobuf/active_record/transformer_spec.rb index d773c19..09cc29e 100644 --- a/spec/protobuf/active_record/transformer_spec.rb +++ b/spec/protobuf/active_record/transformer_spec.rb @@ -2,7 +2,7 @@ describe ::Protobuf::ActiveRecord::Transformer do let(:callable) { lambda { |proto| proto.name } } - let(:proto) { ::UserMessage.new(:name => "test", :nullify => ["name"]) } + let(:proto) { ::UserMessage.new(name: "test", nullify: ["name"]) } let(:options) { {} } subject { described_class.new(callable, options) } @@ -22,7 +22,7 @@ end context "nullify_on name" do - let(:options) { { :nullify_on => :name } } + let(:options) { {nullify_on: :name} } context "invalid message" do let(:proto) { ::UserSearchMessage.new } diff --git a/spec/support/db/setup.rb b/spec/support/db/setup.rb index 90334c8..d16b436 100644 --- a/spec/support/db/setup.rb +++ b/spec/support/db/setup.rb @@ -1,20 +1,20 @@ require "active_record" ActiveRecord::Base.establish_connection( - :adapter => "sqlite3", - :database => "spec/test.db" + adapter: "sqlite3", + database: "spec/test.db" ) ActiveRecord::Base.connection.data_sources.each do |table| ActiveRecord::Base.connection.drop_table(table) end -ActiveRecord::Schema.define(:version => 1) do +ActiveRecord::Schema.define(version: 1) do create_table :photos do |t| t.string :url t.integer :user_id - t.timestamps :null => false + t.timestamps null: false end create_table :users do |t| @@ -24,6 +24,6 @@ t.string :email t.integer :account_id - t.timestamps :null => false + t.timestamps null: false end end diff --git a/spec/support/models/user.rb b/spec/support/models/user.rb index 06762f9..7c53557 100644 --- a/spec/support/models/user.rb +++ b/spec/support/models/user.rb @@ -7,10 +7,10 @@ class User < ActiveRecord::Base accepts_nested_attributes_for :photos - scope :by_guid, lambda { |*guids| where(:guid => guids) } - scope :by_email, lambda { |*emails| where(:email => emails) } + scope :by_guid, lambda { |*guids| where(guid: guids) } + scope :by_email, lambda { |*emails| where(email: emails) } - protobuf_fields :except => :photos + protobuf_fields except: :photos attribute_from_proto :first_name, :extract_first_name attribute_from_proto :last_name, :extract_last_name diff --git a/spec/support/protobuf/messages.pb.rb b/spec/support/protobuf/messages.pb.rb index 1d50a87..e89da33 100644 --- a/spec/support/protobuf/messages.pb.rb +++ b/spec/support/protobuf/messages.pb.rb @@ -1,18 +1,16 @@ -# encoding: utf-8 - ## # This file is auto-generated. DO NOT EDIT! # -require 'protobuf' - +require "protobuf" ## # Message Classes # class PhotoMessage < ::Protobuf::Message; end + class UserMessage < ::Protobuf::Message; end -class UserSearchMessage < ::Protobuf::Message; end +class UserSearchMessage < ::Protobuf::Message; end ## # Message Fields @@ -26,7 +24,7 @@ class UserMessage optional :string, :guid, 1 optional :string, :name, 2 optional :string, :email, 3 - optional :string, :email_domain, 4, :deprecated => true + optional :string, :email_domain, 4, deprecated: true optional :string, :password, 5 repeated :string, :nullify, 6 repeated ::PhotoMessage, :photos, 7 @@ -38,4 +36,3 @@ class UserSearchMessage repeated :string, :guid, 1 repeated :string, :email, 2 end -