class Arborist::Subscription
An observer subscription to node events.
Attributes
The callable that should be called when the subscription receives a matching event
Node selection attributes to require
The Arborist
event pattern that this subscription handles.
A unique identifier for this subscription request.
Node selection attributes to exclude
Public Class Methods
Instantiate a new Subscription
object given an event
pattern and event criteria
.
# File lib/arborist/subscription.rb, line 22 def initialize( event_type=nil, criteria={}, negative_criteria={}, &callback ) @callback = callback @event_type = event_type @criteria = stringify_keys( criteria ) @negative_criteria = stringify_keys( negative_criteria ) self.check_callback @id = self.generate_id end
Public Instance Methods
Check to make sure the subscription will function as it's set up.
# File lib/arborist/subscription.rb, line 62 def check_callback raise LocalJumpError, "requires a callback block" unless self.callback end
Add the given criteria
hash to the negative_criteria
.
# File lib/arborist/subscription.rb, line 55 def exclude( criteria ) criteria = stringify_keys( criteria ) self.negative_criteria.merge!( criteria ) end
Create an identifier for this subscription object.
# File lib/arborist/subscription.rb, line 93 def generate_id return SecureRandom.uuid end
Return a String representation of the object suitable for debugging.
# File lib/arborist/subscription.rb, line 79 def inspect return "#<%p:%#x [%s] for %s events matching: %p %s-> %p>" % [ self.class, self.object_id * 2, self.id, self.event_type, self.criteria, self.negative_criteria.empty? ? '' : "(but not #{self.negative_criteria.inspect}", self.callback, ] end
Returns true
if the receiver is interested in publishing the specified event
.
# File lib/arborist/subscription.rb, line 99 def interested_in?( event ) self.log.debug "Testing %p against type = %p and criteria = %p but not %p" % [ event, self.event_type, self.criteria, self.negative_criteria ] rval = event.match( self ) self.log.debug " event %s match." % [ rval ? "did" : "did NOT" ] return rval end
Publish any of the specified events
which match the subscription.
# File lib/arborist/subscription.rb, line 68 def on_events( *events ) events.flatten.each do |event| if self.interested_in?( event ) self.log.debug "Calling %p for a %s event" % [ self.callback, event.type ] self.callback.call( self.id, event ) end end end