finite_state_machine 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 31485239a46bc5a97274996829a253a467a6d565
4
- data.tar.gz: b3023965486eb68bb87cb82b203f18560890a0d0
3
+ metadata.gz: 76a4b06b4c2b38f084523605742d7e063949884d
4
+ data.tar.gz: 238229e5089f74af1efb5f6e5d1f8d4c6d650834
5
5
  SHA512:
6
- metadata.gz: 0eb999f3990df20cf8542cf25fd4f72fa95590e15a30a8179ab5ae8103545808e6556ad2531ad4bc6e2d18c95dda0caa0d2624d02365221f03b30d694b250847
7
- data.tar.gz: 8877f8722a4bf0ed44676df5a8c7a1aa99cff58e57a5c07ea5e2fbaf8e6e5d2fab3c262dac2b60b23170d3bb6c3a2c91d4e7734eee4e5afc8271934a819e3e28
6
+ metadata.gz: 390d2d60d3d9225f5ecc52a4b7e1eadef805b17b969dcc3022d02b47bf0d6bdf7b7e2e592f7dc1866c83be4a612243e2a73c9dfeedeffb820e0e827a9cb9f864
7
+ data.tar.gz: acf2f6a0cea294c58eec07cb9114b19ddab592d28b374cded054321949060138f0062b08777fd3ec29e9d23dec20ca387b8a3bc171abe8971244e2b32d65ed7c
@@ -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
@@ -0,0 +1,9 @@
1
+ require "./lib/fsm"
2
+
3
+ class Fsm::TransitionError < StandardError
4
+ attr_accessor :message, :object
5
+
6
+ def initialize(object)
7
+ @object = object
8
+ end
9
+ 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.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