From ab4ac003eb8b3f1809fedc8ccbecf09704f4cd12 Mon Sep 17 00:00:00 2001 From: Nikita Shilnikov Date: Wed, 8 Jan 2025 20:33:47 +0100 Subject: [PATCH] Use anonymous variable for block where possible --- changeset/lib/rom/changeset/stateful.rb | 14 ++++----- core/lib/rom/commands/class_interface.rb | 4 +-- core/lib/rom/configuration.rb | 4 +-- core/lib/rom/configuration_dsl.rb | 10 +++--- core/lib/rom/configuration_dsl/command.rb | 4 +-- core/lib/rom/lint/test.rb | 4 +-- core/lib/rom/mapper/attribute_dsl.rb | 38 +++++++++++------------ core/lib/rom/mapper/builder.rb | 4 +-- core/lib/rom/relation/view_dsl.rb | 4 +-- core/lib/rom/schema.rb | 6 ++-- core/lib/rom/schema/dsl.rb | 6 ++-- core/lib/rom/struct.rb | 6 ++-- core/lib/rom/transformer.rb | 4 +-- 13 files changed, 54 insertions(+), 54 deletions(-) diff --git a/changeset/lib/rom/changeset/stateful.rb b/changeset/lib/rom/changeset/stateful.rb index 5a3d784e4..cc0572ad7 100644 --- a/changeset/lib/rom/changeset/stateful.rb +++ b/changeset/lib/rom/changeset/stateful.rb @@ -73,9 +73,9 @@ def self.map(**options, &block) # @return [Array, Transproc::Function>] # # @api public - def self.extend(*, &block) - if block - map(use_for_diff: false, &block) + def self.extend(*, &) + if block_given? + map(use_for_diff: false, &) else super end @@ -148,12 +148,12 @@ def map(*steps, &) # @return [Changeset] # # @api public - def extend(*steps, **options, &block) - if block + def extend(*steps, **options, &) + if block_given? if steps.empty? - with(pipe: pipe.compose(Pipe.new(block).bind(self), **options)) + with(pipe: pipe.compose(Pipe.new(proc(&)).bind(self), **options)) else - extend(*steps, **options).extend(**options, &block) + extend(*steps, **options).extend(**options, &) end else with(pipe: steps.reduce(pipe.with(**options)) { |a, e| a.compose(pipe[e], **options) }) diff --git a/core/lib/rom/commands/class_interface.rb b/core/lib/rom/commands/class_interface.rb index 90330c607..f1adfc41c 100644 --- a/core/lib/rom/commands/class_interface.rb +++ b/core/lib/rom/commands/class_interface.rb @@ -84,12 +84,12 @@ def build(relation, **options) # @return [Class, Object] # # @api public - def create_class(name, type, &block) + def create_class(name, type, &) klass = Dry::Core::ClassBuilder .new(name: "#{Inflector.classify(type)}[:#{name}]", parent: type) .call - if block + if block_given? yield(klass) else klass diff --git a/core/lib/rom/configuration.rb b/core/lib/rom/configuration.rb index d5071ffdc..4da9adbe0 100644 --- a/core/lib/rom/configuration.rb +++ b/core/lib/rom/configuration.rb @@ -50,12 +50,12 @@ class Configuration # @return [Configuration] # # @api private - def initialize(*args, &block) + def initialize(*args, &) @environment = Environment.new(*args) @notifications = Notifications.event_bus(:configuration) @setup = Setup.new(notifications) - block&.call(self) + yield self if block_given? end # Apply a plugin to the configuration diff --git a/core/lib/rom/configuration_dsl.rb b/core/lib/rom/configuration_dsl.rb index 8868e7c39..104762f41 100644 --- a/core/lib/rom/configuration_dsl.rb +++ b/core/lib/rom/configuration_dsl.rb @@ -19,11 +19,11 @@ module ConfigurationDSL # end # # @api public - def relation(name, options = EMPTY_HASH, &block) + def relation(name, options = EMPTY_HASH, &) klass_opts = { adapter: default_adapter }.merge(options) klass = Relation.build_class(name, klass_opts) klass.schema_opts(dataset: name, relation: name) - klass.class_eval(&block) if block + klass.class_eval(&) if block_given? register_relation(klass) klass end @@ -76,14 +76,14 @@ def mappers(&block) # @return [Plugin] # # @api public - def plugin(adapter, spec, &block) + def plugin(adapter, spec, &) type, name = spec.flatten(1) plugin = plugin_registry[type].adapter(adapter).fetch(name) do plugin_registry[type].fetch(name) end - if block - register_plugin(plugin.configure(&block)) + if block_given? + register_plugin(plugin.configure(&)) else register_plugin(plugin) end diff --git a/core/lib/rom/configuration_dsl/command.rb b/core/lib/rom/configuration_dsl/command.rb index e02ae9b44..e57e7f185 100644 --- a/core/lib/rom/configuration_dsl/command.rb +++ b/core/lib/rom/configuration_dsl/command.rb @@ -13,7 +13,7 @@ class Command # This is used by Setup#commands DSL and its `define` block # # @api private - def self.build_class(name, relation, options = EMPTY_HASH, &block) + def self.build_class(name, relation, options = EMPTY_HASH, &) type = options.fetch(:type) { name } command_type = Inflector.classify(type) adapter = options.fetch(:adapter) @@ -23,7 +23,7 @@ def self.build_class(name, relation, options = EMPTY_HASH, &block) Dry::Core::ClassBuilder.new(name: class_name, parent: parent).call do |klass| klass.register_as(name) klass.relation(relation) - klass.class_eval(&block) if block + klass.class_eval(&) if block_given? end end diff --git a/core/lib/rom/lint/test.rb b/core/lib/rom/lint/test.rb index 4d5ffa576..8a9169083 100644 --- a/core/lib/rom/lint/test.rb +++ b/core/lib/rom/lint/test.rb @@ -12,9 +12,9 @@ module Test # @param [String] name # # @api private - def define_test_method(name, &block) + def define_test_method(name, &) define_method "test_#{name}" do - instance_eval(&block) + instance_eval(&) rescue ROM::Lint::Linter::Failure => e raise Minitest::Assertion, e.message end diff --git a/core/lib/rom/mapper/attribute_dsl.rb b/core/lib/rom/mapper/attribute_dsl.rb index e9c39aae8..43c08190b 100644 --- a/core/lib/rom/mapper/attribute_dsl.rb +++ b/core/lib/rom/mapper/attribute_dsl.rb @@ -89,11 +89,11 @@ def prefix_separator(value = Undefined) # @api public def attribute(name, options = EMPTY_HASH, &block) with_attr_options(name, options) do |attr_options| - if options[:type] && block + if options[:type] && block_given? raise ArgumentError, "can't specify type and block at the same time" end - attr_options[:coercer] = block if block + attr_options[:coercer] = block if block_given? add_attribute(name, attr_options) end end @@ -140,7 +140,7 @@ def step(options = EMPTY_HASH, &) # its attributes # # @api public - def embedded(name, options, &block) + def embedded(name, options, &) with_attr_options(name) do |attr_options| mapper = options[:mapper] @@ -150,7 +150,7 @@ def embedded(name, options, &block) mapper, name, embedded_options.update(attr_options) ) else - dsl = new(options, &block) + dsl = new(options, &) attr_options.update(options) add_attribute( name, { header: dsl.header, type: :array }.update(attr_options) @@ -178,7 +178,7 @@ def embedded(name, options, &block) # @see AttributeDSL#embedded # # @api public - def wrap(*args, &block) + def wrap(*args, &) ensure_mapper_configuration('wrap', args, block_given?) with_name_or_options(*args) do |name, options, mapper| @@ -187,7 +187,7 @@ def wrap(*args, &block) if mapper attributes_from_mapper(mapper, name, wrap_options) else - dsl(name, wrap_options, &block) + dsl(name, wrap_options, &) end end end @@ -211,14 +211,14 @@ def wrap(*args, &block) # @see AttributeDSL#embedded # # @api public - def unwrap(*args, &block) + def unwrap(*args, &) with_name_or_options(*args) do |name, options, mapper| unwrap_options = { type: :hash, unwrap: true }.update(options) if mapper attributes_from_mapper(mapper, name, unwrap_options) else - dsl(name, unwrap_options, &block) + dsl(name, unwrap_options, &) end end end @@ -240,7 +240,7 @@ def unwrap(*args, &block) # @see AttributeDSL#embedded # # @api public - def group(*args, &block) + def group(*args, &) ensure_mapper_configuration('group', args, block_given?) with_name_or_options(*args) do |name, options, mapper| @@ -249,7 +249,7 @@ def group(*args, &block) if mapper attributes_from_mapper(mapper, name, group_options) else - dsl(name, group_options, &block) + dsl(name, group_options, &) end end end @@ -265,10 +265,10 @@ def group(*args, &block) # @see AttributeDSL#embedded # # @api public - def ungroup(*args, &block) + def ungroup(*args, &) with_name_or_options(*args) do |name, options, *| ungroup_options = { type: :array, ungroup: true }.update(options) - dsl(name, ungroup_options, &block) + dsl(name, ungroup_options, &) end end @@ -285,10 +285,10 @@ def ungroup(*args, &block) # @see AttributeDSL#embedded # # @api public - def fold(*args, &block) + def fold(*args, &) with_name_or_options(*args) do |name, *| fold_options = { type: :array, fold: true } - dsl(name, fold_options, &block) + dsl(name, fold_options, &) end end @@ -407,9 +407,9 @@ def with_name_or_options(*args) # This is used by embedded, wrap and group # # @api private - def dsl(name_or_attrs, options, &block) - if block - attributes_from_block(name_or_attrs, options, &block) + def dsl(name_or_attrs, options, &) + if block_given? + attributes_from_block(name_or_attrs, options, &) else attributes_from_hash(name_or_attrs, options) end @@ -467,9 +467,9 @@ def add_attribute(name, options) # Embedded, wrap and group can override top-level options like `prefix` # # @api private - def new(options, &block) + def new(options, &) dsl = self.class.new([], @options.merge(options)) - dsl.instance_exec(&block) unless block.nil? + dsl.instance_exec(&) if block_given? dsl end diff --git a/core/lib/rom/mapper/builder.rb b/core/lib/rom/mapper/builder.rb index dd089be33..61029cba2 100644 --- a/core/lib/rom/mapper/builder.rb +++ b/core/lib/rom/mapper/builder.rb @@ -11,7 +11,7 @@ class Builder # This is used by Setup#mappers DSL # # @api private - def self.build_class(name, mapper_registry, options = EMPTY_HASH, &block) + def self.build_class(name, mapper_registry, options = EMPTY_HASH, &) class_name = "ROM::Mapper[#{name}]" parent = options[:parent] @@ -29,7 +29,7 @@ def self.build_class(name, mapper_registry, options = EMPTY_HASH, &block) klass.relation(name) klass.inherit_header(inherit_header) - klass.class_eval(&block) if block + klass.class_eval(&) if block_given? end end end diff --git a/core/lib/rom/relation/view_dsl.rb b/core/lib/rom/relation/view_dsl.rb index d47a08323..fb2e13fbe 100644 --- a/core/lib/rom/relation/view_dsl.rb +++ b/core/lib/rom/relation/view_dsl.rb @@ -39,8 +39,8 @@ def initialize(name, schema, &) # @see Relation::ClassInterface.view # # @api public - def schema(&block) - @new_schema = -> relations { @schema.with(relations: relations).instance_exec(&block) } + def schema(&) + @new_schema = -> relations { @schema.with(relations: relations).instance_exec(&) } end # Define a relation block for a relation view diff --git a/core/lib/rom/schema.rb b/core/lib/rom/schema.rb index ff9d2ba4a..6e7890d11 100644 --- a/core/lib/rom/schema.rb +++ b/core/lib/rom/schema.rb @@ -347,9 +347,9 @@ def append(*new_attributes) # @return [Schema] # # @api public - def uniq(&block) - if block - new(attributes.uniq(&block)) + def uniq(&) + if block_given? + new(attributes.uniq(&)) else new(attributes.uniq(&:name)) end diff --git a/core/lib/rom/schema/dsl.rb b/core/lib/rom/schema/dsl.rb index 882482cb1..9c1b1fc2d 100644 --- a/core/lib/rom/schema/dsl.rb +++ b/core/lib/rom/schema/dsl.rb @@ -10,7 +10,7 @@ class Schema # # @api public class DSL < ::BasicObject - KERNEL_METHODS = %i[extend method].freeze + KERNEL_METHODS = %i[extend method block_given?].freeze KERNEL_METHODS.each { |m| define_method(m, ::Kernel.instance_method(m)) } extend Initializer @@ -182,8 +182,8 @@ def app_plugin(plugin, options = ::ROM::EMPTY_HASH) end # @api private - def call(&block) - instance_exec(&block) if block + def call(&) + instance_exec(&) if block_given? instance_exec(&definition) if definition schema_class.define(relation, **opts) do |schema| diff --git a/core/lib/rom/struct.rb b/core/lib/rom/struct.rb index 4a6aa7426..46f84fffc 100644 --- a/core/lib/rom/struct.rb +++ b/core/lib/rom/struct.rb @@ -77,8 +77,8 @@ module ROM # @see http://dry-rb.org/gems/dry-types dry-types # # @api public - class Struct < Dry::Struct - MissingAttribute = Class.new(NameError) do + class Struct < ::Dry::Struct + MissingAttribute = ::Class.new(::NameError) do def initialize(&block) super @message_proc = block @@ -107,7 +107,7 @@ def respond_to_missing?(*) def method_missing(*) super - rescue NameError => e + rescue ::NameError => e raise(MissingAttribute.new { "#{e.message} (attribute not loaded?)" }) end end diff --git a/core/lib/rom/transformer.rb b/core/lib/rom/transformer.rb index 00b0b4cac..10a098092 100644 --- a/core/lib/rom/transformer.rb +++ b/core/lib/rom/transformer.rb @@ -70,9 +70,9 @@ def self.relation(name = Undefined, options = EMPTY_HASH) # @return [self] # # @api public - def self.map(&block) + def self.map(&) define! do - map_array(&block) + map_array(&) end end