Skip to content

Commit

Permalink
Add Standard Ruby, fix offenses
Browse files Browse the repository at this point in the history
Replace MadRubocop with Standard Ruby, a Ruby Style Guide with linter &
automatic code fixer, to adopt community styles, and fix all offenses.
  • Loading branch information
liveh2o committed Mar 4, 2024
1 parent 9195c43 commit 16b895f
Show file tree
Hide file tree
Showing 22 changed files with 169 additions and 210 deletions.
31 changes: 5 additions & 26 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions .standard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parallel: true # default: false
ruby_version: 3.0 # default: RUBY_VERSION
7 changes: 2 additions & 5 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/protobuf/active_record/attribute_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ 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

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
Expand Down
7 changes: 3 additions & 4 deletions lib/protobuf/active_record/columns.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require "set"
require "active_support/concern"
require "thread"

module Protobuf
module ActiveRecord
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require "concurrent"
require "thread"

module Protobuf
module ActiveRecord
Expand All @@ -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!
Expand Down
4 changes: 1 addition & 3 deletions lib/protobuf/active_record/middleware/query_cache.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "thread"

module Protobuf
module ActiveRecord
module Middleware
Expand All @@ -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
Expand Down
20 changes: 10 additions & 10 deletions lib/protobuf/active_record/scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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:
Expand All @@ -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

Expand Down Expand Up @@ -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:
Expand Down
62 changes: 29 additions & 33 deletions lib/protobuf/active_record/serialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
47 changes: 22 additions & 25 deletions lib/protobuf/active_record/transformation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -64,26 +62,27 @@ def _filter_attribute_fields(proto)
#
# :nodoc:
def _filtered_attributes
return self.attribute_names
attribute_names
end

# :nodoc:
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,
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Loading

0 comments on commit 16b895f

Please sign in to comment.