Giter Site home page Giter Site logo

newflow's Introduction

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!

newflow's People

Contributors

trotter avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

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.