Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/system76/state_fair
State machine system for Elixir applications
https://github.com/system76/state_fair
Last synced: about 2 months ago
JSON representation
State machine system for Elixir applications
- Host: GitHub
- URL: https://github.com/system76/state_fair
- Owner: system76
- License: mit
- Created: 2015-10-27T22:06:45.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2015-10-27T22:07:32.000Z (about 9 years ago)
- Last Synced: 2023-08-03T10:10:11.290Z (over 1 year ago)
- Language: Elixir
- Size: 109 KB
- Stars: 3
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# StateFair
Inspired by the[`state_machine` ruby gem](https://github.com/pluginaweek/state_machine),
this## Installation
The package can be installed as:
1. Add state_fair to your list of dependencies in `mix.exs`:
def deps do
[{:state_fair, "~> 0.1"}]
end## Overview
```elixir
defmodule Order do
import StateFairdefstruct status: :new
state_manager :status do
event :purchase do
transition from: :new, to: :ordered
endevent :ship do
transition from: :ordered, to: :shipped
endevent :cancel do
transition from: [:new, :ordered], to: :cancelled
endevent :force_cancel do
transition from: {:any}, to: :cancelled
end
end
endorder = %Order{status: :new}
{:ok, order} = order |> Order.purchase # => order.status == :ordered
if order |> Order.can_ship? do
order = order |> Order.ship! # => order.status == :shipped
endorder |> Order.cancel # => :error
order |> Order.cancel! # throws StateFair.InvalidTransitionError
order |> Order.force_cancel! # => order.status == :cancelled
```