Class: Arborist::Subscription

Inherits:
Object
  • Object
show all
Extended by:
Loggability
Includes:
HashUtilities
Defined in:
lib/arborist/subscription.rb

Overview

An observer subscription to node events.

Direct Known Subclasses

NodeSubscription

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#callbackObject (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

#criteriaObject (readonly)

Node selection attributes to require



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

def criteria
  @criteria
end

#event_typeObject (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

#idObject (readonly)

A unique identifier for this subscription request.



43
44
45
# File 'lib/arborist/subscription.rb', line 43

def id
  @id
end

#negative_criteriaObject (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_callbackObject

Check to make sure the subscription will function as it’s set up.

Raises:

  • (LocalJumpError)


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_idObject

Create an identifier for this subscription object.



94
95
96
# File 'lib/arborist/subscription.rb', line 94

def generate_id
  return SecureRandom.uuid
end

#inspectObject

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.

Returns:

  • (Boolean)


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