Module: Hallon::Observable::ClassMethods

Defined in:
lib/hallon/observable.rb

Overview

This module is responsible for creating methods for registering callbacks. It expects a certain protocol to already available.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#callbacksMethod, Struct (readonly)

Returns callbacks to attach to this object.

Returns:

  • (Method, Struct)

    callbacks to attach to this object



18
19
20
# File 'lib/hallon/observable.rb', line 18

def callbacks
  @callbacks
end

Class Method Details

.extended(other) ⇒ Object

When extended it’ll call #initialize_observable to set-up other.



13
14
15
# File 'lib/hallon/observable.rb', line 13

def self.extended(other)
  other.send(:initialize_observable)
end

Instance Method Details

#callback_for(name) ⇒ Method (protected)

Parameters:

  • name (#to_s)

Returns:

  • (Method)


41
42
43
# File 'lib/hallon/observable.rb', line 41

def callback_for(name)
  method("#{name}_callback")
end

#initialize_observableObject (protected)

Run when ClassMethods are extended.

It sets up the callbacks and all book-keeping required to keep track of all subscribers properly.



34
35
36
37
# File 'lib/hallon/observable.rb', line 34

def initialize_observable
  @callbacks = initialize_callbacks
  @observers = WeakObservable::Hub.new
end

#subscribe(object, pointer) ⇒ Object

Subscribe to callbacks for a given pointer.

Parameters:



24
25
26
# File 'lib/hallon/observable.rb', line 24

def subscribe(object, pointer)
  @observers.add(pointer.address, object, :trigger)
end

#trigger(pointer, event, *arguments) ⇒ Object (protected)

Scans through the list of subscribers, trying to find any subscriber attached to this pointer. For each subscriber, trigger the appropriate event.

Parameters:

Returns:

  • whatever the (last) handler returned



53
54
55
56
57
# File 'lib/hallon/observable.rb', line 53

def trigger(pointer, event, *arguments)
  if results = @observers.notify(pointer.address, event, *arguments)
    results[-1]
  end
end