Designing state machines
When dealing with business processes, I feel that the evented nature of a formal fsm can be a bit off-putting. I have on several occasions used a batch-processing approach, that maintains state in a persistent record, giving many of the same characteristics as an fsm. Let&#x27;s say we have our movie from the example (pseudo-code, obviously):<p><pre><code>    class MovieState &lt; Model
      belongs_to :movie
    end

    class InitialStep
      def select
        MovieState.where(state: &#x27;initial&#x27;)
      end

      def process(movie_state)
        movie_state.state = &#x27;in_production&#x27;
      end
    end

    class InProductionStep
      def select
        MovieState.where(state: &#x27;in_production&#x27;)
      end

      def process(movie_state)
        if movie_state.movie.finished_shooting?
          movie_state.state = &#x27;in_theaters&#x27;
        end
      end
    end

    class MovieWorkflow
      def initialize
        @steps = []
        @steps &lt;&lt; InitialStep.new
        @steps &lt;&lt; InProductionStep.new
      end

      def run
        @steps.each do |step|
          step.select.each do |state|
            step.process(state)
            state.save
          end
        end
      end
    end
</code></pre>
This makes it very clear when things are happening. It&#x27;s also easy to debug and test.<p>Of course, there are a lot of limitations, most notably that it can&#x27;t deal with an async event happening. In my experience though, this can be dealt with by storing the payload of that event somewhere in the db, where the step-selector can access it on next run. One thing this model deals very well with, is delays, which is often part of business processes.<p>Any thoughts? Does this design have a more formal name?