mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
Fix warning when overriding an exsiting method
This commit is contained in:
parent
24b1ad99ec
commit
d485306794
3 changed files with 66 additions and 21 deletions
|
@ -404,7 +404,7 @@ simple.aasm(:work).current
|
||||||
|
|
||||||
_AASM_ doesn't prohibit to define the same event in more than one state machine. The
|
_AASM_ doesn't prohibit to define the same event in more than one state machine. The
|
||||||
latest definition "wins" and overrides previous definitions. Nonetheless, a warning is issued:
|
latest definition "wins" and overrides previous definitions. Nonetheless, a warning is issued:
|
||||||
`SimpleMultipleExample: The aasm event name run is already used!`.
|
`SimpleMultipleExample: overriding method 'run'!`.
|
||||||
|
|
||||||
All _AASM_ class- and instance-level `aasm` methods accept a state machine selector.
|
All _AASM_ class- and instance-level `aasm` methods accept a state machine selector.
|
||||||
So, for example, to use inspection on a class level, you have to use
|
So, for example, to use inspection on a class level, you have to use
|
||||||
|
|
|
@ -74,13 +74,10 @@ module AASM
|
||||||
def state(name, options={})
|
def state(name, options={})
|
||||||
@state_machine.add_state(name, klass, options)
|
@state_machine.add_state(name, klass, options)
|
||||||
|
|
||||||
if klass.instance_methods.include?("#{name}?")
|
aasm_name = @name.to_sym
|
||||||
warn "#{klass.name}: The aasm state name #{name} is already used!"
|
state = name.to_sym
|
||||||
end
|
safely_define_method klass, "#{name}?", -> do
|
||||||
|
aasm(aasm_name).current_state == state
|
||||||
aasm_name = @name
|
|
||||||
klass.send :define_method, "#{name}?", ->() do
|
|
||||||
aasm(:"#{aasm_name}").current_state == :"#{name}"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
unless klass.const_defined?("STATE_#{name.upcase}")
|
unless klass.const_defined?("STATE_#{name.upcase}")
|
||||||
|
@ -92,27 +89,24 @@ module AASM
|
||||||
def event(name, options={}, &block)
|
def event(name, options={}, &block)
|
||||||
@state_machine.add_event(name, options, &block)
|
@state_machine.add_event(name, options, &block)
|
||||||
|
|
||||||
if klass.instance_methods.include?("may_#{name}?".to_sym)
|
aasm_name = @name.to_sym
|
||||||
warn "#{klass.name}: The aasm event name #{name} is already used!"
|
event = name.to_sym
|
||||||
end
|
|
||||||
|
|
||||||
# an addition over standard aasm so that, before firing an event, you can ask
|
# an addition over standard aasm so that, before firing an event, you can ask
|
||||||
# may_event? and get back a boolean that tells you whether the guard method
|
# may_event? and get back a boolean that tells you whether the guard method
|
||||||
# on the transition will let this happen.
|
# on the transition will let this happen.
|
||||||
aasm_name = @name
|
safely_define_method klass, "may_#{name}?", ->(*args) do
|
||||||
|
aasm(aasm_name).may_fire_event?(event, *args)
|
||||||
klass.send :define_method, "may_#{name}?", ->(*args) do
|
|
||||||
aasm(:"#{aasm_name}").may_fire_event?(:"#{name}", *args)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
klass.send :define_method, "#{name}!", ->(*args, &block) do
|
safely_define_method klass, "#{name}!", ->(*args, &block) do
|
||||||
aasm(:"#{aasm_name}").current_event = :"#{name}!"
|
aasm(aasm_name).current_event = :"#{name}!"
|
||||||
aasm_fire_event(:"#{aasm_name}", :"#{name}", {:persist => true}, *args, &block)
|
aasm_fire_event(aasm_name, event, {:persist => true}, *args, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
klass.send :define_method, "#{name}", ->(*args, &block) do
|
safely_define_method klass, name, ->(*args, &block) do
|
||||||
aasm(:"#{aasm_name}").current_event = :"#{name}"
|
aasm(aasm_name).current_event = event
|
||||||
aasm_fire_event(:"#{aasm_name}", :"#{name}", {:persist => false}, *args, &block)
|
aasm_fire_event(aasm_name, event, {:persist => false}, *args, &block)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -183,5 +177,13 @@ module AASM
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def safely_define_method(klass, method_name, method_definition)
|
||||||
|
if klass.instance_methods.include?(method_name.to_sym)
|
||||||
|
warn "#{klass.name}: overriding method '#{method_name}'!"
|
||||||
|
end
|
||||||
|
|
||||||
|
klass.send(:define_method, method_name, method_definition)
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
43
spec/unit/override_warning_spec.rb
Normal file
43
spec/unit/override_warning_spec.rb
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe 'warns when overrides a method' do
|
||||||
|
module Clumsy
|
||||||
|
def self.included base
|
||||||
|
base.send :include, AASM
|
||||||
|
|
||||||
|
base.aasm do
|
||||||
|
state :valid
|
||||||
|
event(:save) { }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'state' do
|
||||||
|
class Base
|
||||||
|
def valid?; end
|
||||||
|
end
|
||||||
|
it do
|
||||||
|
expect { Base.send :include, Clumsy }.
|
||||||
|
to output(/Base: overriding method 'valid\?'!/).to_stderr
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'event' do
|
||||||
|
context 'may?' do
|
||||||
|
class Base
|
||||||
|
def may_save?; end
|
||||||
|
def save!; end
|
||||||
|
def save; end
|
||||||
|
end
|
||||||
|
let(:klass) { Base }
|
||||||
|
it do
|
||||||
|
expect { Base.send :include, Clumsy }.
|
||||||
|
to output(/Base: overriding method 'may_save\?'!/).to_stderr
|
||||||
|
expect { Base.send :include, Clumsy }.
|
||||||
|
to output(/Base: overriding method 'save!'!/).to_stderr
|
||||||
|
expect { Base.send :include, Clumsy }.
|
||||||
|
to output(/Base: overriding method 'save'!/).to_stderr
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue