Observer

class
Superclass
Object
Extended With
Loggability
Arborist::MethodUtilities

The Arborist entity responsible for observing changes to the tree and reporting on them.

Constants

LOADED_INSTANCE_KEY

The key for the thread local that is used to track instances as they're loaded.

OBSERVER_FILE_PATTERN

The glob pattern to use for searching for observers

Attributes

actions[R]

The observer's actions

description[R]

The observer's description

source[RW]

The source file the observer was loaded from

Public Class Methods

anchor
add_loaded_instance( new_instance )

Record a new loaded instance if the Thread-local variable is set up to track them.

# File lib/arborist/observer.rb, line 47
def self::add_loaded_instance( new_instance )
        instances = Thread.current[ LOADED_INSTANCE_KEY ] or return
        instances << new_instance
end
anchor
each_in( loader )

Return an iterator for all the observers supplied by the specified loader.

# File lib/arborist/observer.rb, line 65
def self::each_in( loader )
        return loader.observers
end
anchor
load( file )

Load the specified file and return any new Nodes created as a result.

# File lib/arborist/observer.rb, line 54
def self::load( file )
        self.log.info "Loading observer file %s..." % [ file ]
        Thread.current[ LOADED_INSTANCE_KEY ] = []
        Kernel.load( file )
        return Thread.current[ LOADED_INSTANCE_KEY ]
ensure
        Thread.current[ LOADED_INSTANCE_KEY ] = nil
end
anchor
new( * )

Overridden to track instances of created nodes for the DSL.

# File lib/arborist/observer.rb, line 38
def self::new( * )
        new_instance = super
        Arborist::Observer.add_loaded_instance( new_instance )
        return new_instance
end
anchor
new( description, &block )

Create a new Observer with the specified description.

# File lib/arborist/observer.rb, line 72
def initialize( description, &block )
        @description = description
        @subscriptions = []
        @actions = []

        self.instance_exec( &block ) if block
end

Public Instance Methods

anchor
action( options={}, &block )

Register an action that will be taken when a subscribed event is received.

# File lib/arborist/observer.rb, line 116
def action( options={}, &block )
        @actions << Arborist::Observer::Action.new( options, &block )
end
anchor
client()

Return a client singleton for optional observer callbacks to the manager.

# File lib/arborist/observer.rb, line 129
def client
        return Arborist::Client.instance
end
anchor
handle_event( uuid, event )

Handle a published event.

# File lib/arborist/observer.rb, line 150
def handle_event( uuid, event )
        self.actions.each do |action|
                action.handle_event( event )
        end
end
anchor
subscribe( to: nil, where: {}, exclude: {}, on: nil )

Specify a pattern for events the observer is interested in. Options:

to

the name of the event; defaults to every event type

where

a Hash of criteria to match against event data

on

the identifier of the node to subscribe on, defaults to the root node which receives all node events.

# File lib/arborist/observer.rb, line 110
def subscribe( to: nil, where: {}, exclude: {}, on: nil )
        @subscriptions << { criteria: where, exclude: exclude, identifier: on, event_type: to }
end
anchor
subscriptions()

Fetch the descriptions of which events this Observer would like to receive. If no subscriptions have been specified, a subscription that will match any event is returned.

# File lib/arborist/observer.rb, line 140
def subscriptions

        # Subscribe to all events if there are no subscription criteria.
        self.subscribe if @subscriptions.empty?

        return @subscriptions
end
anchor
summarize( options={}, &block )

Register a summary action.

# File lib/arborist/observer.rb, line 122
def summarize( options={}, &block )
        @actions << Arborist::Observer::Summarize.new( options, &block )
end
anchor
timers()

Return an Array of timer callbacks of the form:

[ interval_seconds, callable ]
# File lib/arborist/observer.rb, line 161
def timers
        return self.actions.map do |action|
                next nil unless action.respond_to?( :on_timer ) &&
                        action.time_threshold.nonzero?
                [ action.time_threshold, action.method(:on_timer) ]
        end.compact
end