Skip to content

Commit

Permalink
Enter hook now accepts any block
Browse files Browse the repository at this point in the history
  • Loading branch information
veelenga committed Jun 13, 2016
1 parent 8ce95f9 commit b14bc68
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions spec/aasm/state_machine_spec.cr
Expand Up @@ -140,29 +140,29 @@ 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
end

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
Expand Down
5 changes: 5 additions & 0 deletions spec/aasm/state_spec.cr
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/aasm_spec.cr
Expand Up @@ -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|
Expand Down
4 changes: 3 additions & 1 deletion src/aasm/state.cr
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/aasm/state_machine.cr
Expand Up @@ -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?
Expand Down

0 comments on commit b14bc68

Please sign in to comment.