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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/low_state.rb +19 -22
  3. data/lib/version.rb +1 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a7f8551226b4f95f7ef5778fb8e83cc7580007ccf9d463d151311203a6427dc3
4
- data.tar.gz: 88a706762b5372cf69b96a043db9b29f24766652b560aba8843355190d8cb6e9
3
+ metadata.gz: 0a89a7e89f29c28b76d0b8a6ffc54ec2fa5fbf5eb9e6511b2a58505f4f75e3cd
4
+ data.tar.gz: 79b05d82e962249e9e5e341d1fcdd2cf7878388a2894f42fef37730ed42fa458
5
5
  SHA512:
6
- metadata.gz: 3633447429803efb5bc09b8c228ca84c105b3123824031786e437c91ee9c48b47da04810ef2c9096001f5098b7f5a4ad00dc12e00e2eec90e0dcd5fb2775b539
7
- data.tar.gz: ef99c7f8107902aa4026063898e86f0fdc9fd5e0de6702f18c642d90495a3993fe67c4e94c66293c3ba3a84d0a46f7463915705d564e420195dc8d483d6461b2
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
- @@states
35
+ @states ||= {}
36
36
  end
37
- end
38
-
39
- base.extend ClassMethods
40
- end
41
37
 
42
- module ClassMethods
43
- def low_state(state)
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
- class_variable_set :@@states, class_states
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: nil, before: nil, after: nil, error: true)
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.nil? || @from.include?(current_state)
68
+ if @from.empty? || @from.include?(current_state)
72
69
  return true
73
70
  end
74
71
 
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LowState
4
- VERSION = '0.1.3'
4
+ VERSION = '0.2.0'
5
5
  end
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.1.3
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-24 00:00:00.000000000 Z
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.