class NotifyHub

{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

Constants

VERSION

Attributes

autodeclare[RW]

Declare sets automatically at action registration.

Public Class Methods

auto( *id ) click to toggle source

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

@param id [Array<Symbol>] Notify set(s).

# File lib/notifyhub.rb, line 89
def NotifyHub.auto( *id )
    n = NotifyHub.new( *id )
    n.autodeclare = true
    n
end
create( &blk ) click to toggle source

Create {NotifyHub} and run block with it.

@yield Block to run with NotifyHub.

# File lib/notifyhub.rb, line 99
def NotifyHub.create( &blk )
    cg = NotifyHub.new
    if block_given?
        cg.instance_eval( &blk )
    end
    cg
end
declare( *id ) click to toggle source

Create {NotifyHub} and declare sets.

@param id [Array<Symbol>] Notify set(s).

# File lib/notifyhub.rb, line 80
def NotifyHub.declare( *id )
    NotifyHub.new( *id )
end
new( *id ) click to toggle source

Instantiation.

@param id [Array<Symbol>] Notify set id(s).

# File lib/notifyhub.rb, line 111
def initialize( *id )
    @autodeclare = false
    @set = {}
    declare( *id )
end
version() click to toggle source
# File lib/version.rb, line 3
def NotifyHub.version
    NotifyHub::VERSION
end

Public Instance Methods

[]( id ) click to toggle source

Get {NotifySet} by ID.

@param id [Symbol] Set ID. @return [NotifySet] Set.

# File lib/notifyhub.rb, line 190
def []( id )
    useSet( id ) do |set|
        set
    end
end
action( id, &action ) click to toggle source

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

@param id [Symbol] Notify set id (class). @param action [Block] Notify action.

# File lib/notifyhub.rb, line 137
def action( id, &action )
    useSet( id ) do |set|
        set.action( &action )
    end
end
Also aliased as: register, with
declare( *id ) click to toggle source

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

@param id [Array<Symbol>] Notify set id(s).

# 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
enable( id, value ) click to toggle source

Enable/disable {Notify} set or all if not set.

@param id [Symbol] Notify set id (all if nil given). @param value [Boolean] Enable with true and disable with false.

# 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
ids() click to toggle source

Get list of {NotifySet} IDs.

@return [Array<Symbol>] NotifySet IDs.

# File lib/notifyhub.rb, line 200
def ids
    @set.keys
end
notify( id, *args ) click to toggle source

Run all notifiers in {Notify} set.

@param id [Symbol] Notify set id (class). @param args [Array<Object>] Arguments for notifiers.

# File lib/notifyhub.rb, line 179
def notify( id, *args )
    withSet( id ) do |set|
        set.notify( *args )
    end
end
register( id, &action )
Alias for: action
remove( id, notify = nil ) click to toggle source

Remove all or one Notify.

@param id [Symbol] Notify set id (class). @param notify [Notify] Notify to remove (all if not given).

# File lib/notifyhub.rb, line 151
def remove( id, notify = nil )
    withSet( id ) do |set|
        set.remove( notify )
    end
end
with( id, &action )
Alias for: action

Private Instance Methods

useSet( id ) { |set| ... } click to toggle source

Check that {NotifySet} exists (or create it).

# File lib/notifyhub.rb, line 219
def useSet( id, &blk )

    if @set[id] || @autodeclare

        unless @set[id]
            declare( id )
        end

        yield @set[ id ]

    else

        raise NotFound, "Uknown notify set: #{id.to_s}"

    end
end
withSet( id ) { |set| ... } click to toggle source

Check that {NotifySet} exists (or create it).

# File lib/notifyhub.rb, line 209
def withSet( id, &blk )
    if @set[id]
        yield @set[ id ]
    else
        raise NotFound, "Uknown notify set: #{id.to_s}"
    end
end