Class: Arborist::Observer

Inherits:
Object
  • Object
show all
Extended by:
MethodUtilities, Loggability
Defined in:
lib/arborist/observer.rb

Overview

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

Defined Under Namespace

Classes: Action, Summarize

Constant Summary collapse

LOADED_INSTANCE_KEY =

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

:loaded_observer_instances
OBSERVER_FILE_PATTERN =

The glob pattern to use for searching for observers

'**/*.rb'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MethodUtilities

attr_predicate, attr_predicate_accessor, dsl_accessor, singleton_attr_accessor, singleton_attr_reader, singleton_attr_writer, singleton_method_alias, singleton_predicate_accessor, singleton_predicate_reader

Constructor Details

#initialize(description, &block) ⇒ Observer

Create a new Observer with the specified description.



73
74
75
76
77
78
79
# File 'lib/arborist/observer.rb', line 73

def initialize( description, &block )
	@description = description
	@subscriptions = []
	@actions = []

	self.instance_exec( &block ) if block
end

Instance Attribute Details

#actionsObject (readonly)

The observer’s actions



92
93
94
# File 'lib/arborist/observer.rb', line 92

def actions
  @actions
end

#descriptionObject (readonly)

The observer’s description



88
89
90
# File 'lib/arborist/observer.rb', line 88

def description
  @description
end

#sourceObject

The source file the observer was loaded from



96
97
98
# File 'lib/arborist/observer.rb', line 96

def source
  @source
end

Class Method Details

.add_loaded_instance(new_instance) ⇒ Object

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



48
49
50
51
# File 'lib/arborist/observer.rb', line 48

def self::add_loaded_instance( new_instance )
	instances = Thread.current[ LOADED_INSTANCE_KEY ] or return
	instances << new_instance
end

.each_in(loader) ⇒ Object

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



66
67
68
# File 'lib/arborist/observer.rb', line 66

def self::each_in( loader )
	return loader.observers
end

.load(file) ⇒ Object

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



55
56
57
58
59
60
61
62
# File 'lib/arborist/observer.rb', line 55

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

.newObject

Overridden to track instances of created nodes for the DSL.



39
40
41
42
43
# File 'lib/arborist/observer.rb', line 39

def self::new( * )
	new_instance = super
	Arborist::Observer.add_loaded_instance( new_instance )
	return new_instance
end

Instance Method Details

#action(**options, &block) ⇒ Object

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



117
118
119
# File 'lib/arborist/observer.rb', line 117

def action( **options, &block )
	@actions << Arborist::Observer::Action.new( **options, &block )
end

#clientObject

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



130
131
132
# File 'lib/arborist/observer.rb', line 130

def client
	return Arborist::Client.instance
end

#handle_event(uuid, event) ⇒ Object

Handle a published event.



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

def handle_event( uuid, event )
	self.actions.each do |action|
		action.handle_event( event )
	end
end

#subscribe(to: nil, where: {}, exclude: {}, on: nil) ⇒ Object

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.


111
112
113
# File 'lib/arborist/observer.rb', line 111

def subscribe( to: nil, where: {}, exclude: {}, on: nil )
	@subscriptions << { criteria: where, exclude: exclude, identifier: on, event_type: to }
end

#subscriptionsObject

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.



141
142
143
144
145
146
147
# File 'lib/arborist/observer.rb', line 141

def subscriptions

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

	return @subscriptions
end

#summarize(**options, &block) ⇒ Object

Register a summary action.



123
124
125
# File 'lib/arborist/observer.rb', line 123

def summarize( **options, &block )
	@actions << Arborist::Observer::Summarize.new( **options, &block )
end

#timersObject

Return an Array of timer callbacks of the form:

[ interval_seconds, callable ]


162
163
164
165
166
167
168
# File 'lib/arborist/observer.rb', line 162

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