Class: Arborist::Subscription
- Inherits:
-
Object
- Object
- Arborist::Subscription
- Extended by:
- Loggability
- Includes:
- HashUtilities
- Defined in:
- lib/arborist/subscription.rb
Overview
An observer subscription to node events.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#callback ⇒ Object
readonly
The callable that should be called when the subscription receives a matching event.
-
#criteria ⇒ Object
readonly
Node selection attributes to require.
-
#event_type ⇒ Object
readonly
The Arborist event pattern that this subscription handles.
-
#id ⇒ Object
readonly
A unique identifier for this subscription request.
-
#negative_criteria ⇒ Object
readonly
Node selection attributes to exclude.
Instance Method Summary collapse
-
#check_callback ⇒ Object
Check to make sure the subscription will function as it’s set up.
-
#exclude(criteria) ⇒ Object
Add the given
criteriahash to the #negative_criteria. -
#generate_id ⇒ Object
Create an identifier for this subscription object.
-
#initialize(event_type = nil, criteria = {}, negative_criteria = {}, &callback) ⇒ Subscription
constructor
Instantiate a new Subscription object given an
eventpattern and eventcriteria. -
#inspect ⇒ Object
Return a String representation of the object suitable for debugging.
-
#interested_in?(event) ⇒ Boolean
(also: #is_interested_in?)
Returns
trueif the receiver is interested in publishing the specifiedevent. -
#on_events(*events) ⇒ Object
Publish any of the specified
eventswhich match the subscription.
Methods included from HashUtilities
compact_hash, hash_matches, merge_recursively, stringify_keys, symbolify_keys
Constructor Details
#initialize(event_type = nil, criteria = {}, negative_criteria = {}, &callback) ⇒ Subscription
Instantiate a new Subscription object given an event pattern and event criteria.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/arborist/subscription.rb', line 23 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 |
Instance Attribute Details
#callback ⇒ Object (readonly)
The callable that should be called when the subscription receives a matching event
40 41 42 |
# File 'lib/arborist/subscription.rb', line 40 def callback @callback end |
#criteria ⇒ Object (readonly)
Node selection attributes to require
49 50 51 |
# File 'lib/arborist/subscription.rb', line 49 def criteria @criteria end |
#event_type ⇒ Object (readonly)
The Arborist event pattern that this subscription handles.
46 47 48 |
# File 'lib/arborist/subscription.rb', line 46 def event_type @event_type end |
#id ⇒ Object (readonly)
A unique identifier for this subscription request.
43 44 45 |
# File 'lib/arborist/subscription.rb', line 43 def id @id end |
#negative_criteria ⇒ Object (readonly)
Node selection attributes to exclude
52 53 54 |
# File 'lib/arborist/subscription.rb', line 52 def negative_criteria @negative_criteria end |
Instance Method Details
#check_callback ⇒ Object
Check to make sure the subscription will function as it’s set up.
63 64 65 |
# File 'lib/arborist/subscription.rb', line 63 def check_callback raise LocalJumpError, "requires a callback block" unless self.callback end |
#exclude(criteria) ⇒ Object
Add the given criteria hash to the #negative_criteria.
56 57 58 59 |
# File 'lib/arborist/subscription.rb', line 56 def exclude( criteria ) criteria = stringify_keys( criteria ) self.negative_criteria.merge!( criteria ) end |
#generate_id ⇒ Object
Create an identifier for this subscription object.
94 95 96 |
# File 'lib/arborist/subscription.rb', line 94 def generate_id return SecureRandom.uuid end |
#inspect ⇒ Object
Return a String representation of the object suitable for debugging.
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/arborist/subscription.rb', line 80 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 |
#interested_in?(event) ⇒ Boolean Also known as: is_interested_in?
Returns true if the receiver is interested in publishing the specified event.
100 101 102 103 104 105 106 |
# File 'lib/arborist/subscription.rb', line 100 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 |
#on_events(*events) ⇒ Object
Publish any of the specified events which match the subscription.
69 70 71 72 73 74 75 76 |
# File 'lib/arborist/subscription.rb', line 69 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 |