提交 7f4d8e3f 编写于 作者: J José Valim

Yield the payload notifications for further modification (like adding the result).

上级 b0994be5
......@@ -106,9 +106,9 @@ def head(path, headers = {})
private
# Makes a request to the remote service.
def request(method, path, *arguments)
result = ActiveSupport::Notifications.instrument!("active_resource.request",
:method => method, :path => path, :site => site) do
http.send(method, path, *arguments)
result = ActiveSupport::Notifications.instrument("active_resource.request",
:method => method, :path => path, :site => site) do |payload|
payload[:result] = http.send(method, path, *arguments)
end
handle_response(result)
rescue Timeout::Error => e
......
......@@ -45,7 +45,7 @@ module Notifications
class << self
attr_writer :notifier
delegate :publish, :subscribe, :to => :notifier
delegate :instrument, :instrument!, :to => :instrumenter
delegate :instrument, :to => :instrumenter
def notifier
@notifier ||= Notifier.new
......
......@@ -13,19 +13,13 @@ def initialize(notifier)
# Instrument the given block by measuring the time taken to execute it
# and publish it.
def instrument(name, payload={}, add_result=false)
def instrument(name, payload={})
time = Time.now
result = yield if block_given?
payload.merge!(:result => result) if add_result
result = yield(payload) if block_given?
@notifier.publish(name, time, Time.now, @id, payload)
result
end
# The same as instrument, but adds the result as payload.
def instrument!(name, payload={}, &block)
instrument(name, payload, true, &block)
end
private
def unique_id
SecureRandom.hex(10)
......
......@@ -90,8 +90,8 @@ def test_instrument_returns_block_result
drain
end
def test_instrument_with_band_adds_result_to_payload
assert_equal 2, instrument!(:awesome) { 1 + 1 }
def test_instrument_yields_the_paylod_for_further_modification
assert_equal 2, instrument(:awesome) { |p| p[:result] = 1 + 1 }
drain
assert_equal 1, @events.size
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册