Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
veelenga committed Jan 16, 2016
0 parents commit cde762d
Show file tree
Hide file tree
Showing 14 changed files with 249 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
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

1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
language: crystal
21 changes: 21 additions & 0 deletions LICENSE
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.
53 changes: 53 additions & 0 deletions README.md
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
7 changes: 7 additions & 0 deletions shard.yml
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
9 changes: 9 additions & 0 deletions spec/aasm_spec.cr
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
2 changes: 2 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "spec"
require "../src/aasm"
21 changes: 21 additions & 0 deletions src/aasm.cr
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
8 changes: 8 additions & 0 deletions src/aasm/event.cr
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
25 changes: 25 additions & 0 deletions src/aasm/exceptions.cr
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
9 changes: 9 additions & 0 deletions src/aasm/state.cr
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
71 changes: 71 additions & 0 deletions src/aasm/state_machine.cr
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
9 changes: 9 additions & 0 deletions src/aasm/transition.cr
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
3 changes: 3 additions & 0 deletions src/aasm/version.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module AASM
VERSION = "0.1.0"
end

0 comments on commit cde762d

Please sign in to comment.