Skip to content

Commit 382c43f

Browse files
committed
Update conditional argument handling to correctly support lambdas
1 parent bf9e8fc commit 382c43f

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

lib/workflow/event.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ def condition_applicable?(object, event_arguments)
2828
object.send(condition, *event_arguments)
2929
end
3030
else
31-
# since blocks can ignore extra arguments without raising an error in Ruby,
32-
# no `if` is needed - compare with `arity` switch in above methods handling
33-
condition.call(object, *event_arguments)
31+
# Blocks and block-based Procs can ignore extra arguments without raising an error in Ruby,
32+
# but lambdas cannot not, so we still have to check arity
33+
if condition.arity == 1
34+
condition.call(object)
35+
else
36+
condition.call(object, *event_arguments)
37+
end
3438
end
3539
else
3640
true

test/conditionals_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ def check_low_battery?() # supports no arguments, lets test below, what happens
9292
event :turn_on, :transitions_to => :low_battery # otherwise
9393
end
9494
state :on do
95-
event :check, :transitions_to => :low_battery, :if => proc { |obj| return false }
95+
# Use a lambda proc, which enforces correct arity
96+
event :check, :transitions_to => :low_battery, :if => -> (obj) { return false }
9697
event :check, :transitions_to => :on # stay in on state otherwise
9798
end
9899
state :low_battery
@@ -110,6 +111,5 @@ def initialize(battery)
110111
# it still works and just ignores superfluous arguments
111112
assert device.on?
112113
end
113-
114114
end
115115

0 commit comments

Comments
 (0)