finite_state_machine 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/fsm/machine.rb +44 -0
- data/lib/fsm/transition_error.rb +9 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 76a4b06b4c2b38f084523605742d7e063949884d
|
4
|
+
data.tar.gz: 238229e5089f74af1efb5f6e5d1f8d4c6d650834
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 390d2d60d3d9225f5ecc52a4b7e1eadef805b17b969dcc3022d02b47bf0d6bdf7b7e2e592f7dc1866c83be4a612243e2a73c9dfeedeffb820e0e827a9cb9f864
|
7
|
+
data.tar.gz: acf2f6a0cea294c58eec07cb9114b19ddab592d28b374cded054321949060138f0062b08777fd3ec29e9d23dec20ca387b8a3bc171abe8971244e2b32d65ed7c
|
data/lib/fsm/machine.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
module Fsm
|
2
|
+
class Machine
|
3
|
+
attr_accessor :schema, :from, :to, :error
|
4
|
+
|
5
|
+
def initialize(schema:, changeset:)
|
6
|
+
raise ArgumentError.new("changeset argument must be an array with only 2 elements") unless (changeset.is_a?(Array) && changeset.length == 2)
|
7
|
+
raise ArgumentError.new("schema is invalid") unless valid_schema?(schema)
|
8
|
+
@schema = schema
|
9
|
+
@from, @to = changeset
|
10
|
+
end
|
11
|
+
|
12
|
+
def transition
|
13
|
+
found_transition = schema.dup.select do |mapping|
|
14
|
+
mapping[:from].include?(from.to_sym) && mapping[:to] == to.to_sym
|
15
|
+
end
|
16
|
+
|
17
|
+
if found_transition.length > 1
|
18
|
+
@error = TransitionError.new(self)
|
19
|
+
@error.message = "Found an ambiguous number of transitions that can ocurr: #{found_transition.map { |x| x[:call] }}"
|
20
|
+
false
|
21
|
+
elsif found_transition.length < 1
|
22
|
+
@error = TransitionError.new(self)
|
23
|
+
@error.message = "Could not find a transition from #{from} to #{to}"
|
24
|
+
false
|
25
|
+
else
|
26
|
+
found_transition.pop[:call]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def transition!
|
31
|
+
transition || raise(error)
|
32
|
+
end
|
33
|
+
|
34
|
+
def valid_schema?(schema)
|
35
|
+
return false unless schema.is_a?(Array)
|
36
|
+
schema.all? do |mapping|
|
37
|
+
mapping.keys == [:from, :to, :call] &&
|
38
|
+
mapping[:from].is_a?(Array) &&
|
39
|
+
mapping[:to].is_a?(Symbol) &&
|
40
|
+
mapping[:call].is_a?(Symbol)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: finite_state_machine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mario Gintili
|
@@ -18,6 +18,8 @@ extensions: []
|
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
20
|
- lib/fsm.rb
|
21
|
+
- lib/fsm/machine.rb
|
22
|
+
- lib/fsm/transition_error.rb
|
21
23
|
homepage: http://rubygems.org/gems/finite_state_machine
|
22
24
|
licenses:
|
23
25
|
- MIT
|