trotter / newflow

Workflows for ruby classes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Newflow

Newflow lets you decorate ruby classes with workflows. It also has a cool .dot generation tool for visualizing your workflows.

Usage


  class OfficeWorker
    attr_accessor :boss, :phones
    attr_accessor :workflow_state  # Necessary unless you are using Rails
                                   # In Rails, create a workflow_state column in the db

    include Newflow

    define_workflow do
      state :at_work, :start => true do
        transitions_to :sleeping_under_desk, :unless => :boss_is_there?
        transitions_to :answering_phones, :if => :boss_is_there?
      end

      state :sleeping_under_desk do
        transitions_to :answering_phones, :if => :phones_are_ringing?
        transitions_to :going_home, :if => :after_five?, :trigger => :shutdown_computer
      end

      state :answering_phones do
        transitions_to :going_home, :if => :after_five?, :trigger => :shutdown_computer
      end

      state :going_home, :stop => true
    end

    def initialize
      @phones = []
    end

    def boss_is_there?
      !!boss
    end

    def phones_are_ringing?
      phones.detect { |p| p.ringing? }
    end

    def after_five?
      Time.now.hour >= 17
    end

    def shutdown_computer
      puts "shutdown -h now"
    end
  end

  office_worker = OfficeWorker.new
  office_worker.at_work?             # => true
  office_worker.transition!
  office_worker.to_dotty             # returns a .dot formatted string 
                                     #   that you can write to a 
                                     #   file and open with graphviz
  office_worker.sleeping_under_desk? # => true (because the boss wasn't there)

Caveats

None! This thing is awesome!

About

Workflows for ruby classes


Languages

Language:Ruby 100.0%