Skip to content

Commit

Permalink
Use anonymous variable for block where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
flash-gordon committed Jan 8, 2025
1 parent 8a65cbd commit ab4ac00
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 54 deletions.
14 changes: 7 additions & 7 deletions changeset/lib/rom/changeset/stateful.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ def self.map(**options, &block)
# @return [Array<Pipe>, 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
Expand Down Expand Up @@ -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) })
Expand Down
4 changes: 2 additions & 2 deletions core/lib/rom/commands/class_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions core/lib/rom/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions core/lib/rom/configuration_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions core/lib/rom/configuration_dsl/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions core/lib/rom/lint/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 19 additions & 19 deletions core/lib/rom/mapper/attribute_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]

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

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

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

Expand Down
4 changes: 2 additions & 2 deletions core/lib/rom/mapper/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions core/lib/rom/relation/view_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions core/lib/rom/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions core/lib/rom/schema/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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|
Expand Down
6 changes: 3 additions & 3 deletions core/lib/rom/struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions core/lib/rom/transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit ab4ac00

Please sign in to comment.