Skip to content

Commit

Permalink
Merge pull request #49458 from eugeneius/broadcast_delegate_block
Browse files Browse the repository at this point in the history
Delegate block in broadcast logger method_missing
  • Loading branch information
eugeneius committed Oct 3, 2023
2 parents 5de585e + 5574a2f commit d24bbcd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
12 changes: 8 additions & 4 deletions activesupport/lib/active_support/broadcast_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,20 @@ def dispatch(&block)
@broadcasts.each { |logger| block.call(logger) }
end

def method_missing(name, *args)
def method_missing(name, *args, &block)
loggers = @broadcasts.select { |logger| logger.respond_to?(name) }

if loggers.none?
super(name, *args)
super(name, *args, &block)
elsif loggers.one?
loggers.first.send(name, *args)
loggers.first.send(name, *args, &block)
else
loggers.map { |logger| logger.send(name, *args) }
loggers.map { |logger| logger.send(name, *args, &block) }
end
end

def respond_to_missing?(method, include_all)
@broadcasts.any? { |logger| logger.respond_to?(method, include_all) }
end
end
end
14 changes: 14 additions & 0 deletions activesupport/test/broadcast_logger_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,16 @@ def info(msg, &block)
assert(logger.foo)
end

test "calling a method that accepts a block" do
logger = BroadcastLogger.new(CustomLogger.new)

called = false
logger.bar do
called = true
end
assert(called)
end

class CustomLogger
attr_reader :adds, :closed, :chevrons
attr_accessor :level, :progname, :formatter, :local_level
Expand All @@ -286,6 +296,10 @@ def foo
true
end

def bar
yield
end

def debug(message, &block)
add(::Logger::DEBUG, message, &block)
end
Expand Down

0 comments on commit d24bbcd

Please sign in to comment.