1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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

This commit is contained in:
José Valim 2010-01-15 11:01:37 +01:00
parent b0994be5bd
commit 7f4d8e3fbd
4 changed files with 8 additions and 14 deletions

View file

@ -106,9 +106,9 @@ module ActiveResource
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

View file

@ -45,7 +45,7 @@ module ActiveSupport
class << self
attr_writer :notifier
delegate :publish, :subscribe, :to => :notifier
delegate :instrument, :instrument!, :to => :instrumenter
delegate :instrument, :to => :instrumenter
def notifier
@notifier ||= Notifier.new

View file

@ -13,19 +13,13 @@ module ActiveSupport
# 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)

View file

@ -90,8 +90,8 @@ module Notifications
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