finite_machine 0.13.0 → 0.14.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +42 -23
- data/LICENSE.txt +1 -1
- data/README.md +207 -145
- data/lib/finite_machine/catchable.rb +20 -12
- data/lib/finite_machine/choice_merger.rb +2 -2
- data/lib/finite_machine/definition.rb +54 -17
- data/lib/finite_machine/dsl.rb +35 -5
- data/lib/finite_machine/env.rb +1 -1
- data/lib/finite_machine/events_map.rb +5 -6
- data/lib/finite_machine/hook_event.rb +1 -1
- data/lib/finite_machine/hooks.rb +10 -5
- data/lib/finite_machine/message_queue.rb +72 -26
- data/lib/finite_machine/observer.rb +38 -26
- data/lib/finite_machine/safety.rb +6 -6
- data/lib/finite_machine/state_machine.rb +19 -16
- data/lib/finite_machine/state_parser.rb +8 -8
- data/lib/finite_machine/subscribers.rb +1 -1
- data/lib/finite_machine/threadable.rb +1 -1
- data/lib/finite_machine/transition.rb +6 -5
- data/lib/finite_machine/transition_builder.rb +4 -4
- data/lib/finite_machine/transition_event.rb +1 -1
- data/lib/finite_machine/version.rb +1 -1
- data/lib/finite_machine.rb +6 -6
- metadata +18 -13
@@ -1,15 +1,15 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "forwardable"
|
4
4
|
|
5
|
-
require_relative
|
6
|
-
require_relative
|
7
|
-
require_relative
|
8
|
-
require_relative
|
9
|
-
require_relative
|
10
|
-
require_relative
|
11
|
-
require_relative
|
12
|
-
require_relative
|
5
|
+
require_relative "catchable"
|
6
|
+
require_relative "dsl"
|
7
|
+
require_relative "env"
|
8
|
+
require_relative "events_map"
|
9
|
+
require_relative "hook_event"
|
10
|
+
require_relative "observer"
|
11
|
+
require_relative "threadable"
|
12
|
+
require_relative "subscribers"
|
13
13
|
|
14
14
|
module FiniteMachine
|
15
15
|
# Base class for state machine
|
@@ -64,7 +64,8 @@ module FiniteMachine
|
|
64
64
|
# Allow or not logging of transitions
|
65
65
|
attr_threadsafe :log_transitions
|
66
66
|
|
67
|
-
def_delegators :dsl, :initial, :terminal, :event, :trigger_init
|
67
|
+
def_delegators :dsl, :initial, :terminal, :event, :trigger_init,
|
68
|
+
:alias_target
|
68
69
|
|
69
70
|
# Initialize state machine
|
70
71
|
#
|
@@ -194,7 +195,7 @@ module FiniteMachine
|
|
194
195
|
# fsm.can?(:go) # => true
|
195
196
|
#
|
196
197
|
# @example
|
197
|
-
# fsm.can?(:go,
|
198
|
+
# fsm.can?(:go, "Piotr") # checks condition with parameter "Piotr"
|
198
199
|
#
|
199
200
|
# @param [String] event
|
200
201
|
#
|
@@ -202,7 +203,7 @@ module FiniteMachine
|
|
202
203
|
#
|
203
204
|
# @api public
|
204
205
|
def can?(*args)
|
205
|
-
event_name
|
206
|
+
event_name = args.shift
|
206
207
|
events_map.can_perform?(event_name, current, *args)
|
207
208
|
end
|
208
209
|
|
@@ -284,7 +285,7 @@ module FiniteMachine
|
|
284
285
|
else
|
285
286
|
exception = InvalidStateError
|
286
287
|
catch_error(exception) ||
|
287
|
-
|
288
|
+
raise(exception, "inappropriate current state '#{current}'")
|
288
289
|
|
289
290
|
false
|
290
291
|
end
|
@@ -390,8 +391,10 @@ module FiniteMachine
|
|
390
391
|
# @api public
|
391
392
|
def inspect
|
392
393
|
sync_shared do
|
393
|
-
"<##{self.class}:0x#{object_id.to_s(16)}
|
394
|
-
"@
|
394
|
+
"<##{self.class}:0x#{object_id.to_s(16)} " \
|
395
|
+
"@current=#{current.inspect} " \
|
396
|
+
"@states=#{states} " \
|
397
|
+
"@events=#{events} " \
|
395
398
|
"@transitions=#{events_map.state_transitions}>"
|
396
399
|
end
|
397
400
|
end
|
@@ -407,7 +410,7 @@ module FiniteMachine
|
|
407
410
|
#
|
408
411
|
# @api private
|
409
412
|
def raise_transition_error(error)
|
410
|
-
|
413
|
+
raise TransitionError, Logger.format_error(error)
|
411
414
|
end
|
412
415
|
|
413
416
|
# Forward the message to observer or self
|
@@ -7,7 +7,9 @@ module FiniteMachine
|
|
7
7
|
#
|
8
8
|
# @api private
|
9
9
|
class StateParser
|
10
|
-
|
10
|
+
NON_STATE_KEYS = %i[name if unless silent].freeze
|
11
|
+
|
12
|
+
STATE_KEYS = %i[from to].freeze
|
11
13
|
|
12
14
|
# Extract states from user defined attributes
|
13
15
|
#
|
@@ -35,7 +37,7 @@ module FiniteMachine
|
|
35
37
|
# @api private
|
36
38
|
def self.ensure_only_states!(attrs)
|
37
39
|
attributes = attrs.dup
|
38
|
-
|
40
|
+
NON_STATE_KEYS.each { |key| attributes.delete(key) }
|
39
41
|
raise_not_enough_transitions unless attributes.any?
|
40
42
|
attributes
|
41
43
|
end
|
@@ -69,7 +71,7 @@ module FiniteMachine
|
|
69
71
|
#
|
70
72
|
# @api public
|
71
73
|
def self.contains_from_to_keys?(attrs)
|
72
|
-
|
74
|
+
STATE_KEYS.any? { |key| attrs.keys.include?(key) }
|
73
75
|
end
|
74
76
|
private_class_method :contains_from_to_keys?
|
75
77
|
|
@@ -79,9 +81,8 @@ module FiniteMachine
|
|
79
81
|
#
|
80
82
|
# @api private
|
81
83
|
def self.convert_from_to_attributes_to_states_hash(attrs)
|
82
|
-
Array(attrs[:from] || ANY_STATE).
|
84
|
+
Array(attrs[:from] || ANY_STATE).each_with_object({}) do |state, hash|
|
83
85
|
hash[state] = attrs[:to] || state
|
84
|
-
hash
|
85
86
|
end
|
86
87
|
end
|
87
88
|
private_class_method :convert_from_to_attributes_to_states_hash
|
@@ -99,13 +100,12 @@ module FiniteMachine
|
|
99
100
|
#
|
100
101
|
# @api private
|
101
102
|
def self.convert_attributes_to_states_hash(attrs)
|
102
|
-
attrs.
|
103
|
+
attrs.each_with_object({}) do |(k, v), hash|
|
103
104
|
if k.respond_to?(:to_ary)
|
104
105
|
k.each { |el| hash[el] = v }
|
105
106
|
else
|
106
107
|
hash[k] = v
|
107
108
|
end
|
108
|
-
hash
|
109
109
|
end
|
110
110
|
end
|
111
111
|
private_class_method :convert_attributes_to_states_hash
|
@@ -119,7 +119,7 @@ module FiniteMachine
|
|
119
119
|
#
|
120
120
|
# @api private
|
121
121
|
def self.raise_not_enough_transitions
|
122
|
-
raise NotEnoughTransitionsError,
|
122
|
+
raise NotEnoughTransitionsError, "please provide state transitions"
|
123
123
|
end
|
124
124
|
private_class_method :raise_not_enough_transitions
|
125
125
|
end # StateParser
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
4
|
-
require_relative
|
3
|
+
require_relative "callable"
|
4
|
+
require_relative "threadable"
|
5
5
|
|
6
6
|
module FiniteMachine
|
7
7
|
# Class describing a transition associated with a given event
|
@@ -117,7 +117,7 @@ module FiniteMachine
|
|
117
117
|
#
|
118
118
|
# @example
|
119
119
|
# transition = Transition.new(context, name: :go)
|
120
|
-
# transition.to_s # =>
|
120
|
+
# transition.to_s # => "go"
|
121
121
|
#
|
122
122
|
# @return [String]
|
123
123
|
#
|
@@ -132,8 +132,9 @@ module FiniteMachine
|
|
132
132
|
#
|
133
133
|
# @api public
|
134
134
|
def inspect
|
135
|
-
transitions = @states.map { |from, to| "#{from} -> #{to}" }.join(
|
136
|
-
"<##{self.class} @name=#{@name}, @transitions=#{transitions},
|
135
|
+
transitions = @states.map { |from, to| "#{from} -> #{to}" }.join(", ")
|
136
|
+
"<##{self.class} @name=#{@name}, @transitions=#{transitions}, " \
|
137
|
+
"@when=#{@conditions}>"
|
137
138
|
end
|
138
139
|
end # Transition
|
139
140
|
end # FiniteMachine
|
@@ -1,9 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
4
|
-
require_relative
|
5
|
-
require_relative
|
6
|
-
require_relative
|
3
|
+
require_relative "event_definition"
|
4
|
+
require_relative "state_definition"
|
5
|
+
require_relative "state_parser"
|
6
|
+
require_relative "transition"
|
7
7
|
|
8
8
|
module FiniteMachine
|
9
9
|
# A class reponsible for building transition out of parsed states
|
data/lib/finite_machine.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "logger"
|
4
4
|
|
5
|
-
require_relative
|
6
|
-
require_relative
|
7
|
-
require_relative
|
8
|
-
require_relative
|
9
|
-
require_relative
|
5
|
+
require_relative "finite_machine/const"
|
6
|
+
require_relative "finite_machine/logger"
|
7
|
+
require_relative "finite_machine/definition"
|
8
|
+
require_relative "finite_machine/state_machine"
|
9
|
+
require_relative "finite_machine/version"
|
10
10
|
|
11
11
|
module FiniteMachine
|
12
12
|
# Default state name
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: finite_machine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.14.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Murach
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-10-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: concurrent-ruby
|
@@ -39,33 +39,33 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0.5'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
61
|
+
version: '3.0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
68
|
+
version: '3.0'
|
69
69
|
description: A minimal finite state machine with a straightforward syntax. You can
|
70
70
|
quickly model states, add callbacks and use object-oriented techniques to integrate
|
71
71
|
with ORMs.
|
@@ -73,7 +73,10 @@ email:
|
|
73
73
|
- piotr@piotrmurach.com
|
74
74
|
executables: []
|
75
75
|
extensions: []
|
76
|
-
extra_rdoc_files:
|
76
|
+
extra_rdoc_files:
|
77
|
+
- README.md
|
78
|
+
- CHANGELOG.md
|
79
|
+
- LICENSE.txt
|
77
80
|
files:
|
78
81
|
- CHANGELOG.md
|
79
82
|
- LICENSE.txt
|
@@ -112,11 +115,13 @@ licenses:
|
|
112
115
|
- MIT
|
113
116
|
metadata:
|
114
117
|
allowed_push_host: https://rubygems.org
|
118
|
+
bug_tracker_uri: https://github.com/piotrmurach/finite_machine/issues
|
115
119
|
changelog_uri: https://github.com/piotrmurach/finite_machine/blob/master/CHANGELOG.md
|
116
120
|
documentation_uri: https://www.rubydoc.info/gems/finite_machine
|
117
121
|
homepage_uri: https://piotrmurach.github.io/finite_machine/
|
122
|
+
rubygems_mfa_required: 'true'
|
118
123
|
source_code_uri: https://github.com/piotrmurach/finite_machine
|
119
|
-
post_install_message:
|
124
|
+
post_install_message:
|
120
125
|
rdoc_options: []
|
121
126
|
require_paths:
|
122
127
|
- lib
|
@@ -131,8 +136,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
131
136
|
- !ruby/object:Gem::Version
|
132
137
|
version: '0'
|
133
138
|
requirements: []
|
134
|
-
rubygems_version: 3.
|
135
|
-
signing_key:
|
139
|
+
rubygems_version: 3.3.26
|
140
|
+
signing_key:
|
136
141
|
specification_version: 4
|
137
142
|
summary: A minimal finite state machine with a straightforward syntax.
|
138
143
|
test_files: []
|