Class: Informator::Publisher

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/informator/publisher.rb

Overview

The base class for publishers

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#attributesHash (readonly)

Returns Initialized attributes.

Returns:

  • (Hash)

    Initialized attributes



23
24
25
# File 'lib/informator/publisher.rb', line 23

def attributes
  @attributes
end

#subscribersArray<Informator::Subscriber> (readonly)

Returns The list of subscribers.

Returns:



17
18
19
# File 'lib/informator/publisher.rb', line 17

def subscribers
  @subscribers
end

Class Method Details

.==(other) ⇒ Boolean Also known as: eql?

Treats two publishers of the same class with the same attributes as equal

Parameters:

  • other (Object)

Returns:

  • (Boolean)


87
88
89
# File 'lib/informator/publisher.rb', line 87

def ==(other)
  other.instance_of?(self.class) && attributes.eql?(other.attributes)
end

.initialize(attributes = {}) ⇒ Object



34
35
36
37
38
# File 'lib/informator/publisher.rb', line 34

def initialize(attributes = {})
  @attributes  = Hash[attributes]
  @subscribers = block_given? ? yield : Set.new
  IceNine.deep_freeze(self)
end

.inspectString Also known as: to_s

Human-readable description of the publisher

Returns:

  • (String)


96
97
98
# File 'lib/informator/publisher.rb', line 96

def inspect
  "#<#{self.class} @attributes=#{attributes}>"
end

.new(attributes = {}) ⇒ Informator::Publisher

Creates the immutable publisher

Parameters:

  • attributes (Hash) (defaults to: {})

Returns:



# File 'lib/informator/publisher.rb', line 25

.publish(name, arguments) ⇒ Informator::Event

Creates the immutable event and sends it to all subscribers

Parameters:

  • name (#to_sym)

    The name of the event

  • arguments (Hash)

    The arguments of the event

Returns:



61
62
63
64
65
# File 'lib/informator/publisher.rb', line 61

def publish(name, arguments)
  event = Event.new(self, name, arguments)
  subscribers.each { |subscriber| subscriber.notify(event) }
  event
end

.publish!(name, arguments) ⇒ Object

Does the same as [#publish], and then throws the ‘:published` exception, that carries an event

Raises:

  • (UncaughtThrowError)

    The exception to be catched later



76
77
78
79
# File 'lib/informator/publisher.rb', line 76

def publish!(name, arguments)
  event = publish(name, arguments)
  throw :published, event
end

.subscribe(listener, callback) ⇒ Informator::Publisher

Returns a new publisher with the listener being added to its subscribers

Parameters:

  • listener (Object)
  • callback (Symbol, String)

    The name of the listener method, that should receive events The method is expected to accept one argument (for the event)

Returns:



49
50
51
52
# File 'lib/informator/publisher.rb', line 49

def subscribe(listener, callback)
  subscriber = Subscriber.new(listener, callback)
  self.class.new(attributes) { subscribers | [subscriber] }
end