The Arborist entity responsible for observing changes to the tree and reporting on them.
The key for the thread local that is used to track instances as they're loaded.
The glob pattern to use for searching for observers
The observer's actions
The observer's description
The source file the observer was loaded from
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
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
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
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
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
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
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
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
Specify a pattern for events the observer is interested in. Options:
the name of the event; defaults to every event type
a Hash of criteria to match against event data
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
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
Register a summary action.
# File lib/arborist/observer.rb, line 122
def summarize( options={}, &block )
@actions << Arborist::Observer::Summarize.new( options, &block )
end
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