Module: Observatory::Observable

Defined in:
lib/observatory/observable.rb

Overview

Note:

Including this module will create a read-only attribute ‘dispatcher` but not set it. You need to populate it yourself.

An observable object can publish events to registered observers. This module provides some simple helper methods as syntactic sugar.

Using these shortcut methods will default the observable object of the events raised to ‘self`, so the method signatures are the same as in Dispatcher but without the first one, the observable object.

Examples:

Manually triggering events in your code

class Post
  attr_reader :dispatcher

  def initialize(dispatcher)
    @dispatcher = dispatcher
  end

  def publish
    event = Observatory::Event.new(self, 'post.publish')
    dispatcher.notify(event)
  end
end

Using the Observable shortcut methods

class Post
  include Observatory::Observable

  def publish
    notify('post.publish') # => instance of Event
  end
end

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



37
38
39
# File 'lib/observatory/observable.rb', line 37

def self.included(base)
  base.send(:attr_reader, :dispatcher)
end

Instance Method Details

#filter(*args) ⇒ Object

See Also:



49
50
51
52
53
54
# File 'lib/observatory/observable.rb', line 49

def filter(*args)
  value = args.pop
  Observatory::Event.new(self, *args).tap do |e|
    dispatcher.filter(e, value)
  end
end

#notify(*args) ⇒ Object

See Also:



42
43
44
45
46
# File 'lib/observatory/observable.rb', line 42

def notify(*args)
  Observatory::Event.new(self, *args).tap do |e|
    dispatcher.notify(e)
  end
end

#notify_until(*args) ⇒ Object



57
58
59
60
61
# File 'lib/observatory/observable.rb', line 57

def notify_until(*args)
  Observatory::Event.new(self, *args).tap do |e|
    dispatcher.notify_until(e)
  end
end