low_state 0.1.3 → 0.2.0
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/low_state.rb +19 -22
- data/lib/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0a89a7e89f29c28b76d0b8a6ffc54ec2fa5fbf5eb9e6511b2a58505f4f75e3cd
|
|
4
|
+
data.tar.gz: 79b05d82e962249e9e5e341d1fcdd2cf7878388a2894f42fef37730ed42fa458
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4ff419f702882cae619b45fc478a01c402bad30422f95152d1ef44acda50f931c6bf4ce13132c2a66d38285c4395600cbf41eae0bb1318d200b0471cb28229ed
|
|
7
|
+
data.tar.gz: 82c52726c0739ac33117dbc217ea2ef41d389f45373aa054302fc8e55b84130978f425ebef059ca65cb4173caca6ac680027c5b4719cea806b1343e3335504ff
|
data/lib/low_state.rb
CHANGED
|
@@ -4,6 +4,7 @@ require_relative 'version'
|
|
|
4
4
|
|
|
5
5
|
module LowState
|
|
6
6
|
class StateChangeError < StandardError; end
|
|
7
|
+
class MissingStateError < StandardError; end
|
|
7
8
|
|
|
8
9
|
def current_state
|
|
9
10
|
@current_state ||= states.keys.first
|
|
@@ -11,6 +12,8 @@ module LowState
|
|
|
11
12
|
|
|
12
13
|
def change_state(new_state, *args)
|
|
13
14
|
state = states[new_state]
|
|
15
|
+
raise MissingStateError, "State :#{new_state} missing in #{states.keys}" if state.nil?
|
|
16
|
+
|
|
14
17
|
before_result = state.has_before_callback?(self.class) ? send(state.before, *args) : true
|
|
15
18
|
|
|
16
19
|
if before_result != false && state.can_change_state?(current_state)
|
|
@@ -28,38 +31,32 @@ module LowState
|
|
|
28
31
|
|
|
29
32
|
def self.included(base)
|
|
30
33
|
class << base
|
|
31
|
-
# Class level variables because we want states to be accessed by any other included module or subclass.
|
|
32
|
-
@@states = {}
|
|
33
|
-
|
|
34
34
|
def states
|
|
35
|
-
|
|
35
|
+
@states ||= {}
|
|
36
36
|
end
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
base.extend ClassMethods
|
|
40
|
-
end
|
|
41
37
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
class_states = class_variable_get :@@states
|
|
45
|
-
|
|
46
|
-
if state.is_a?(Symbol)
|
|
47
|
-
class_states[state] = State.new(state:)
|
|
48
|
-
elsif state.is_a?(Hash)
|
|
49
|
-
state_name = state.keys.first
|
|
50
|
-
class_states[state_name] = State.new(state:, from: [*state[state_name]])
|
|
51
|
-
elsif state.is_a?(State)
|
|
52
|
-
class_states[state.state] = state
|
|
38
|
+
def set_states(states)
|
|
39
|
+
@states = states
|
|
53
40
|
end
|
|
54
41
|
|
|
55
|
-
|
|
42
|
+
def low_state(state, **args)
|
|
43
|
+
class_states = states
|
|
44
|
+
|
|
45
|
+
if state.is_a?(Symbol)
|
|
46
|
+
class_states[state] = State.new(state:, **args)
|
|
47
|
+
elsif state.is_a?(State)
|
|
48
|
+
class_states[state.state] = state
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
set_states(class_states)
|
|
52
|
+
end
|
|
56
53
|
end
|
|
57
54
|
end
|
|
58
55
|
|
|
59
56
|
class State
|
|
60
57
|
attr_reader :state, :from, :before, :after
|
|
61
58
|
|
|
62
|
-
def initialize(state:, from:
|
|
59
|
+
def initialize(state:, from: [], before: nil, after: nil, error: true)
|
|
63
60
|
@state = state
|
|
64
61
|
@from = [*from]
|
|
65
62
|
@before = before || "#{@state}_state_before"
|
|
@@ -68,7 +65,7 @@ module LowState
|
|
|
68
65
|
end
|
|
69
66
|
|
|
70
67
|
def can_change_state?(current_state)
|
|
71
|
-
if @from.
|
|
68
|
+
if @from.empty? || @from.include?(current_state)
|
|
72
69
|
return true
|
|
73
70
|
end
|
|
74
71
|
|
data/lib/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: low_state
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- maedi
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2025-09-
|
|
11
|
+
date: 2025-09-25 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: A very fast low-level state machine. Uses no events for simplicity and
|
|
14
14
|
speed, it just changes state.
|