Class: QML::Signal

Inherits:
Object
  • Object
show all
Defined in:
lib/qml/signal.rb

Defined Under Namespace

Classes: Connection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Signal

Initializes the Signal.

Parameters:

  • params (Array<#to_sym>, nil)

    the parameter names (the signal will be variadic if nil).



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

#arityObject (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.

Yields:

  • called when #emit is called.

Returns:



54
55
56
57
# File 'lib/qml/signal.rb', line 54

def connect(&listener)
  @listeners << listener
  Connection.new(self, listener)
end

#connection_countInteger

Returns the number of connections.

Returns:

  • (Integer)


47
48
49
# File 'lib/qml/signal.rb', line 47

def connection_count
  @listeners.size
end

#disconnect(listener) ⇒ self

Disconnects a procedure.

Parameters:

  • listener

Returns:

  • (self)


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.

Parameters:

  • args

    the arguments.



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

#parametersArray<Array<Symbol>>

Returns the format of the parameters in the same format as Proc#parameters.

Returns:



41
42
43
# File 'lib/qml/signal.rb', line 41

def parameters
  @params ? @params.map { |arg| [:req, arg] } : [[:rest, :args]]
end