Module: Informator

Defined in:
lib/informator.rb,
lib/informator/event.rb,
lib/informator/reporter.rb,
lib/informator/subscriber.rb

Overview

The module provides subscribe/publish/report features

Defined Under Namespace

Classes: Event, Reporter, Subscriber

Instance Method Summary collapse

Instance Method Details

#publish(type, messages, attributes) ⇒ Array<Informator::Event> #publishArray<Informator::Event>

Returns list of events having been published.

Overloads:

  • #publish(type, messages, attributes) ⇒ Array<Informator::Event>

    Builds the event and then publishes all unpublished events

  • #publishArray<Informator::Event>

    Publishes all unpublished events without adding a new one

Returns:



53
54
55
56
# File 'lib/informator.rb', line 53

def publish(*args)
  remember(*args) if args.any?
  __reporter__.notify __subscribers__
end

#publish(type, messages, attributes) ⇒ Object #publishObject

The same as ‘publish` except for it throws `:published` afterwards

Overloads:

  • #publish(type, messages, attributes) ⇒ Object

    Builds the event and then publishes all unpublished events

  • #publishObject

    Publishes all unpublished events without adding a new one

Raises:

  • (UncaughtThrowError, ArgumentError)

    for ‘:published` events being carried



71
72
73
# File 'lib/informator.rb', line 71

def publish!(*args)
  throw :published, publish(*args)
end

#remember(type, messages, attributes) ⇒ self

Builds and stores the event waiting for being published

Returns:

  • (self)

    itself



37
38
39
40
41
# File 'lib/informator.rb', line 37

def remember(*args)
  __reporter__.remember(*args)

  self
end

#subscribe(listener, callback = :receive) ⇒ self

Subscribes the listener for receiving events notifications via callback

Parameters:

  • listener (Object)

    The object to send events to

  • callback (#to_sym) (defaults to: :receive)

    (:receive) The name of the listener method to send events through

Returns:

  • (self)

    itself



23
24
25
26
27
28
# File 'lib/informator.rb', line 23

def subscribe(listener, callback = :receive)
  sub = Subscriber.new(listener, callback)
  __subscribers__ << sub unless __subscribers__.include? sub

  self
end