Skip to content

Commit

Permalink
Use anonymous variable for blocks where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
flash-gordon committed Jan 8, 2025
1 parent a9e5c36 commit 5edc9ed
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 52 deletions.
6 changes: 3 additions & 3 deletions lib/rom/sql/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ def initialize(schema)
end

# @api private
def call(&block)
def call(&)
arg, kwargs = select_relations(block.parameters)

if kwargs.nil?
result = instance_exec(arg, &block)
result = instance_exec(arg, &)
else
result = instance_exec(**kwargs, &block)
result = instance_exec(**kwargs, &)
end

if result.is_a?(::Array)
Expand Down
12 changes: 6 additions & 6 deletions lib/rom/sql/function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ def case(mapping)
# @return [SQL::Function]
#
# @api public
def filter(condition = Undefined, &block)
if block
conditions = schema.restriction(&block)
def filter(condition = Undefined, &)
if block_given?
conditions = schema.restriction(&)
conditions &= condition unless condition.equal?(Undefined)
else
conditions = condition
Expand All @@ -246,9 +246,9 @@ def filter(condition = Undefined, &block)
# @return [SQL::Function]
#
# @api public
def within_group(*args, &block)
if block
group = args + ::ROM::SQL::OrderDSL.new(schema).(&block)
def within_group(*args, &)
if block_given?
group = args + ::ROM::SQL::OrderDSL.new(schema).(&)
else
group = args
end
Expand Down
6 changes: 3 additions & 3 deletions lib/rom/sql/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ class << self
# @param [Symbol] gateway The gateway name, :default by default
#
# @api public
def migration(*args, &block)
def migration(*args, &)
if args.any?
container, gateway, * = args
with_gateway(container.gateways[gateway || :default]) { migration(&block) }
with_gateway(container.gateways[gateway || :default]) { migration(&) }
else
current_gateway.migration(&block)
current_gateway.migration(&)
end
end

Expand Down
8 changes: 4 additions & 4 deletions lib/rom/sql/migration/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ module Migration
class Recorder
attr_reader :operations

def initialize(&block)
def initialize(&)
@operations = []

instance_exec(&block) if block
instance_exec(&) if block_given?
end

private
Expand All @@ -19,8 +19,8 @@ def respond_to_missing?(_m, _include_private = false)
true
end

def method_missing(m, *args, &block)
nested = block ? Recorder.new(&block).operations : EMPTY_ARRAY
def method_missing(m, *args, &)
nested = block_given? ? Recorder.new(&).operations : EMPTY_ARRAY
@operations << [m, args, nested]
end
end
Expand Down
58 changes: 29 additions & 29 deletions lib/rom/sql/relation/reading.rb
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,9 @@ def avg(*args)
# @return [Relation]
#
# @api public
def where(*args, &block)
if block
where(*args).where(schema.canonical.restriction(&block))
def where(*args, &)
if block_given?
where(*args).where(schema.canonical.restriction(&))
elsif args.size == 1 && args[0].is_a?(Hash)
new(dataset.where(coerce_conditions(args[0])))
elsif !args.empty?
Expand Down Expand Up @@ -420,9 +420,9 @@ def exclude(...)
# @return [Relation]
#
# @api public
def having(*args, &block)
if block
new(dataset.having(*args, *schema.canonical.restriction(&block)))
def having(*args, &)
if block_given?
new(dataset.having(*args, *schema.canonical.restriction(&)))
else
new(dataset.__send__(__method__, *args))
end
Expand Down Expand Up @@ -475,11 +475,11 @@ def invert
# @return [Relation]
#
# @api public
def order(*args, &block)
if block
new(dataset.order(*args, *schema.canonical.order(&block)))
def order(*args, &)
if block_given?
new(dataset.order(*args, *schema.canonical.order(&)))
else
new(dataset.__send__(__method__, *args, &block))
new(dataset.__send__(__method__, *args, &))
end
end

Expand Down Expand Up @@ -724,12 +724,12 @@ def right_join(...)
# @return [Relation]
#
# @api public
def group(*args, &block)
if block
def group(*args, &)
if block_given?
if args.size.positive?
group(*args).group_append(&block)
group(*args).group_append(&)
else
new(dataset.__send__(__method__, *schema.canonical.group(&block)))
new(dataset.__send__(__method__, *schema.canonical.group(&)))
end
else
new(dataset.__send__(__method__, *schema.canonical.project(*args)))
Expand Down Expand Up @@ -763,12 +763,12 @@ def group(*args, &block)
# @return [Relation]
#
# @api public
def group_append(*args, &block)
if block
def group_append(*args, &)
if block_given?
if args.size.positive?
group_append(*args).group_append(&block)
group_append(*args).group_append(&)
else
new(dataset.group_append(*schema.canonical.group(&block)))
new(dataset.group_append(*schema.canonical.group(&)))
end
else
new(dataset.group_append(*args))
Expand Down Expand Up @@ -910,12 +910,12 @@ def read(sql)
# @yieldparam relation [Array]
#
# @api public
def lock(**options, &block)
def lock(**options, &)
clause = lock_clause(**options)

if block
if block_given?
transaction do
block.call(dataset.lock_style(clause).to_a)
yield(dataset.lock_style(clause).to_a)
end
else
new(dataset.lock_style(clause))
Expand Down Expand Up @@ -1094,21 +1094,21 @@ def coerce_conditions(conditions)
# @api private
#
# rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity, Metrics/MethodLength, Metrics/CyclomaticComplexity
def __join__(type, other, join_cond = EMPTY_HASH, opts = EMPTY_HASH, &block)
def __join__(type, other, join_cond = EMPTY_HASH, opts = EMPTY_HASH, &)
if other.is_a?(Symbol) || other.is_a?(ROM::Relation::Name)
if join_cond.equal?(EMPTY_HASH) && !block
if join_cond.equal?(EMPTY_HASH) && !block_given?
assoc = associations[other]
assoc.join(type, self)
elsif block
__join__(type, other, JoinDSL.new(schema).(&block), opts)
elsif block_given?
__join__(type, other, JoinDSL.new(schema).(&), opts)
else
new(dataset.__send__(type, other.to_sym, join_cond, opts, &block))
new(dataset.__send__(type, other.to_sym, join_cond, opts, &))
end
elsif other.is_a?(Sequel::SQL::AliasedExpression)
new(dataset.__send__(type, other, join_cond, opts, &block))
elsif other.is_a?(::Sequel::SQL::AliasedExpression)
new(dataset.__send__(type, other, join_cond, opts, &))
elsif other.respond_to?(:name) && other.name.is_a?(Relation::Name)
if block
join_cond = JoinDSL.new(schema).(&block)
join_cond = JoinDSL.new(schema).(&)

if other.name.aliaz
join_opts = { table_alias: other.name.aliaz }
Expand Down
6 changes: 3 additions & 3 deletions lib/rom/sql/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ def qualified_projection(table_alias = nil)
# @return [Schema] A new schema with projected attributes
#
# @api public
def project(*names, &block)
if block
super(*(names + ProjectionDSL.new(self).(&block)))
def project(*names, &)
if block_given?
super(*(names + ProjectionDSL.new(self).(&)))
else
super
end
Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ module Types
include Dry.Types(default: :strict)
end

def with_adapters(*args, &block)
def with_adapters(*args, &)
reset_adapter = Hash[*ADAPTERS.flat_map { |a| [a, false] }]
adapters = args.empty? || args[0] == :all ? ADAPTERS : (args & ADAPTERS)

adapters.each do |adapter|
context("with #{adapter}", **reset_adapter, adapter => true, &block)
context("with #{adapter}", **reset_adapter, adapter => true, &)
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/unit/relation/instrument_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def initialize
@logs = []
end

def instrument(*args, &block)
def instrument(*args, &)
logs << args
block.call
yield
end
end.new
end
Expand Down

0 comments on commit 5edc9ed

Please sign in to comment.