flawless_state
flawless_state
FlawlessState is a shard that you can include in your objects to provide a simple state machine. It's written as a Struct in order to keep it as fast as possible.
Installation
Add this to your application's shard.yml
:
dependencies:
flawless_state:
github: jasonl99/flawless_state
Usage
require "flawless_state"
Create an instance property as follows:
property state = FlawlessState::State.new
The default out-of-the-box configuration provides five states. The format is simply: each state as a key has a value of states that can be transitioned into
{
:new => [:initialized, :ready, :complete],
:initialized => [:ready, :complete],
:ready => [:complete],
:complete => [] of Symbol,
:error => [] of Symbol
}
If you'd like to use your own states on your object's property, it can be overriden using property state = FlawlessState::State.new(my_states)
To transition between states, provide the new state:
class MyObject
def complete
state.transition_to :completed
end
end
If a state cannot transition to the new state, a FlawlessState::Exception is raised. You can force a state transition by uisng the force method:
class MyObject
def error
state.transition_to! :error
end
end
You can test if a state is terminal:
my_instantce.state.terminal?
Or get its current value
my_instance.state.current_state
Check to see if it's possible to transition
my_instance.state.transition? :completed
Development
The goal of FlawlessState is to remain as simple as possible, providing a simple but effective state machine. This is very early release, and I'm new to crystal, so I'm sure there'js lots to improve.
Contributing
- Fork it ( https://github.com/[your-github-name]/flawless_state/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
Contributors
- jasonl99 Jason Landry - creator, maintainer
flawless_state
- 0
- 0
- 0
- 0
- 0
- almost 8 years ago
- January 6, 2017
MIT License
Wed, 20 Nov 2024 19:11:16 GMT