Module: Downstream

Defined in:
lib/downstream.rb,
lib/downstream/event.rb,
lib/downstream/config.rb,
lib/downstream/engine.rb,
lib/downstream/version.rb,
lib/downstream/rspec/have_published_event.rb

Defined Under Namespace

Classes: Config, Engine, Event, HavePublishedEvent

Constant Summary collapse

VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.configObject



12
13
14
# File 'lib/downstream.rb', line 12

def config
  @config ||= Config.new
end

.publish(event) ⇒ Object



59
60
61
# File 'lib/downstream.rb', line 59

def publish(event)
  ActiveSupport::Notifications.publish("#{config.namespace}.#{event.type}", event)
end

.subscribe(subscriber = nil, to: nil, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/downstream.rb', line 16

def subscribe(subscriber = nil, to: nil, &block)
  to ||= infer_event_from_subscriber(subscriber) if subscriber.is_a?(Module)

  if to.nil?
    raise ArgumentError, "Couldn't infer event from subscriber. " \
                          "Please, specify event using `to:` option"
  end

  subscriber ||= block if block

  if subscriber.nil?
    raise ArgumentError, "Subsriber must be present"
  end

  identifier =
    if to.is_a?(Class) && Event >= to
      to.identifier
    else
      to
    end

  ActiveSupport::Notifications.subscribe("#{config.namespace}.#{identifier}", subscriber)
end

.subscribed(subscriber, to: nil, &block) ⇒ Object

temporary subscriptions



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/downstream.rb', line 41

def subscribed(subscriber, to: nil, &block)
  to ||= infer_event_from_subscriber(subscriber) if subscriber.is_a?(Module)

  if to.nil?
    raise ArgumentError, "Couldn't infer event from subscriber. " \
                          "Please, specify event using `to:` option"
  end

  identifier =
    if to.is_a?(Class) && Event >= to
      to.identifier
    else
      to
    end

  ActiveSupport::Notifications.subscribed(subscriber, "#{config.namespace}.#{identifier}", &block)
end