Module: GBS::SignalSlot

Defined in:
lib/gbs_signal_slot.rb

Overview

A module providing signal/slot mechanism.


Copyright © 2008 Greenblack Software. Licensed under the MIT License. For more info please read the README or visit www.greenblacksoftware.com


Based on Niklas Frykholm and Nobuyoshi Nakada signal/slot pattern implementations posted to comp.lang.ruby usenet group on 01-02-2002

Defined Under Namespace

Modules: Sender

Instance Method Summary collapse

Instance Method Details

#connect(signal, slot = nil, obj = Object, &pr) ⇒ Object

Connects a signal to a slot. The slot might be either a method referenced as a symbol (often together with a class or an instance) or just an anonymous closure passed as a block.

:call-seq:

sth.connect(signal) { ... }       -> proc
sth.connect(signal, slot)         -> global slot method
sth.connect(signal, slot, obj)    -> obj.slot method

Parameters

signal

symbol defined in a class with the signal or class_signal method

slot

a method name referenced as a symbol object. If ommited the connect will try to bind a block

obj

an instance (for instance method), a class (for class methods) or nothing for global methods

Returns

  • determined procedure (method) object or nil if only the signal parameter was passed (without a block or a slot method name). It is important for disconnecting anonymous proc objects because this is the only way to obtain the reference. See also GBS::SignalSlot#disconnect


Examples

sth.connect(:value_changed, :update_control, self)

if the signal :value_changed is emitted, the self#update_control will be executed

sth.connect(:value_changed, update_status, MyClass)

similar example but with MyClass.update_status class method instead

sth.connect(:value_changed, :global_update)

now we are reffering to a global function (to something that belongs to the Object class in fact as the default value of obj is set to Object)

sth.connect(:value_changed) { |value| puts value }

passing anonymous closure

Raises:

  • (ArgumentError)


56
57
58
59
60
61
# File 'lib/gbs_signal_slot.rb', line 56

def connect(signal, slot = nil, obj = Object, &pr)
  s = slot ? obj.method(slot) : pr
  raise ArgumentError, "No slot provided" unless s
  ((@_gbs_signals ||= {})[signal] ||= []) << s
  s
end

#disconnect(signal, slot = nil, obj = Object, &pr) ⇒ Object

Disconnects a signal from a slot. The slot might be either a method referenced as a symbol together with a class or an instance or just an closure passed as a parameter (with & prefix)

:call-seq:

sth.disconnect(signal)
sth.disconnect(signal, &proc)
sth.disconnect(signal, slot)
sth.disconnect(signal, slot, obj)

Parameters

signal

symbol defined in a class with the signal or class_signal method

slot

a method name referenced as a symbol object. If ommited the connect will try to bind the &proc. If the &proc parameter is also omitted, all connections to the signal will be erased. It disconnects all slots.

obj

an instance (for instance method), a class (for class methods) or nothing for global methods

&proc

a proc object appended with the & prefix. This object can be obtained by the GBS::SignalSlot#connect method with anonymous closure passed.


Examples

sth.disconnect(:value_changed, :update_control, self)

if the signal :value_changed is emitted, the self#update_control will not be executed anymore

sth.disconnect(:value_changed, update_status, MyClass)

similar example but with MyClass.update_status class method instead

sth.disconnect(:value_changed, :global_update)

now we are reffering to a global function (to something that belongs to the Object class in fact as the default value of obj is set to Object)

sth.disconnect(:value_changed, &proc)

the proc object will not be called anymore on :value_changed signal activation.

sth.disconnect(:value_changed)

:value_changed totally disconnected from all slots



104
105
106
107
108
109
110
111
112
# File 'lib/gbs_signal_slot.rb', line 104

def disconnect(signal, slot = nil, obj = Object, &pr)
  s = slot ? obj.method(slot) : pr
  return unless @_gbs_signals and @_gbs_signals[signal]
  if s
    @_gbs_signals[signal].delete(s)
  else
    @_gbs_signals[signal] = []
  end
end