An open API service indexing awesome lists of open source software.

https://github.com/siman-man/state_transition

StateMachine for Ruby
https://github.com/siman-man/state_transition

gem ruby

Last synced: 11 months ago
JSON representation

StateMachine for Ruby

Awesome Lists containing this project

README

          

# StateTransition

https://github.com/jakesgordon/javascript-state-machine
This program is very useful. so I created ruby ver.

version 0.1.0 is active only upper 2.1.0.

## Installation

Add this line to your application's Gemfile:

gem 'state_transition'

And then execute:

$ bundle

Or install it yourself as:

$ gem install state_transition

## Usage

require 'state_transition'

state = StateTransition::StateMachine.new({
initial: :first,

actions: [
{ name: "move_first", from: [ :second, :third ], to: :first },
{ name: "move_second", from: [ :first, :third ], to: :second },
{ name: "move_third", from: :second, to: :third },
],

callbacks: {
before_second: -> { puts "before_second" },
after_second: -> { puts "after_second" },
}
})

puts state.current #=> :first
puts state.can_move?(:second) #=> true
puts state.can_move?(:third) #=> false

state.move_second

puts state.current #=> :second

## 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