Class: Informator::Publisher
- Inherits:
-
Object
- Object
- Informator::Publisher
- Includes:
- Comparable
- Defined in:
- lib/informator/publisher.rb
Overview
The base class for publishers
Instance Attribute Summary collapse
-
#attributes ⇒ Hash
readonly
Initialized attributes.
-
#subscribers ⇒ Array<Informator::Subscriber>
readonly
The list of subscribers.
Class Method Summary collapse
-
.==(other) ⇒ Boolean
(also: eql?)
Treats two publishers of the same class with the same attributes as equal.
- .initialize(attributes = {}) ⇒ Object
-
.inspect ⇒ String
(also: to_s)
Human-readable description of the publisher.
-
.new(attributes = {}) ⇒ Informator::Publisher
Creates the immutable publisher.
-
.publish(name, arguments) ⇒ Informator::Event
Creates the immutable event and sends it to all subscribers.
-
.publish!(name, arguments) ⇒ Object
Does the same as [#publish], and then throws the ‘:published` exception, that carries an event.
-
.subscribe(listener, callback) ⇒ Informator::Publisher
Returns a new publisher with the listener being added to its subscribers.
Instance Attribute Details
#attributes ⇒ Hash (readonly)
Returns Initialized attributes.
23 24 25 |
# File 'lib/informator/publisher.rb', line 23 def attributes @attributes end |
#subscribers ⇒ Array<Informator::Subscriber> (readonly)
Returns The list of subscribers.
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
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 |
.inspect ⇒ String Also known as: to_s
Human-readable description of the publisher
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
|
# File 'lib/informator/publisher.rb', line 25
|
.publish(name, arguments) ⇒ Informator::Event
Creates the immutable event and sends it to all subscribers
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
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
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 |