Project

finite

0.0
No commit activity in last 3 years
No release in over 3 years
A simple state machine implementation for ruby
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
>= 0
~> 3.0
>= 0
 Project Readme

Finite Build Status

A simple state machine implementation for ruby

Installation

Add this line to your application's Gemfile:

gem 'finite'

And then execute:

$ bundle

Or install it yourself as:

$ gem install finite

Usage

require 'finite'

class Elevator
    include Finite

    def initialize
        @broken = false
    end

    finite initial: :idle do

        before do
            "This is called before every state but has no purpose other than to show its existence in this example."
        end

        before :doors_closing do
            puts 'Doors Closing!'
        end

        before :doors_opening do
            puts 'Doors Opening!'
        end

        event :prepare do
            go from: :idle, to: :doors_closing, if: -> { not @broken }
        end

        event :go_up do
            go from: :doors_closing, to: :elevator_going_up
            after do
                puts 'Going Up!'
            end
        end

        event :go_down do
            go from: :doors_closing, to: :elevator_going_down
            after do
                puts 'Going Down!'
            end
        end

        event :start do
            go from: [:elevator_going_up, :elevator_going_down], to: :moving
        end

        event :approach do
            go from: :moving, to: :stopping
        end

        event :stop do
            go from: :stopping, to: :doors_opening
            before do
                announce_floor
            end
        end

        event :open_doors do
            go from: :doors_opening, to: :at_floor
        end

        event :finish do
            go from: :at_floor, to: :checking_next_dest
        end

        event :make_request do
            go from: :checking_next_dest, to: :doors_closing
        end

        event :make_no_request do
            go from: :checking_next_dest, to: :idle
        end
    end

    def announce_floor
        puts "Arriving on floor #{rand(10)}"
    end
end
elevator = Elevator.new
elevator.current_state          # => :idle
elevator.can_prepare?           # => true
elevator.can_open_doors?        # => false
elevator.open_doors             # => Finite::Error 'Invalid Transition'
elevator.idle?                  # => true
elevator.prepare                # => 'Doors Closing!'
elevator.current_state          # => :doors_closing
elevator.possible_events        # => [:go_up, :go_down]

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

Copyright (c) 2014 Kristen Mills

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.