Module: Kamaze::Project::Concern::Observable::ClassMethods

Defined in:
lib/kamaze/project/concern/observable.rb

Overview

Class methods

Instance Method Summary collapse

Instance Method Details

#add_observer(observer_class, func = :handle_event) ⇒ self

Add observer.

Parameters:

  • observer_class (Class)

Returns:

  • (self)


59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/kamaze/project/concern/observable.rb', line 59

def add_observer(observer_class, func = :handle_event)
  func = func.to_sym

  unless observer_class.instance_methods.include?(func)
    m = "#<#{observer_class}> does not respond to `#{func}'"
    raise NoMethodError, m
  end

  observer_peers[observer_class] = func

  self
end

#delete_observer(observer_class) ⇒ self

Remove observer, so that it will no longer receive notifications.

Parameters:

  • observer_class (Class)

Returns:

  • (self)


77
78
79
80
81
# File 'lib/kamaze/project/concern/observable.rb', line 77

def delete_observer(observer_class)
  self.tap do
    observer_peers.delete(observer_class)
  end
end

#delete_observersself

Remove all observers.

Returns:

  • (self)


86
87
88
# File 'lib/kamaze/project/concern/observable.rb', line 86

def delete_observers
  self.tap { observers.clear }
end

#observer_peersHash (protected)

Returns:

  • (Hash)


93
94
95
# File 'lib/kamaze/project/concern/observable.rb', line 93

def observer_peers
  @observer_peers ||= {}
end