• Stars
    star
    50
  • Rank 558,302 (Top 12 %)
  • Language
    Crystal
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿ”ƒ Easy to use finite state machine for Crystal classes

aasm.cr Build Status

Aasm stands for "Acts As State Machine" which means that some abstract object can act as a finite state machine and can be in one of a finite number of states at a time; can change one state to another when initiated by a triggering event.

Getting Started

Adding a state machine to a Crystal class is as simple as including AASM module and writing act_as_state_machine method where you can define states and events with their transitions:

class Transaction
  include AASM

  def act_as_state_machine
    aasm.state :pending, initial: true
    aasm.state :active,  enter: -> { puts "Just got activated" }
    aasm.state :completed

    aasm.event :activate do |e|
      e.transitions from: :pending, to: :active
    end

    aasm.event :complete do |e|
      e.transitions from: :active, to: :completed
    end
  end
end

t = Transaction.new.tap &.act_as_state_machine
t.state          #=> :pending
t.next_state     #=> :active
t.fire :activate # Just got activated
t.state          #=> :active
t.next_state     #=> :completed

States

State can be defined using aasm.state method passing the name and options:

aasm.state :passive, initial: true

State options

Currently state supports the following options:

  • initial : Bool optional - indicates whether this state is initial or not. If initial state not specified, first one will be considered as initial
  • guard : (-> Bool) optional - a callback, that gets evaluated once state is getting entered. State will not enter if guard returns false
  • enter : (->) optional - a hook, that gets evaluated once state entered.

Events

Event can be defined using aasm.state method passing the name and a block with transitions:

aasm.event :delete do |e|
  e.transitions from: :active, to: :deleted
end

Event has to be defined after state definition. In other case NoSuchStateException will be raise.

Event options

Currently event supports the following options:

  • before : (->) optional - a callback, that gets evaluated once before changing a state
  • after : (->) optional - a callback, that gets evaluated once after changing a state.

Transitions

Transition can be defined on event with transitions method passing options:

aasm.event :complete do |e|
  e.transitions from: [:pending, :active], to: :deleted
end

Transition options

Currently transition supports the following options:

  • from : (Symbol | Array(Symbol)) required - state (or states) from which state of state machine can be changed when event fired
  • to : Symbol required - state to which state of state machine will change when event fired.

More examples

One state machine (circular)

class CircularStateMachine
  include AASM

  def act_as_state_machine
    aasm.state :started

    aasm.event :restart do |e|
      e.transitions from: :started, to: :started
    end
  end
end

Two states machine

class TwoStateMachine
  include AASM

  def act_as_state_machine
    assm.state :active
    aasm.state :deleted

    aasm.event :delete do |e|
      e.transitions from: :active, to: :deleted
    end
  end
end

Three states machine

class ThreeStatesMachine
  include AASM

  def act_as_state_machine
    aasm.state :pending, initial: true
    aasm.state :active
    aasm.state :completed

    aasm.event :activate do |e|
      e.transitions from: :pending, to: :active
    end
    aasm.event :complete do |e|
      e.before { puts "completing..." }
      e.after  { puts "completed" }
      e.transitions from: [:active, :pending], to: :completed
    end
  end
end

More Repositories

1

awesome-crystal

๐Ÿ’Ž A collection of awesome Crystal libraries, tools, frameworks and software
Crystal
3,307
star
2

dress

๐Ÿ‘— Dress up your stdout
Elixir
62
star
3

lua.cr

Crystal to Lua bridge
Crystal
59
star
4

carbon-now-sh.el

Emacs to https://carbon.now.sh integration
Emacs Lisp
56
star
5

emoji.cr

๐Ÿ’š Emoji for Crystal
Crystal
47
star
6

vicr

โœŒ๏ธ Vim-like Interactive CRystal
Crystal
41
star
7

i3ipc-ruby

๐Ÿ›ซ An improved Ruby library to control i3wm
Ruby
35
star
8

crystal-zsh

.oh-my-zsh plugin for Crystal
Shell
28
star
9

aws-lambda-face-blur

AWS Lambda Face Blur
JavaScript
17
star
10

aws-heic-converter

AWS Lamba HEIC Converter
JavaScript
13
star
11

shorturl.cr

A tiny library to use URL shortening services
Crystal
13
star
12

rainbow-spec

Rainbow spec formatter for Crystal
Crystal
12
star
13

rblist

Russia Ban List
TypeScript
11
star
14

yymmdd

โณ Simple DSL for working with dates.
Crystal
9
star
15

wil

What I Learnt
Ruby
8
star
16

terminal-focus-reporting.el

Minor mode for terminal focus reporting
Emacs Lisp
7
star
17

ember-mail-to

Ember component to create HTML mailto links
JavaScript
6
star
18

play-crystal.el

Emacs to https://play.crystal-lang.org integration
Emacs Lisp
6
star
19

jsonapi-payload-explorer

JSON::API Payload Explorer
JavaScript
6
star
20

bojack.ex

Elixir client for BoJack - the unreliable key-value store ๐Ÿด
Elixir
5
star
21

aws-dynamodb.cr

Crystal client for AWS DynamoDB
Crystal
5
star
22

jira_report

๐Ÿ“Ž Cli tool that prints jira activity report.
Ruby
4
star
23

nyancat.ex

Nyancat for Elixir ๐Ÿฑ
Elixir
4
star
24

gh-pr-review-voodoo

A GitHub bot to improve PR review workflow.
TypeScript
3
star
25

crystal-snippets

YASnippet Crystal snippets for Emacs
Emacs Lisp
3
star
26

raph

Ruby Argument Parsing for Humans
Ruby
2
star
27

heroku-buildpack-sidekiq.cr

Sidekiq for Crystal Heroku Buildpack
Shell
2
star
28

curly.el

Straight way to work with current file locations in Emacs
Emacs Lisp
2
star
29

amber_cmd

Amber provides a CLI client that makes interfacing with your file system and applications much smoother.
Crystal
1
star
30

aws-lambda-blur-by-rekognition-label

AWS Lambda to blur objects detected by AWS Rekognition
JavaScript
1
star
31

homebrew-tap

Ruby
1
star
32

veelenga.github.io

My own blog pages
CSS
1
star
33

cybfootball

ะคัƒั‚ะฑะพะปัŒะฝั– ั‚ัƒั€ะฝั–ั€ะธ ั„ะฐะบัƒะปัŒั‚ะตั‚ัƒ ะบั–ะฑะตั€ะฝะตั‚ะธะบะธ
Ruby
1
star
34

libcurl.cr

Crystal bindings to libcurl [WIP]
Crystal
1
star
35

crown

Preview markdown files locally with Crystal Markdown
Crystal
1
star
36

any_setting

Load your settings with ease
Crystal
1
star
37

ee

exercism exercises
Elixir
1
star
38

hallo

I tiny project for Make your own Shard in Crystal language tutorial
Crystal
1
star
39

ctci

Solutions for Cracking the Coding Interview, 5th edition
Crystal
1
star
40

react-flash-messages

Rails flash messages with React
Ruby
1
star
41

rev.ex

Use reveal.js in your Elixir ๐Ÿšง
Elixir
1
star
42

dotfiles

Storing my dotfiles
Emacs Lisp
1
star
43

kyiv-coworking-map

Kyiv Coworking Map
HTML
1
star