fix arity difference between Procs and lambdas (see #293 for details)

This commit is contained in:
Thorsten Böttger 2016-01-05 23:51:11 +13:00
parent f74b9a4122
commit 6e97e79419
4 changed files with 27 additions and 1 deletions

View File

@ -1,5 +1,9 @@
# CHANGELOG
## unreleased
* fix arity difference between Procs and lambdas (see [issue #293](https://github.com/aasm/aasm/issues/293) for details)
## 4.5.1
* make sure to use override configuration options if state machine is defined more than once (see [issue #287](https://github.com/aasm/aasm/issues/287) for details)

View File

@ -55,7 +55,7 @@ module AASM::Core
arity = record.send(:method, code.to_sym).arity
arity == 0 ? record.send(code) : record.send(code, *args)
when Proc
code.arity == 0 ? record.instance_exec(&code) : record.instance_exec(*args, &code)
code.parameters.size == 0 ? record.instance_exec(&code) : record.instance_exec(*args, &code)
when Array
if options[:guard]
# invoke guard callbacks

View File

@ -1,6 +1,9 @@
class Guardian
include AASM
def inner_guard(options={})
end
aasm do
state :alpha, :initial => true
state :beta
@ -12,6 +15,13 @@ class Guardian
transitions :from => :alpha, :to => :beta, :guard => :fail
end
event :use_proc_guard_with_params do
transitions :from => :alpha, :to => :beta, :guard => Proc.new { |options={}| inner_guard(options) }
end
event :use_lambda_guard_with_params do
transitions :from => :alpha, :to => :beta, :guard => lambda { |options={}| inner_guard(options) }
end
event :use_guards_that_succeed do
transitions :from => :alpha, :to => :beta, :guards => [:succeed, :another_succeed]
end

View File

@ -27,6 +27,18 @@ describe "per-transition guards" do
expect { guardian.use_guards_where_the_second_fails! }.to raise_error(AASM::InvalidTransition)
expect(guardian).to be_alpha
end
describe "with params" do
it "using a Proc" do
expect(guardian).to receive(:inner_guard).with({:flag => true}).and_return(true)
guardian.use_proc_guard_with_params(:flag => true)
end
it "using a lambda" do
expect(guardian).to receive(:inner_guard).with({:flag => true}).and_return(true)
guardian.use_lambda_guard_with_params(:flag => true)
end
end
end
describe "event guards" do