Class: Observatory::Event

Inherits:
Hash
  • Object
show all
Defined in:
lib/observatory/event.rb

Overview

Note:

An event is essentially a Hash with some extra properties, so you can use all the regular Hash and Enumerable methods to your liking.

An Event is a value object that the observable passes along to the observers. It gives observers easy access to the original observable and any additional values it may have wanted to pass along (the parameters).

The event object is also returned back to the observable that originally issued it, so it also works as a value object to pass information from the observers to the observable. The event knows whether it has been acted upon, and it can remember a return value that the observable may want to use (and other observers may want to act upon).

The event works just like a regular Ruby Hash, so you can access any parameters just like you would with a hash.

Examples:

Accessing parameters like a Hash

event = Event.new(self, 'post.publish', :title => 'My new post')
event[:title] # => 'My new post'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(observable, signal, parameters = {}) ⇒ Event

Create a new event instance with the given observable and signal. Any parameters are stored, which you can later access like a hash.

Parameters:

  • observable (Object)
  • signal (String, #to_s)
  • parameters (Hash) (defaults to: {})

    is a hash of additional information that observers may want to use.



44
45
46
47
48
49
# File 'lib/observatory/event.rb', line 44

def initialize(observable, signal, parameters = {})
  @observable, @signal = observable, signal.to_s
  merge! parameters
  @processed = false
  super()
end

Instance Attribute Details

#observableObject (readonly)

The original observable object that issued the event.

Returns:

  • (Object)


26
27
28
# File 'lib/observatory/event.rb', line 26

def observable
  @observable
end

#return_valueObject

The return value for the observable, that observers may modify.

Returns:

  • (Object)


35
36
37
# File 'lib/observatory/event.rb', line 35

def return_value
  @return_value
end

#signalString (readonly)

The name of the signal that the observable triggered. Namespaced with periods.

Returns:

  • (String)


31
32
33
# File 'lib/observatory/event.rb', line 31

def signal
  @signal
end

Instance Method Details

#process!Boolean

Mark this event as processed, so the observable knows an observer was active when using Dispatcher#notify_until.

Returns:

  • (Boolean)

    true

See Also:



66
67
68
# File 'lib/observatory/event.rb', line 66

def process!
  @processed = true
end

#processed?Boolean

See if this event has been processed by an observer. Useful when using Dispatcher#notify_until to see if there was any observer that actually did something.

Returns:

  • (Boolean)

See Also:



57
58
59
# File 'lib/observatory/event.rb', line 57

def processed?
  @processed
end