Git Product home page Git Product logo

micromachine's Issues

Sharing some love

This is by far one of the cleanest code I've ever encountered.
You deserve my LOVE.
❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️

:any transition state

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.

Bang methods in the README example

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?

Change to callback parameters in 1.2 is backwards-incompatible

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'

Trigerring Event Automatically

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?

Test suite fails to run on ruby 2.1.2

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

Using micromachine

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.

Add `#triggerable_events` method?

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?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.