Class: QML::Signal
- Inherits:
-
Object
- Object
- QML::Signal
- Defined in:
- lib/qml/signal.rb
Defined Under Namespace
Classes: Connection
Instance Attribute Summary collapse
-
#arity ⇒ Object
readonly
Returns the value of attribute arity.
Instance Method Summary collapse
-
#connect { ... } ⇒ QML::Signal::Connection
Connects a procedure.
-
#connection_count ⇒ Integer
Returns the number of connections.
-
#disconnect(listener) ⇒ self
Disconnects a procedure.
-
#emit(*args) ⇒ Object
Calls every connected procedure with given arguments.
-
#initialize(params) ⇒ Signal
constructor
Initializes the Signal.
-
#parameters ⇒ Array<Array<Symbol>>
Returns the format of the parameters in the same format as Proc#parameters.
Constructor Details
#initialize(params) ⇒ Signal
Initializes the Signal.
21 22 23 24 25 |
# File 'lib/qml/signal.rb', line 21 def initialize(params) @listeners = [] @params = params.map(&:to_sym) @arity = params.size end |
Instance Attribute Details
#arity ⇒ Object (readonly)
Returns the value of attribute arity.
17 18 19 |
# File 'lib/qml/signal.rb', line 17 def arity @arity end |
Instance Method Details
#connect { ... } ⇒ QML::Signal::Connection
Connects a procedure.
54 55 56 57 |
# File 'lib/qml/signal.rb', line 54 def connect(&listener) @listeners << listener Connection.new(self, listener) end |
#connection_count ⇒ Integer
Returns the number of connections.
47 48 49 |
# File 'lib/qml/signal.rb', line 47 def connection_count @listeners.size end |
#disconnect(listener) ⇒ self
Disconnects a procedure.
62 63 64 65 |
# File 'lib/qml/signal.rb', line 62 def disconnect(listener) @listeners.delete(listener) self end |
#emit(*args) ⇒ Object
Calls every connected procedure with given arguments. Raises an ArgumentError when the arity is wrong.
30 31 32 33 34 35 36 37 |
# File 'lib/qml/signal.rb', line 30 def emit(*args) if args.size != @arity fail ::ArgumentError ,"wrong number of arguments for signal (#{args.size} for #{@arity})" end @listeners.each do |listener| listener.call(*args) end end |