Module: Spree::Event::Adapters::ActiveSupportNotifications

Extended by:
ActiveSupportNotifications
Included in:
ActiveSupportNotifications
Defined in:
lib/spree/event/adapters/active_support_notifications.rb

Defined Under Namespace

Classes: InvalidEventNameType

Instance Method Summary collapse

Instance Method Details

#fire(event_name, opts) ⇒ Object



11
12
13
14
15
# File 'lib/spree/event/adapters/active_support_notifications.rb', line 11

def fire(event_name, opts)
  ActiveSupport::Notifications.instrument event_name, opts do
    yield opts if block_given?
  end
end

#listeners_for(names) ⇒ Object



28
29
30
31
32
33
# File 'lib/spree/event/adapters/active_support_notifications.rb', line 28

def listeners_for(names)
  names.each_with_object({}) do |name, memo|
    listeners = ActiveSupport::Notifications.notifier.listeners_for(name)
    memo[name] = listeners if listeners.present?
  end
end

#normalize_name(event_name) ⇒ Object

Normalizes the event name according to this specific adapter rules.

When the event name is a string or a symbol, if the suffix is missing, then
it is added automatically.
When the event name is a regexp, due to the huge variability of regexps, adding
or not the suffix is developer's responsibility (if you don't, you will subscribe
to all internal rails events as well).
When the event type is not supported, an error is raised.

Parameters:

  • event_name (String, Symbol, Regexp)

    the event name, with or without the suffix (Spree::Config.events.suffix defaults to ‘.spree`).



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/spree/event/adapters/active_support_notifications.rb', line 45

def normalize_name(event_name)
  case event_name
  when Regexp
    event_name
  when String, Symbol
    name = event_name.to_s
    name.end_with?(suffix) ? name : [name, suffix].join
  else
    raise InvalidEventNameType, "Invalid event name type: #{event_name.class}"
  end
end

#subscribe(event_name) ⇒ Object



17
18
19
20
21
22
# File 'lib/spree/event/adapters/active_support_notifications.rb', line 17

def subscribe(event_name)
  ActiveSupport::Notifications.subscribe event_name do |*args|
    event = ActiveSupport::Notifications::Event.new(*args)
    yield event
  end
end

#suffixObject

The suffix used for namespacing event names, defaults to ‘.spree`



61
62
63
# File 'lib/spree/event/adapters/active_support_notifications.rb', line 61

def suffix
  Spree::Config.events.suffix
end

#unsubscribe(subscriber_or_name) ⇒ Object



24
25
26
# File 'lib/spree/event/adapters/active_support_notifications.rb', line 24

def unsubscribe(subscriber_or_name)
  ActiveSupport::Notifications.unsubscribe(subscriber_or_name)
end