module Shrine::Plugins::Instrumentation::ClassMethods

  1. lib/shrine/plugins/instrumentation.rb

Methods

Public Instance

  1. instrument
  2. subscribe

Public Instance methods

instrument(event_name, payload = {}, &block)

Sends a *.shrine event.

# sends a `my_event.shrine` event
Shrine.instrument(:my_event) do
  # work
end
[show source]
   # File lib/shrine/plugins/instrumentation.rb
33 def instrument(event_name, payload = {}, &block)
34   payload[:uploader] = self
35 
36   notifications.instrument("#{event_name}.shrine", payload, &block)
37 end
subscribe(event_name, &subscriber)

Subscribes to a *.shrine event. It rejects duplicate subscribers.

# subscribes to the `storage_upload.shrine` event
Shrine.subscribe(:storage_upload) do |event|
  event.name #=> :storage_upload
  event.payload #=> { location: "...", ... }
  event[:location] #=> "..."
  event.duration #=> 50 (in milliseconds)
end
[show source]
   # File lib/shrine/plugins/instrumentation.rb
48 def subscribe(event_name, &subscriber)
49   return if subscriber.nil?
50   return if subscribers[event_name]&.include?(subscriber)
51 
52   notifications.subscribe("#{event_name}.shrine") do |event|
53     subscriber.call(event) if event[:uploader] <= self
54   end
55 
56   subscribers[event_name] ||= []
57   subscribers[event_name] << subscriber
58 end