Class: NotifyHub
- Inherits:
-
Object
- Object
- NotifyHub
- Defined in:
- lib/notifyhub.rb
Overview
NotifyHub is a callback facility. NotifyHub is used by the informer to notify clients about arbitrary events in the informer. NotifyHub contains notification sets (NotifySet).
NotifySet is identified by set ID and it can include one or many notifications (Notify). Client defines the action performed at notification (callback). Informer activates the notification when notification events occur.
Notifications can be enabled/disabled on different levels (NotifyHub, NotifySet, and Notify).
Example:
require 'notifyhub'
# Create class that includes interesting events.
class Storage
# Handle to NotifyHub.
attr_accessor :hub
def initialize
# Create NotifyHub.
@hub = NotifyHub.declare( :store, :load, :na )
end
# Store data and notify clients.
def store( data )
@data = data
@hub[ :store ].notify( @data )
end
# Load data and notify clients.
def load
@hub[ :load ].notify( @data )
@data
end
end
storage = Storage.new
# Setup notify action for store.
storage.hub[ :store ].action do |data|
puts "store: #{data}"
end
# Setup notify action for load.
storage.hub[ :load ].action do |data|
puts "load: #{data}"
end
# Use storage and get notifications.
storage.store( "my data" )
data = storage.load
Defined Under Namespace
Classes: NotFound, Redefining
Instance Attribute Summary collapse
-
#autodeclare ⇒ Object
Declare sets automatically at action registration.
Class Method Summary collapse
-
.auto(*id) ⇒ Object
Create NotifyHub and with autodeclare for sets.
-
.create { ... } ⇒ Object
Create NotifyHub and run block with it.
-
.declare(*id) ⇒ Object
Create NotifyHub and declare sets.
Instance Method Summary collapse
-
#[](id) ⇒ NotifySet
Get NotifySet by ID.
-
#action(id, &action) ⇒ Object
(also: #register, #with)
Register action to NotifySet.
-
#declare(*id) ⇒ Object
Declare Notify by set.
-
#enable(id, value) ⇒ Object
Enable/disable Notify set or all if not set.
-
#initialize(*id) ⇒ NotifyHub
constructor
Instantiation.
-
#notify(id, *args) ⇒ Object
Run all notifiers in Notify set.
-
#remove(id, notify = nil) ⇒ Object
Remove all or one Notify.
Constructor Details
#initialize(*id) ⇒ NotifyHub
Instantiation.
107 108 109 110 111 |
# File 'lib/notifyhub.rb', line 107 def initialize( *id ) @autodeclare = false @set = {} declare( *id ) end |
Instance Attribute Details
#autodeclare ⇒ Object
Declare sets automatically at action registration.
63 64 65 |
# File 'lib/notifyhub.rb', line 63 def autodeclare @autodeclare end |
Class Method Details
.auto(*id) ⇒ Object
Create NotifyHub and with autodeclare for sets. Declare sets automatically at action registration.
85 86 87 88 89 |
# File 'lib/notifyhub.rb', line 85 def NotifyHub.auto( *id ) n = NotifyHub.new( *id ) n.autodeclare = true n end |
.create { ... } ⇒ Object
Create NotifyHub and run block with it.
95 96 97 98 99 100 101 |
# File 'lib/notifyhub.rb', line 95 def NotifyHub.create( &blk ) cg = NotifyHub.new if block_given? cg.instance_eval( &blk ) end cg end |
.declare(*id) ⇒ Object
Create NotifyHub and declare sets.
76 77 78 |
# File 'lib/notifyhub.rb', line 76 def NotifyHub.declare( *id ) NotifyHub.new( *id ) end |
Instance Method Details
#[](id) ⇒ NotifySet
Get NotifySet by ID.
186 187 188 189 190 |
# File 'lib/notifyhub.rb', line 186 def []( id ) useSet( id ) do |set| set end end |
#action(id, &action) ⇒ Object Also known as: register, with
Register action to NotifySet. Multiple notifies can exist per set.
133 134 135 136 137 |
# File 'lib/notifyhub.rb', line 133 def action( id, &action ) useSet( id ) do |set| set.action( &action ) end end |
#declare(*id) ⇒ Object
Declare Notify by set. Multiple notifiers can exist per set.
117 118 119 120 121 122 123 124 125 |
# File 'lib/notifyhub.rb', line 117 def declare( *id ) id.each do |i| if @set[ i ] raise Redefining, "Notify set already declared: #{i.to_s}" else @set[ i ] = NotifySet.new( i ) end end end |
#enable(id, value) ⇒ Object
Enable/disable Notify set or all if not set.
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/notifyhub.rb', line 158 def enable( id, value ) if id withSet( id ) do |set| set.enable = value end else @set.each_value do |set| set.enable = value end end end |
#notify(id, *args) ⇒ Object
Run all notifiers in Notify set.
175 176 177 178 179 |
# File 'lib/notifyhub.rb', line 175 def notify( id, *args ) withSet( id ) do |set| set.notify( *args ) end end |
#remove(id, notify = nil) ⇒ Object
Remove all or one Notify.
147 148 149 150 151 |
# File 'lib/notifyhub.rb', line 147 def remove( id, notify = nil ) withSet( id ) do |set| set.remove( notify ) end end |