Skip to content

Commit 3bfc6a4

Browse files
author
stoffie
committed
Use same args signatures for PO#on_signal and POI#on_signal
Fixes issue mvidner#31; ProxyObjectInterface#on_signal shares now the arguments signature whit ProxyObject#on_signal and can be used without specifying explicitly the bus parameter, since the POI itself already owns the reference of the bus is being used on.
1 parent c08f0a8 commit 3bfc6a4

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

lib/dbus/introspect.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,24 @@ def define_method(methodname, prototype)
299299
define(m)
300300
end
301301

302+
# @overload on_signal(name, &block)
303+
# @overload on_signal(bus, name, &block)
302304
# Registers a handler (code block) for a signal with _name_ arriving
303305
# over the given _bus_. If no block is given, the signal is unregistered.
304-
def on_signal(bus, name, &block)
306+
# Note that specifying _bus_ is discouraged and the option is kept only for
307+
# backward compatibility.
308+
def on_signal(*args, &block)
309+
# Since we must function under ruby 1.8.7, it isn't possible to define the
310+
# function as on_signal(bus = nil, name, &block)
311+
bus = case args.size
312+
when 1
313+
@object.bus
314+
when 2
315+
args.shift
316+
else
317+
raise ArgumentError, "wrong number of arguments (#{args.size} for 1-2)"
318+
end
319+
name = args.shift
305320
mr = DBus::MatchRule.new.from_signal(self, name)
306321
if block.nil?
307322
bus.remove_match(mr)
@@ -435,7 +450,7 @@ def has_iface?(name)
435450
# It uses _default_iface_ which must have been set.
436451
def on_signal(name, &block)
437452
if @default_iface and has_iface?(@default_iface)
438-
@interfaces[@default_iface].on_signal(@bus, name, &block)
453+
@interfaces[@default_iface].on_signal(name, &block)
439454
else
440455
# TODO improve
441456
raise NoMethodError

test/signal_test.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ def setup
1010
@obj = svc.object("/org/ruby/MyInstance")
1111
@obj.introspect # necessary
1212
@obj.default_iface = "org.ruby.Loop"
13+
@intf = @obj["org.ruby.Loop"]
1314

1415
@loop = DBus::Main.new
1516
@loop << @session_bus
1617
end
1718

1819
# testing for commit 017c83 (kkaempf)
1920
def test_overriding_a_handler
21+
DBus.logger.debug "Inside test_overriding_a_handler"
2022
counter = 0
2123

2224
@obj.on_signal "LongTaskEnd" do
@@ -47,6 +49,31 @@ def test_overriding_a_handler
4749
assert_equal 1, counter
4850
end
4951

52+
def test_on_signal_backward_compatibility
53+
DBus.logger.debug "Inside test_on_signal_backward_compatibility"
54+
counter = 0
55+
# Same as intf.on_signal("LongTaskEnd"), just the old way
56+
@intf.on_signal @obj.bus, "LongTaskEnd" do
57+
counter += 1
58+
end
59+
@obj.LongTaskBegin 3
60+
quitter = Thread.new do
61+
DBus.logger.debug "sleep before quit"
62+
sleep 1
63+
DBus.logger.debug "will quit"
64+
@loop.quit
65+
end
66+
@loop.run
67+
quitter.join
68+
69+
assert_equal 1, counter
70+
end
71+
72+
def test_on_signal_args_check
73+
assert_raise(ArgumentError) {@intf.on_signal } # not enough
74+
assert_raise(ArgumentError) {@intf.on_signal 'to', 'many', 'yarrrrr!'}
75+
end
76+
5077
def test_too_many_rules
5178
100.times do
5279
@obj.on_signal "Whichever" do

0 commit comments

Comments
 (0)