Subscription

class
Superclass
Object
Included Modules
Arborist::HashUtilities
Extended With
Loggability

An observer subscription to node events.

Attributes

callback[R]

The callable that should be called when the subscription receives a matching event

criteria[R]

Node selection attributes to require

event_type[R]

The Arborist event pattern that this subscription handles.

id[R]

A unique identifier for this subscription request.

negative_criteria[R]

Node selection attributes to exclude

Public Class Methods

anchor
new( event_type=nil, criteria={}, negative_criteria={}, &callback )

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

anchor
check_callback()

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
anchor
exclude( criteria )

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
anchor
generate_id()

Create an identifier for this subscription object.

# File lib/arborist/subscription.rb, line 93
def generate_id
        return SecureRandom.uuid
end
anchor
inspect()

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
anchor
interested_in?( event )

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
Also aliased as: is_interested_in?
anchor
is_interested_in?( event )
Alias for: interested_in?
anchor
on_events( *events )

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