-
Notifications
You must be signed in to change notification settings - Fork 0
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 f54d25c
Showing
6 changed files
with
179 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,19 @@ | ||
Copyright (c) 2021 Jacob Herrington | ||
|
||
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,66 @@ | ||
nmachine | ||
============ | ||
|
||
Nanoscopic Finite State Machine | ||
|
||
Description | ||
----------- | ||
I wrote this gem for fun. | ||
|
||
It's heavily inspired by [micromachine](https://github.com/soveran/micromachine). | ||
|
||
Actually, this is just what happened after I read the micromachine repository. | ||
|
||
The idea is to get most of the same functionality with less code. | ||
|
||
The implementation is around 30 lines, but including all the other Ruby code in | ||
this repository (mostly tests), you're looking at about 80 lines. | ||
|
||
You can use the only class in this project to create Finite State Machines: | ||
|
||
```ruby | ||
require 'nmachine' | ||
|
||
m = NMachine.new(:new) # :new is the machine's initial state, this can be anything | ||
m.when(:submit, new: :pending) do # define a new event and the associated transitions | ||
puts "Submitted, the state is now #{m.state}" # pass an optional block for callbacks | ||
end | ||
m.when(:approve, pending: :approved) do | ||
puts "Approved, the state is now #{m.state}" | ||
end | ||
m.when(:reject, pending: :rejected) do | ||
puts "Rejected, the state is now #{m.state}" | ||
end | ||
m.when(:pause, approved: :paused, rejected: :paused, new: :paused, pending: :paused) do | ||
puts "Paused, the state is now #{m.state}" | ||
end | ||
m.when(:reset, approved: :new, rejected: :new, paused: :new) do | ||
puts "Reset, the state is now #{m.state}" | ||
end | ||
|
||
m.state #=> :new | ||
|
||
m.submit.state | ||
#=> Submitted, the state is now pending | ||
#=> :pending | ||
|
||
m.pause | ||
#=> Paused, the state is now paused | ||
|
||
m.reject | ||
#=> NMachine::InvalidEventError (reject not valid from paused) | ||
|
||
m.reset.pause.reset.approve.state | ||
#=> Reset, the state is now paused | ||
#=> Paused, the state is now paused | ||
#=> Reset, the state is now paused | ||
#=> Approved, the state is now approved | ||
#=> :approved | ||
``` | ||
|
||
Installation | ||
------------ | ||
|
||
```sh | ||
gem install nmachine | ||
``` |
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 @@ | ||
require 'rake/testtask' | ||
|
||
Rake::TestTask.new do |t| | ||
t.libs << 'test' | ||
end | ||
|
||
desc "Run tests" | ||
task default: :test |
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,31 @@ | ||
class NMachine | ||
InvalidEventError = Class.new(ArgumentError) | ||
|
||
attr_reader :state | ||
|
||
def initialize(initial_state) | ||
@state = initial_state | ||
@events = Hash.new | ||
end | ||
|
||
def when(event, transistions = {}, &block) | ||
@events[event] = [transistions, block] | ||
end | ||
|
||
private | ||
|
||
def transition(event) | ||
ensure_allowed(event) | ||
@state = @events[event][0][state] | ||
@events[event][1]&.call | ||
self | ||
end | ||
|
||
def ensure_allowed(event) | ||
raise InvalidEventError.new("#{event} not valid from #{state}") unless @events[event][0].has_key?(state) | ||
end | ||
|
||
def method_missing(m, *args, &block) | ||
if @events.has_key?(m) then transition(m) else super 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,11 @@ | ||
Gem::Specification.new do |s| | ||
s.name = 'nmachine' | ||
s.version = '0.1.0' | ||
s.summary = 'Nanoscopic Finite State Machine' | ||
s.description = 'A nanoscopic Finite State Machine. Like micromachine, but smaller.' | ||
s.author = ['Jacob Herrington'] | ||
s.email = ['jacobherringtondeveloper+nmachine@gmail.com'] | ||
s.homepage = "http://github.com/jacobherrington/nmachine" | ||
s.license = 'MIT' | ||
s.files = ['lib/nmachine.rb'] | ||
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,44 @@ | ||
require 'minitest/autorun' | ||
require 'nmachine' | ||
|
||
class NMachineTest < Minitest::Test | ||
attr_accessor :machine | ||
|
||
def setup | ||
@side_effect = nil | ||
@machine = NMachine.new(:new) | ||
machine.when(:see, new: :seen) do | ||
@side_effect = "See" | ||
end | ||
machine.when(:ignore, new: :ignored, seen: :ignored) do | ||
@side_effect = "Ignore" | ||
end | ||
machine.when(:handle, new: :handled, seen: :handled) do | ||
@side_effect = "Handle" | ||
end | ||
machine.when(:reset, seen: :new, ignored: :new, handled: :new) do | ||
@side_effect = "Reset" | ||
end | ||
machine.when(:confuse, seen: :ignored, ignored: :seen, new: :handled, handled: :new) do | ||
@side_effect = "Huh" | ||
end | ||
end | ||
|
||
def test_setting_up_a_new_machine | ||
assert_equal :new, machine.state | ||
end | ||
|
||
def test_transitions | ||
assert_equal :ignored, machine.ignore.state | ||
assert_equal "Ignore", @side_effect | ||
assert_equal :new, machine.reset.state | ||
assert_equal "Reset", @side_effect | ||
assert_equal :handled, machine.confuse.state | ||
assert_equal "Huh", @side_effect | ||
end | ||
|
||
def test_error_handling | ||
assert_raises(NMachine::InvalidEventError, 'see not valid from ignored') { machine.ignore.see } | ||
assert_raises(NoMethodError) { machine.asdf } | ||
end | ||
end |