state_machine
https://github.com/pluginaweek/state_machine
Ruby
Adds support for creating state machines for attributes on any Ruby class
StateMachine::Machine#around_transition
Creates a callback that will be invoked *around* a transition so long as
the given requirements match the transition.
== The callback
Around callbacks wrap transitions, executing code both before and after.
These callbacks are defined in the exact same manner as before / after
callbacks with the exception that the transition must be yielded to in
order to finish running it.
If defining +around+ callbacks using blocks, you must yield within the
transition by directly calling the block (since yielding is not allowed
within blocks).
For example,
class Vehicle
state_machine do
around_transition do |block|
Benchmark.measure { block.call }
end
around_transition do |vehicle, block|
logger.info "vehicle was #{state}..."
block.call
logger.info "...and is now #{state}"
end
around_transition do |vehicle, transition, block|
logger.info "before #{transition.event}: #{vehicle.state}"
block.call
logger.info "after #{transition.event}: #{vehicle.state}"
end
end
end
Notice that referencing the block is similar to doing so within an
actual method definition in that it is always the last argument.
On the other hand, if you're defining +around+ callbacks using method
references, you can yield like normal:
class Vehicle
state_machine do
around_transition :benchmark
...
end
def benchmark
Benchmark.measure { yield }
end
end
See +before_transition+ for a description of the possible configurations
for defining callbacks.Edit
git clone [email protected]:pluginaweek/state_machine.git
cd state_machine
open lib/state_machine/machine.rb
Contribute
# Make a new branchgit checkout -b -your-name--update-docs-StateMachine--Machine-around_transition-for-pr
# Commit to gitgit add lib/state_machine/machine.rbgit commit -m "better docs for StateMachine::Machine#around_transition"
# Open pull requestgem install hub # on a mac you can `brew install hub`
hub fork
git push <your name> -your-name--update-docs-StateMachine--Machine-around_transition-for-pr
hub pull-request
# Celebrate!