diff --git a/README.md b/README.md index 4246ea5..31fdce2 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ Currently state supports the following options: - `initial` : `Bool` **optional** - indicates whether this state is initial or not. If initial state not specified, first one will be considered as initial - `guard` : `(-> Bool)` **optional** - a callback, that gets evaluated once state is getting entered. State will not enter if guard returns false - - `enter` : `(-> Nil)` **optional** - a hook, that gets evaluated once state entered. + - `enter` : `(->)` **optional** - a hook, that gets evaluated once state entered. ### Events diff --git a/spec/aasm/state_machine_spec.cr b/spec/aasm/state_machine_spec.cr index f1a8b07..e6338bd 100644 --- a/spec/aasm/state_machine_spec.cr +++ b/spec/aasm/state_machine_spec.cr @@ -140,21 +140,21 @@ module AASM it "runs enter block" do count = 0 - s = two_states_machine enter: ->{ count += 1; nil } + s = two_states_machine enter: ->{ count += 1 } s.fire_event :activate count.should eq 1 end it "does not run enter block if guard returns false" do count = 0 - s = two_states_machine enter: ->{ count += 1; nil }, guard: ->{ false } + s = two_states_machine enter: ->{ count += 1 }, guard: ->{ false } s.fire_event :activate count.should eq 0 end it "should not run block twice if event gets fired twice" do count = 0 - s = two_states_machine enter: ->{ count += 1; nil } + s = two_states_machine enter: ->{ count += 1 } s.fire_event :activate s.fire_event :activate count.should eq 1 @@ -162,7 +162,7 @@ module AASM it "should handle cycled states" do count = 0 - s = one_state_machine enter: ->{ count += 1; nil } + s = one_state_machine enter: ->{ count += 1 } s.fire_event :restart s.current_state_name.should eq :started count.should eq 1 diff --git a/spec/aasm/state_spec.cr b/spec/aasm/state_spec.cr index 79da9f8..7dd7f1b 100644 --- a/spec/aasm/state_spec.cr +++ b/spec/aasm/state_spec.cr @@ -14,6 +14,11 @@ module AASM t.enter.should_not be_nil end + it "accepts :enter that can return anything" do + t = State.new({enter: ->{ "anything" }}) + t.enter.not_nil!.call.should be_nil + end + it "accepts :guard" do t = State.new({guard: ->{ true }}) t.guard.should_not be_nil diff --git a/spec/aasm_spec.cr b/spec/aasm_spec.cr index bc57b86..5a94f9d 100644 --- a/spec/aasm_spec.cr +++ b/spec/aasm_spec.cr @@ -10,7 +10,7 @@ class Transaction def act_as_state_machine aasm.state :pending, initial: true - aasm.state :active, enter: ->{ @count += 1; nil } + aasm.state :active, enter: ->{ @count += 1 } aasm.state :completed aasm.event :activate do |e| diff --git a/src/aasm/state.cr b/src/aasm/state.cr index 19fc6ce..2af3559 100644 --- a/src/aasm/state.cr +++ b/src/aasm/state.cr @@ -3,7 +3,9 @@ struct AASM::State getter guard : (-> Bool)? def initialize(opts = {enter: nil, guard: nil}) - @enter = opts[:enter]? + if enter = opts[:enter]? + @enter = -> { enter.not_nil!.call; nil } + end @guard = opts[:guard]? end end diff --git a/src/aasm/state_machine.cr b/src/aasm/state_machine.cr index f7aea29..1ddb084 100644 --- a/src/aasm/state_machine.cr +++ b/src/aasm/state_machine.cr @@ -7,7 +7,7 @@ class AASM::StateMachine @transition_table = {} of Symbol => Array(Symbol) end - def state(name : Symbol, initial : Bool = false, enter : (-> Nil) = nil, guard : (-> Bool) = nil) + def state(name : Symbol, initial : Bool = false, enter : (-> _) = nil, guard : (-> Bool) = nil) check_states_already_defined name state = State.new({enter: enter, guard: guard}) @current_state_name = name if initial || @current_state_name.nil?