soveran / micromachine Goto Github PK
View Code? Open in Web Editor NEWMinimal Finite State Machine
License: MIT License
Minimal Finite State Machine
License: MIT License
This is by far one of the cleanest code I've ever encountered.
You deserve my LOVE.
❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️
it would be useful if one could do
machine.when(:event,:any => :state)
it is especially useful for events like :reset, for example a connection is dropped and everything must begin anew.
Latest release on Rubygems is 2.0.0, which does not include triggerable_events
method.
This issue can be easily solved by releasing a new [bumped] version, since the code is already in place.
It seems to me that bang methods in a state machine model suggests that the change in state will be saved. I think it's better not to use ! in the example code. What do you think?
This is probably a very rare case, but the change in fc9635c is not compatible with the previous version and could result in failures.
The code below demonstrates:
require 'micromachine'
class CallbackBug
def initialize
@fsm = MicroMachine.new(:new).tap do |fsm|
fsm.when(:first, new: :first_stage)
fsm.when(:second, first_stage: :second_stage)
fsm.on(:first_stage, &method(:first_stage))
fsm.on(:second_stage, &method(:second_stage))
fsm.on(:any) do |event|
puts "Event #{event} => #{@fsm.state}"
end
end
end
def run
@fsm.trigger!(:first)
@fsm.trigger!(:second)
end
private
def first_stage(event)
p event
end
def second_stage
end
end
CallbackBug.new.run
The output of this is:
:first
Event first => first_stage
ArgumentError: wrong number of arguments (1 for 0)
from (pry):28:in `second_stage'
Hello,
I love the simplicity of this library, thank you for creating it!
I am looking for a FSM framework and micormachine
is a close match. However, we want to automate some part of our workflow. In order to do that, the object needs to know its available next states it can transition into, all its available events (to transition into the next state) and possibly the previous state.
Here is a code example to better describe this feature:
(irb): machine.state
=> :confirmed
# This is what I'd need
(irb): machine.next_events
=> [:reset]
(irb): machine.next_states
=> [:new]
(irb): machine.previous_states
=> [:new]
I understand this tool is very simple and you might want to keep it simple. I figured I need to build this functionality and others might take advantage of it.
What do you think?
If i run rake or rake test i get:
Warning: you should require 'minitest/autorun' instead.
Warning: or add 'gem "minitest"' before 'require "minitest/autorun"'
and the suite stops with the error
MiniTest::Unit::TestCase is now Minitest::Test. From /Users/xxx/.rbenv/versions/2.1.2/lib/ruby/2.1.0/test/unit/testcase.rb:8:in `<module:Unit>'
/Users/xxx/.rbenv/versions/2.1.2/lib/ruby/2.1.0/test/unit.rb:676:in `<class:Runner>': undefined method `_run_suite' for class `Test::Unit::Runner' (NameError)
I'm on OS X Yosemite (v 10.10) with
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]
installed with rbenv
In order to have the test suite working i had to:
rbenv local 1.9.3-p547
gem install contest
rake
Side note: I've tried to use dep install to get contest installed but it failed with:
gem install contest:0.1.3
ERROR: Could not find a valid gem 'contest:0.1.3' (>= 0) in any repository
Not really used to IRC, so felt like i could ask here; sorry if it does not fit. Plus I'm pretty ne to fsm,
I found this gem very understandable, and somehow not bloated compared to other solutions, so 'id like to go further using it
I have a simple use case; let's say sending a specific mail when a state change for one to another.
So in a controller's action, and compared to the example you provide :
@model = Model.find(params[:id])
@model.confirm!
@model.save # to have the new sate effectively saved
In the model I have this :
...
fsm.on(:confirmed) do
... sending some mail ...
end
...
If for some reason the model does not save, the mail is still sent (the fsm.on
event occurs right when you call confirm!
on it). Is it possible able to fire the vent only if the model is valid to save ?
To keep a simple syntax, only calling .confirm!
on the model (not overriding with .save
to effectively having the state changed in the db), to validate and save the new state. Is that something possible ?
Again, I know it is not stack overflow; hope I'm clear and that it's okay to ask this kind of thing
Best.
MicroMachine exposes an events
method, which lists all events that may be possible at any time. But only a subset of those can be triggered from a given state.
In an application it's sometimes useful to present that subset to users. For instance, if a post is in "draft" status, display buttons to trigger :publish
or :archive
, but not to :unpublish
.
I'm using this code to find triggerable events:
sm.transitions_for.keys.select { |event| sm.trigger?(event) }
Might this be worth adding to MicroMachine itself?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.