Class: NotifyHub

Inherits:
Object
  • Object
show all
Defined in:
lib/notifyhub.rb,
lib/version.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).

Usage example:

require 'notifyhub'

# Create class that includes interesting events.
class Storage

    # Handle to NotifyHub.
    attr_accessor :hub

    def initialize
        # Create NotifyHub with 3 callbacks.
        @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

Produces:

store: my data
load:  my data

Defined Under Namespace

Classes: NotFound, Redefining

Constant Summary

VERSION =
"0.0.4"

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (NotifyHub) initialize(*id)

Instantiation.

Parameters:

  • id (Array<Symbol>)

    Notify set id(s).



111
112
113
114
115
# File 'lib/notifyhub.rb', line 111

def initialize( *id )
    @autodeclare = false
    @set = {}
    declare( *id )
end

Instance Attribute Details

- (Object) autodeclare

Declare sets automatically at action registration.



67
68
69
# File 'lib/notifyhub.rb', line 67

def autodeclare
  @autodeclare
end

Class Method Details

+ (Object) auto(*id)

Create NotifyHub and with autodeclare for sets. Declare sets automatically at action registration.

Parameters:

  • id (Array<Symbol>)

    Notify set(s).



89
90
91
92
93
# File 'lib/notifyhub.rb', line 89

def NotifyHub.auto( *id )
    n = NotifyHub.new( *id )
    n.autodeclare = true
    n
end

+ (Object) create { ... }

Create NotifyHub and run block with it.

Yields:

  • Block to run with NotifyHub.



99
100
101
102
103
104
105
# File 'lib/notifyhub.rb', line 99

def NotifyHub.create( &blk )
    cg = NotifyHub.new
    if block_given?
        cg.instance_eval( &blk )
    end
    cg
end

+ (Object) declare(*id)

Create NotifyHub and declare sets.

Parameters:

  • id (Array<Symbol>)

    Notify set(s).



80
81
82
# File 'lib/notifyhub.rb', line 80

def NotifyHub.declare( *id )
    NotifyHub.new( *id )
end

+ (Object) version



3
4
5
# File 'lib/version.rb', line 3

def NotifyHub.version
    NotifyHub::VERSION
end

Instance Method Details

- (NotifySet) [](id)

Get NotifySet by ID.

Parameters:

  • id (Symbol)

    Set ID.

Returns:



190
191
192
193
194
# File 'lib/notifyhub.rb', line 190

def []( id )
    useSet( id ) do |set|
        set
    end
end

- (Object) action(id, &action) Also known as: register, with

Register action to NotifySet. Multiple notifies can exist per set.

Parameters:

  • id (Symbol)

    Notify set id (class).

  • action (Block)

    Notify action.



137
138
139
140
141
# File 'lib/notifyhub.rb', line 137

def action( id, &action )
    useSet( id ) do |set|
        set.action( &action )
    end
end

- (Object) declare(*id)

Declare Notify by set. Multiple notifiers can exist per set.

Parameters:

  • id (Array<Symbol>)

    Notify set id(s).



121
122
123
124
125
126
127
128
129
# File 'lib/notifyhub.rb', line 121

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

- (Object) enable(id, value)

Enable/disable Notify set or all if not set.

Parameters:

  • id (Symbol)

    Notify set id (all if nil given).

  • value (Boolean)

    Enable with true and disable with false.



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/notifyhub.rb', line 162

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

- (Array<Symbol>) ids

Get list of NotifySet IDs.

Returns:

  • (Array<Symbol>)

    NotifySet IDs.



200
201
202
# File 'lib/notifyhub.rb', line 200

def ids
    @set.keys
end

- (Object) notify(id, *args)

Run all notifiers in Notify set.

Parameters:

  • id (Symbol)

    Notify set id (class).

  • args (Array<Object>)

    Arguments for notifiers.



179
180
181
182
183
# File 'lib/notifyhub.rb', line 179

def notify( id, *args )
    withSet( id ) do |set|
        set.notify( *args )
    end
end

- (Object) remove(id, notify = nil)

Remove all or one Notify.

Parameters:

  • id (Symbol)

    Notify set id (class).

  • notify (Notify) (defaults to: nil)

    Notify to remove (all if not given).



151
152
153
154
155
# File 'lib/notifyhub.rb', line 151

def remove( id, notify = nil )
    withSet( id ) do |set|
        set.remove( notify )
    end
end