From 8bf83fd92f55f3841537e7bb5088fcc17c84cbcc Mon Sep 17 00:00:00 2001 From: Nikita Shilnikov Date: Thu, 2 Jan 2025 21:59:43 +0100 Subject: [PATCH] Address rubocop offenses --- Rakefile | 4 +++- bin/console | 1 + core/benchmarks/setup.rb | 3 ++- core/lib/rom/array_dataset.rb | 10 ++++---- .../rom/associations/definitions/abstract.rb | 6 +++-- core/lib/rom/attribute.rb | 3 ++- core/lib/rom/command.rb | 8 +++---- core/lib/rom/commands/class_interface.rb | 20 ++++++++-------- core/lib/rom/configuration.rb | 12 +++++++--- core/lib/rom/data_proxy.rb | 24 +++++++++---------- core/lib/rom/enumerable_dataset.rb | 10 ++++---- core/lib/rom/header.rb | 4 ++-- core/lib/rom/lint/linter.rb | 2 +- core/lib/rom/memory/commands.rb | 2 +- core/lib/rom/memory/dataset.rb | 2 +- core/lib/rom/model_builder.rb | 8 +++---- core/lib/rom/plugins/command/timestamps.rb | 5 ++-- .../rom/plugins/relation/registry_reader.rb | 1 + core/lib/rom/relation/class_interface.rb | 8 +++---- core/lib/rom/relation/combined.rb | 4 ++-- core/spec/unit/rom/commands/graph_spec.rb | 2 +- core/spec/unit/rom/create_container_spec.rb | 3 ++- .../rom/relation/combined/map_with_spec.rb | 4 +++- 23 files changed, 82 insertions(+), 64 deletions(-) diff --git a/Rakefile b/Rakefile index d2fc7e686..86abf9fe0 100644 --- a/Rakefile +++ b/Rakefile @@ -42,4 +42,6 @@ task default: :spec begin require 'yard-junk/rake' YardJunk::Rake.define_task(:text) -rescue LoadError; end +rescue LoadError + # ignore +end diff --git a/bin/console b/bin/console index 1bf49e2e3..a00570886 100755 --- a/bin/console +++ b/bin/console @@ -12,6 +12,7 @@ require 'dry/monitor' %w[pry-byebug debug pry].each do |gem| require gem rescue LoadError + # ignore else break end diff --git a/core/benchmarks/setup.rb b/core/benchmarks/setup.rb index deb565927..7aa356c9c 100644 --- a/core/benchmarks/setup.rb +++ b/core/benchmarks/setup.rb @@ -13,6 +13,7 @@ begin require 'byebug' rescue LoadError + # ignore end require_relative 'gc_suite' @@ -277,7 +278,7 @@ class Tags < ROM::Relation[:sql] end }.flatten -def seed +def seed # rubocop:disable Metrics/AbcSize hr puts "SEEDING #{USER_SEED.count} users" diff --git a/core/lib/rom/array_dataset.rb b/core/lib/rom/array_dataset.rb index 1ed352648..c19ba6d24 100644 --- a/core/lib/rom/array_dataset.rb +++ b/core/lib/rom/array_dataset.rb @@ -35,11 +35,11 @@ def self.included(klass) map! combination cycle delete_if keep_if permutation reject! select! sort_by! ].each do |method| - class_eval <<-RUBY, __FILE__, __LINE__ + 1 - def #{method}(*args, &block) - return to_enum unless block - self.class.new(data.send(:#{method}, *args, &block), **options) - end + class_eval(<<-RUBY, __FILE__, __LINE__ + 1) + def #{method}(*args, &) # def map!(*args, &) + return to_enum unless block_given? # return to_enum unless block_given? + self.class.new(data.__send__(:#{method}, *args, &), **options) # self.class.new(data.__send__(:map!, *args, &), **options) + end # end RUBY end end diff --git a/core/lib/rom/associations/definitions/abstract.rb b/core/lib/rom/associations/definitions/abstract.rb index f1302ec19..4a59e7dc8 100644 --- a/core/lib/rom/associations/definitions/abstract.rb +++ b/core/lib/rom/associations/definitions/abstract.rb @@ -67,8 +67,10 @@ class Abstract # @option opts [Symbol] :as The name of the association (defaults to target) # @option opts [Symbol] :relation The name of the target relation (defaults to target) # @option opts [Symbol] :foreign_key The name of a custom foreign key - # @option opts [Symbol] :view The name of a custom relation view on the target's relation side - # @option opts [TrueClass,FalseClass] :override Whether provided :view should override association's default view + # @option opts [Symbol] :view The name of a custom relation view + # on the target's relation side + # @option opts [true, false] :override Whether provided :view should override + # association's default view # # @api public def self.new(source, target, **opts) diff --git a/core/lib/rom/attribute.rb b/core/lib/rom/attribute.rb index 7187f14ea..b70b06b12 100644 --- a/core/lib/rom/attribute.rb +++ b/core/lib/rom/attribute.rb @@ -23,7 +23,8 @@ class Attribute META_OPTIONS = %i[primary_key foreign_key source target relation].freeze # @!attribute [r] type - # @return [Dry::Types::Nominal, Dry::Types::Sum, Dry::Types::Constrained] The attribute's type object + # @return [Dry::Types::Nominal, Dry::Types::Sum, Dry::Types::Constrained] The attribute's + # type object param :type # @!attribute [r] name diff --git a/core/lib/rom/command.rb b/core/lib/rom/command.rb index 7e21ae7ad..3159355f1 100644 --- a/core/lib/rom/command.rb +++ b/core/lib/rom/command.rb @@ -265,7 +265,7 @@ def execute(*) # This method will apply before/after hooks automatically # # @api public - def call(*args, &block) + def call(*args, &) tuples = if hooks? prepared = @@ -275,7 +275,7 @@ def call(*args, &block) apply_hooks(before_hooks, *args) end - result = prepared ? execute(prepared, &block) : execute(&block) + result = prepared ? execute(prepared, &) : execute(&) if curried? if !args.empty? @@ -286,10 +286,10 @@ def call(*args, &block) apply_hooks(after_hooks, result) end else - apply_hooks(after_hooks, result, *args[1..args.size - 1]) + apply_hooks(after_hooks, result, *args.drop(1)) end else - execute(*(curry_args + args), &block) + execute(*(curry_args + args), &) end if one? diff --git a/core/lib/rom/commands/class_interface.rb b/core/lib/rom/commands/class_interface.rb index 0a6637eed..90330c607 100644 --- a/core/lib/rom/commands/class_interface.rb +++ b/core/lib/rom/commands/class_interface.rb @@ -252,16 +252,16 @@ def options def relation_methods_mod(relation_class) Module.new do relation_class.view_methods.each do |meth| - module_eval <<-RUBY, __FILE__, __LINE__ + 1 - def #{meth}(*args, **kwargs) - response = relation.public_send(:#{meth}, *args, **kwargs) - - if response.is_a?(relation.class) - new(response) - else - response - end - end + module_eval(<<-RUBY, __FILE__, __LINE__ + 1) + def #{meth}(*args, **kwargs) # def create(*args, **kwargs) + response = relation.public_send(:#{meth}, *args, **kwargs) # response = relation.public_send(:create, *args, **kwargs) + # + if response.is_a?(relation.class) # if response.is_a?(relation.class) + new(response) # new(response) + else # else + response # response + end # end + end # end RUBY end end diff --git a/core/lib/rom/configuration.rb b/core/lib/rom/configuration.rb index 32e63bb3d..d5071ffdc 100644 --- a/core/lib/rom/configuration.rb +++ b/core/lib/rom/configuration.rb @@ -36,7 +36,8 @@ class Configuration # @return [Notifications] Notification bus instance attr_reader :notifications - def_delegators :@setup, :register_relation, :register_command, :register_mapper, :register_plugin, + def_delegators :@setup, :register_relation, :register_command, + :register_mapper, :register_plugin, :command_classes, :mapper_classes, :auto_registration @@ -89,7 +90,7 @@ def [](name) # Hook for respond_to? used internally # # @api private - def respond_to?(name, include_all = false) + def respond_to?(name, ...) gateways.key?(name) || super end @@ -122,12 +123,17 @@ def default_adapter private + # @api private + def respond_to_missing?(name, ...) + gateways.key?(name) || super + end + # Returns gateway if method is a name of a registered gateway # # @return [Gateway] # # @api private - def method_missing(name, *) + def method_missing(name, ...) gateways.fetch(name) { super } end end diff --git a/core/lib/rom/data_proxy.rb b/core/lib/rom/data_proxy.rb index 5d0b527b6..192d42a37 100644 --- a/core/lib/rom/data_proxy.rb +++ b/core/lib/rom/data_proxy.rb @@ -77,18 +77,18 @@ def forward(*methods) # FIXME: we should probably raise if one of the non-forwardable methods # was provided (methods - NON_FORWARDABLE).each do |method_name| - class_eval <<-RUBY, __FILE__, __LINE__ + 1 - def #{method_name}(*args, &block) - response = data.public_send(#{method_name.inspect}, *args, &block) - - if response.equal?(data) - self - elsif response.is_a?(data.class) - self.class.new(response) - else - response - end - end + class_eval(<<-RUBY, __FILE__, __LINE__ + 1) + def #{method_name}(...) # def find_all(...) + response = data.public_send(:#{method_name}, ...) # response = data.public_send(:find_all, ...) + # + if response.equal?(data) # if response.equal?(data) + self # self + elsif response.is_a?(data.class) # elsif response.is_a?(data.class) + self.class.new(response) # self.class.new(response) + else # else + response # response + end # end + end # end RUBY end end diff --git a/core/lib/rom/enumerable_dataset.rb b/core/lib/rom/enumerable_dataset.rb index 0e23b6895..7e1142143 100644 --- a/core/lib/rom/enumerable_dataset.rb +++ b/core/lib/rom/enumerable_dataset.rb @@ -59,11 +59,11 @@ def self.included(klass) chunk collect collect_concat drop_while find_all flat_map grep map reject select sort sort_by take_while ].each do |method| - class_eval <<-RUBY, __FILE__, __LINE__ + 1 - def #{method}(*args, &block) - return to_enum unless block - self.class.new(super(*args, &block), **options) - end + class_eval(<<-RUBY, __FILE__, __LINE__ + 1) + def #{method}(...) # def collect(...) + return to_enum unless block_given? # return to_enum unless block_given? + self.class.new(super(...), **options) # self.class.new(super(...), **options) + end # end RUBY end end diff --git a/core/lib/rom/header.rb b/core/lib/rom/header.rb index b81e5d623..7edc53eda 100644 --- a/core/lib/rom/header.rb +++ b/core/lib/rom/header.rb @@ -80,8 +80,8 @@ def initialize(attributes, options = {}) # @yield [Attribute] # # @api private - def each - attributes.each_value { |attribute| yield(attribute) } + def each(&) + attributes.each_value(&) end # Return if there are any aliased attributes diff --git a/core/lib/rom/lint/linter.rb b/core/lib/rom/lint/linter.rb index 02ffdcdbc..e29e23af7 100644 --- a/core/lib/rom/lint/linter.rb +++ b/core/lib/rom/lint/linter.rb @@ -52,7 +52,7 @@ def lint(name) # @return [String] # # @api private - def self.lints + private_class_method def self.lints public_instance_methods(true).grep(/^lint_/).map(&:to_s) end diff --git a/core/lib/rom/memory/commands.rb b/core/lib/rom/memory/commands.rb index f1a0923a8..a3d68eab0 100644 --- a/core/lib/rom/memory/commands.rb +++ b/core/lib/rom/memory/commands.rb @@ -17,7 +17,7 @@ class Create < ROM::Commands::Create # @see ROM::Commands::Create#execute def execute(tuples) - Array([tuples]).flatten.map { |tuple| + [tuples].flatten.map { |tuple| attributes = input[tuple] relation.insert(attributes.to_h) attributes diff --git a/core/lib/rom/memory/dataset.rb b/core/lib/rom/memory/dataset.rb index 982f5262e..edeb53732 100644 --- a/core/lib/rom/memory/dataset.rb +++ b/core/lib/rom/memory/dataset.rb @@ -23,7 +23,7 @@ def join(*args) left, right = args.size > 1 ? args : [self, args.first] join_map = left.each_with_object({}) { |tuple, h| - others = right.to_a.find_all { |t| (tuple.to_a & t.to_a).any? } + others = right.to_a.find_all { |t| tuple.to_a.intersect?(t.to_a) } (h[tuple] ||= []).concat(others) } diff --git a/core/lib/rom/model_builder.rb b/core/lib/rom/model_builder.rb index bcf2014c7..1e3036cc3 100644 --- a/core/lib/rom/model_builder.rb +++ b/core/lib/rom/model_builder.rb @@ -94,10 +94,10 @@ def define_class(attrs) @klass.send(:attr_reader, *attrs) - @klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1 - def initialize(params) - #{attrs.map { |name| "@#{name} = params[:#{name}]" }.join("\n")} - end + @klass.class_eval(<<-RUBY, __FILE__, __LINE__ + 1) + def initialize(params) # def initialize(params) + #{attrs.map { |name| "@#{name} = params[:#{name}]" }.join("\n")} # @name = params[:name] + end # end RUBY self diff --git a/core/lib/rom/plugins/command/timestamps.rb b/core/lib/rom/plugins/command/timestamps.rb index f0d2fe3ed..9d2624cd0 100644 --- a/core/lib/rom/plugins/command/timestamps.rb +++ b/core/lib/rom/plugins/command/timestamps.rb @@ -22,19 +22,20 @@ module Command # result[:created_at] #=> Time.now.utc # # @api public - class Timestamps < Module + class Timestamps < ::Module attr_reader :timestamps attr_reader :datestamps def initialize(timestamps: [], datestamps: []) + super() @timestamps = store_attributes(timestamps) @datestamps = store_attributes(datestamps) end # @api private def store_attributes(attr) - attr.is_a?(Array) ? attr : Array[attr] + attr.is_a?(Array) ? attr : [attr] end # @api private diff --git a/core/lib/rom/plugins/relation/registry_reader.rb b/core/lib/rom/plugins/relation/registry_reader.rb index b5861cdd8..fec206387 100644 --- a/core/lib/rom/plugins/relation/registry_reader.rb +++ b/core/lib/rom/plugins/relation/registry_reader.rb @@ -15,6 +15,7 @@ class RegistryReader < ::Module # @api private def initialize(klass:, relation_readers_module:) + super() klass.include relation_readers_module end diff --git a/core/lib/rom/relation/class_interface.rb b/core/lib/rom/relation/class_interface.rb index df773245a..7d7353150 100644 --- a/core/lib/rom/relation/class_interface.rb +++ b/core/lib/rom/relation/class_interface.rb @@ -91,10 +91,10 @@ def dataset(&block) # @param [Boolean] infer Whether to do an automatic schema inferring # # @api public - def schema(dataset = nil, as: nil, infer: false, &block) - if defined?(@schema) && !block && !infer + def schema(dataset = nil, as: nil, infer: false, &) + if defined?(@schema) && !block_given? && !infer @schema - elsif block || infer + elsif block_given? || infer raise MissingSchemaClassError, self unless schema_class ds_name = dataset || schema_opts.fetch(:dataset, default_name.dataset) @@ -110,7 +110,7 @@ def schema(dataset = nil, as: nil, infer: false, &block) schema_class: schema_class, attr_class: schema_attr_class, inferrer: schema_inferrer.with(enabled: infer), - &block + & ).call(*args, &inner_block) end end diff --git a/core/lib/rom/relation/combined.rb b/core/lib/rom/relation/combined.rb index c6b92d2d6..28cb9bedc 100644 --- a/core/lib/rom/relation/combined.rb +++ b/core/lib/rom/relation/combined.rb @@ -95,7 +95,7 @@ def call(*args) # @return [Relation] # # @api public - def node(name, &block) + def node(name, &) if name.is_a?(Symbol) && !nodes.map { |n| n.name.key }.include?(name) raise ArgumentError, "#{name.inspect} is not a valid aggregate node name" end @@ -107,7 +107,7 @@ def node(name, &block) when Hash other, *rest = name.flatten(1) if other == node.name.key - nodes.detect { |n| n.name.key == other }.node(*rest, &block) + nodes.detect { |n| n.name.key == other }.node(*rest, &) else node end diff --git a/core/spec/unit/rom/commands/graph_spec.rb b/core/spec/unit/rom/commands/graph_spec.rb index 1722ff2cb..622267a63 100644 --- a/core/spec/unit/rom/commands/graph_spec.rb +++ b/core/spec/unit/rom/commands/graph_spec.rb @@ -100,7 +100,7 @@ def associate(tasks, user) before :associate def associate(tags, tasks) - Array([tasks]).flatten.map { |task| + [tasks].flatten.map { |task| tags.map { |tag| tag.merge(task: task[:title]) } }.flatten end diff --git a/core/spec/unit/rom/create_container_spec.rb b/core/spec/unit/rom/create_container_spec.rb index fcefa0326..5cfae7380 100644 --- a/core/spec/unit/rom/create_container_spec.rb +++ b/core/spec/unit/rom/create_container_spec.rb @@ -92,7 +92,8 @@ def orange? expect { container }.not_to raise_error end - it "doesn't raise an error when registering same mapper twice for different relation when no relation specify" do + it "doesn't raise an error when registering same mapper " \ + 'twice for different relation when no relation specify' do configuration users_mapper = Class.new(ROM::Mapper) do diff --git a/core/spec/unit/rom/relation/combined/map_with_spec.rb b/core/spec/unit/rom/relation/combined/map_with_spec.rb index 41633617d..a487e0af4 100644 --- a/core/spec/unit/rom/relation/combined/map_with_spec.rb +++ b/core/spec/unit/rom/relation/combined/map_with_spec.rb @@ -4,7 +4,9 @@ RSpec.describe ROM::Relation::Combined, '#map_with' do subject(:relation) do - ROM::Relation::Combined.new(users, [tasks.to_node(:tasks, type: :many, keys: { id: :user_id }).for_users]) + ROM::Relation::Combined.new( + users, [tasks.to_node(:tasks, type: :many, keys: { id: :user_id }).for_users] + ) end let(:users) do