-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit cde762d
Showing
14 changed files
with
249 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/doc/ | ||
/libs/ | ||
/.crystal/ | ||
/.shards/ | ||
|
||
|
||
# Libraries don't need dependency lock | ||
# Dependencies will be locked in application that uses them | ||
/shard.lock | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
language: crystal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 Vitalii Elenhaupt | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# aasm.cr | ||
|
||
Easy to use state machine for Crystal. | ||
|
||
## Installation | ||
|
||
|
||
Add this to your application's `shard.yml`: | ||
|
||
```yaml | ||
dependencies: | ||
aasm: | ||
github: veelenga/aasm.cr | ||
``` | ||
## Usage | ||
```crystal | ||
require "aasm" | ||
|
||
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 |t| | ||
t.transitions from: :pending, to: :active | ||
end | ||
|
||
aasm.event :complete do |t| | ||
t.transitions from: :pending, to: :active | ||
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 | ||
``` | ||
|
||
## Contributing | ||
|
||
1. Fork it ( https://github.com/veelenga/aasm/fork ) | ||
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 a new Pull Request |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
name: aasm | ||
version: 0.1.0 | ||
|
||
authors: | ||
- Vitalii Elenhaupt <velenhaupt@gmail.com> | ||
|
||
license: MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
require "./spec_helper" | ||
|
||
describe Aasm do | ||
# TODO: Write tests | ||
|
||
it "works" do | ||
false.should eq(true) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require "spec" | ||
require "../src/aasm" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require "./aasm/*" | ||
|
||
module AASM | ||
abstract def act_as_state_machine | ||
|
||
def aasm | ||
@aasm ||= AASM::StateMachine.new | ||
end | ||
|
||
def state | ||
aasm.current_state_name | ||
end | ||
|
||
def next_state | ||
aasm.next_state | ||
end | ||
|
||
def fire(event : Symbol) | ||
aasm.fire event | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class AASM::Event | ||
|
||
getter! :transition | ||
|
||
def transitions(from = nil : Symbol, to = nil : Symbol) | ||
@transition = Transition.new({from: from, to: to}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module AASM | ||
class NoSuchEventException < Exception | ||
def initialize(event_name) | ||
super "Event '#{event_name}' does not exist." | ||
end | ||
end | ||
|
||
class EventAlreadyDefinedException < Exception | ||
def initialize(event_name) | ||
super "Event '#{event_name}' already defined." | ||
end | ||
end | ||
|
||
class NoSuchStateException < Exception | ||
def initialize(state_name) | ||
super "State '#{state_name}' does not exist." | ||
end | ||
end | ||
|
||
class StateAlreadyDefinedException < Exception | ||
def initialize(state_name) | ||
super "State '#{state_name}' already defined." | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
struct AASM::State | ||
|
||
getter :enter, :guard | ||
|
||
def initialize(opts) | ||
@enter = opts[:enter]? | ||
@guard = opts[:guard]? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
class AASM::StateMachine | ||
|
||
getter current_state_name | ||
|
||
def initialize | ||
@states = {} of Symbol => State | ||
@events = {} of Symbol => Event | ||
@transition_table = [] of Transition | ||
end | ||
|
||
def state(name : Symbol, initial = false : Bool, enter = nil : (->), guard = nil : (-> Bool)) | ||
check_state_already_defined name | ||
state = State.new({enter: enter, guard: guard}) | ||
@current_state_name = name if initial || @current_state_name.nil? | ||
@states[name] = state | ||
end | ||
|
||
def event(name : Symbol) | ||
check_event_already_defined name | ||
event = Event.new | ||
yield event | ||
transition = event.transition | ||
check_states_exists transition.from, transition.to | ||
@transition_table << transition | ||
@events[name] = event | ||
end | ||
|
||
def next_state | ||
@transition_table.each do |t| | ||
return t.to if t.from == @current_state_name | ||
end | ||
nil | ||
end | ||
|
||
def fire(event_name : Symbol) | ||
check_event_exists event_name | ||
|
||
transition = @events[event_name].transition | ||
if transition.from == @current_state_name | ||
state = @states[transition.to] | ||
if state.guard.nil? || state.guard.not_nil!.call | ||
state.enter.try &.call | ||
@current_state_name = transition.to | ||
end | ||
end | ||
end | ||
|
||
private def check_states_exists(*state_names) | ||
state_names.each do |state_name| | ||
raise NoSuchStateException.new state_name unless @states[state_name]? | ||
end | ||
end | ||
|
||
private def check_state_already_defined(*state_names) | ||
state_names.each do |state_name| | ||
raise StateAlreadyDefinedException.new state_name if @states[state_name]? | ||
end | ||
end | ||
|
||
private def check_event_exists(*event_names) | ||
event_names.each do |event_name| | ||
raise NoSuchEventException.new event_name unless @events[event_name]? | ||
end | ||
end | ||
|
||
private def check_event_already_defined(*event_names) | ||
event_names.each do |event_name| | ||
raise EventAlreadyDefinedException.new event_name if @events[event_name]? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
struct AASM::Transition | ||
|
||
getter! :from, :to | ||
|
||
def initialize(opts) | ||
@from = opts[:from] | ||
@to = opts[:to] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module AASM | ||
VERSION = "0.1.0" | ||
end |