lib/arborist/subscription.rb in arborist-0.0.1.pre20160128152542 vs lib/arborist/subscription.rb in arborist-0.0.1.pre20160606141735
- old
+ new
@@ -18,24 +18,25 @@
log_to :arborist
### Instantiate a new Subscription object given an +event+ pattern
### and event +criteria+.
- def initialize( publisher, event_type=nil, criteria={} )
- @publisher = publisher
+ def initialize( event_type=nil, criteria={}, &callback )
+ raise LocalJumpError, "requires a callback block" unless callback
+ @callback = callback
@event_type = event_type
@criteria = stringify_keys( criteria )
@id = self.generate_id
end
######
public
######
- # The Arborist::Manager::EventPublisher the subscription will use to publish matching events.
- attr_reader :publisher
+ # The callable that should be called when the subscription receives a matching event
+ attr_reader :callback
# A unique identifier for this subscription request.
attr_reader :id
# The Arborist event pattern that this subscription handles.
@@ -52,11 +53,14 @@
### Publish any of the specified +events+ which match the subscription.
def on_events( *events )
events.flatten.each do |event|
- self.publisher.publish( self.id, event ) if self.interested_in?( 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
### Returns +true+ if the receiver is interested in publishing the specified +event+.
@@ -68,15 +72,16 @@
alias_method :is_interested_in?, :interested_in?
### Return a String representation of the object suitable for debugging.
def inspect
- return "#<%p:%#x [%s] for %s events matching: %p>" % [
+ return "#<%p:%#x [%s] for %s events matching: %p -> %p>" % [
self.class,
self.object_id * 2,
self.id,
self.event_type,
self.criteria,
+ self.callback,
]
end
end # class Arborist::Subscription