Class: PureMVC::Observer

Inherits:
Object
  • Object
show all
Defined in:
src/patterns/observer/observer.rb

Overview

A base IObserver implementation.

An Observer is an object that encapsulates information about an interested object with a method that should be called when a particular INotification is broadcast.

In PureMVC, the Observer class assumes these responsibilities:

  • Encapsulate the notification (callback) method of the interested object.

  • Encapsulate the notification context (self) of the interested object.

  • Provide methods for setting the notification method and context.

  • Provide a method for notifying the interested object.

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(notify = nil, context = nil) ⇒ Observer

Initialize an Observer with a notify method and context.

Parameters:

  • notify (Method, nil) (defaults to: nil)

    the callback method to invoke on notification.

  • context (Object, nil) (defaults to: nil)

    the object context for the callback.



35
36
37
38
# File 'src/patterns/observer/observer.rb', line 35

def initialize(notify = nil, context = nil)
  @notify = notify
  @context = context
end

Instance Attribute Details

#contextObject | nil

Returns context The object context for the callback.

Returns:

  • (Object | nil)

    context The object context for the callback.



29
30
31
# File 'src/patterns/observer/observer.rb', line 29

def context
  @context
end

#notifyMethod | nil

Returns notify The callback method to be called on notification.

Returns:

  • (Method | nil)

    notify The callback method to be called on notification.



26
27
28
# File 'src/patterns/observer/observer.rb', line 26

def notify
  @notify
end

Instance Method Details

#compare_notify_context?(object) ⇒ Boolean

Compares the given object with the Observer’s context.

Parameters:

  • object (Object)

    the object to compare.

Returns:

  • (Boolean)

    true if the given object is the same as the context.



51
52
53
# File 'src/patterns/observer/observer.rb', line 51

def compare_notify_context?(object)
  object.equal?(@context)
end

#notify_observer(notification) ⇒ Object

Calls the notify method with the given notification.

Parameters:

  • notification (INotification)

    the notification to send.



43
44
45
# File 'src/patterns/observer/observer.rb', line 43

def notify_observer(notification)
  @notify&.call(notification)
end