state_machine_enum 0.1.3 → 0.1.4
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/CHANGELOG.md +14 -17
- data/README.md +20 -20
- data/lib/state_machine_enum/version.rb +1 -1
- data/lib/state_machine_enum.rb +17 -2
- metadata +11 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a64dc12150ea615573c5fc5ce222e80fa8f2959392c94f3c6a80fae6efa53cd
|
4
|
+
data.tar.gz: 63b7a19ac4794c1b75d5e4d8a6b93964e5b9400d394927458d44893d2c9f4a31
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b705009767fb54820386c8748f1747a45d2145dc177355569a794669d1514bac07cb9f5666999950d158ed1b9fba3808eebb8b28f63fdeab3da1818314919598
|
7
|
+
data.tar.gz: 187ee30f697c019617f45b057e709d404a2cd11083e57c0f7387f24272a27a17ce7afce6da31b8dff675914498589b9a13e4a64c1eab0e21fe6101d1aa277b75
|
data/CHANGELOG.md
CHANGED
@@ -1,31 +1,28 @@
|
|
1
|
-
|
2
|
-
All notable changes to this project will be documented in this file.
|
1
|
+
## 0.1.4
|
3
2
|
|
4
|
-
|
3
|
+
* Raise `InvalidTransition` which is a subclass of `InvalidState`. That exception contains the following properties which can be used:
|
4
|
+
* `attribute_name` is the name of the enum attribute
|
5
|
+
* `from_state` is the state the transition was attempted from
|
6
|
+
* `to_state` is the state the transition was going to
|
7
|
+
* `already_in_target_state?` is `true` if a repeated transition was attempted
|
8
|
+
* Some more code styling
|
9
|
+
* Relax dependencies so that Bundler does not complain on Rails 8.x apps
|
5
10
|
|
6
11
|
## 0.1.3
|
7
12
|
|
8
|
-
|
9
|
-
|
10
|
-
- Added more documentation and improvements to the readme
|
11
|
-
- Little code styling
|
13
|
+
* Added more documentation and improvements to the readme
|
14
|
+
* Little code styling
|
12
15
|
|
13
16
|
## 0.1.2
|
14
17
|
|
15
|
-
|
16
|
-
|
17
|
-
- basic tests for a library
|
18
|
-
|
19
|
-
### Changed
|
20
|
-
|
21
|
-
- #ensure_<attribute>_may_transition_to! method now actually verifies that an attribute can transition to a new state.
|
18
|
+
* Add basic tests
|
19
|
+
* `#ensure_<attribute>_may_transition_to!` now actually verifies that an attribute can transition to the new state.
|
22
20
|
|
23
21
|
## 0.1.1
|
24
22
|
|
25
|
-
### Added
|
26
23
|
|
27
|
-
|
24
|
+
* Provide real authors.
|
28
25
|
|
29
26
|
## 0.1.0
|
30
27
|
|
31
|
-
|
28
|
+
* Initial release
|
data/README.md
CHANGED
@@ -18,11 +18,11 @@ end
|
|
18
18
|
|
19
19
|
Install the gem and add it to the application's Gemfile by executing:
|
20
20
|
|
21
|
-
|
21
|
+
$ bundle add state_machine_enum
|
22
22
|
|
23
23
|
If bundler is not being used to manage dependencies, install the gem by executing:
|
24
24
|
|
25
|
-
|
25
|
+
$ gem install state_machine_enum
|
26
26
|
|
27
27
|
## Usage
|
28
28
|
|
@@ -33,10 +33,10 @@ class User < ApplicationRecord
|
|
33
33
|
include StateMachineEnum
|
34
34
|
|
35
35
|
state_machine_enum :state, prefix: :state do |s|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
36
|
+
s.permit_transition(:registered, :active)
|
37
|
+
s.permit_transition(:active, :banned)
|
38
|
+
s.permit_transition(:banned, :active)
|
39
|
+
s.permit_transition(:active, :deleted)
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
@@ -69,10 +69,10 @@ For example the state updates to :registered, but before the model is saved
|
|
69
69
|
|
70
70
|
```ruby
|
71
71
|
state_machine_enum :state, prefix: "state" do |s|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
72
|
+
s.permit_transition(:registered, :active)
|
73
|
+
s.after_inline_transition_to(:active) do |model|
|
74
|
+
model.another_attr = Time.now.utc
|
75
|
+
end
|
76
76
|
end
|
77
77
|
```
|
78
78
|
|
@@ -85,10 +85,10 @@ updated to :registered
|
|
85
85
|
|
86
86
|
```ruby
|
87
87
|
state_machine_enum :state, prefix: "state" do |s|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
88
|
+
s.permit_transition(:registered, :active)
|
89
|
+
s.after_committed_transition_to(:active) do |model|
|
90
|
+
model.send_notification!
|
91
|
+
end
|
92
92
|
end
|
93
93
|
```
|
94
94
|
|
@@ -98,11 +98,11 @@ For example if you want to do something after any state update has commited.
|
|
98
98
|
|
99
99
|
```ruby
|
100
100
|
state_machine_enum :state, prefix: "state" do |s|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
101
|
+
s.permit_transition(:registered, :active)
|
102
|
+
s.permit_transition(:active, :suspended)
|
103
|
+
s.after_any_committed_transition_to do |model|
|
104
|
+
log_changes!
|
105
|
+
end
|
106
106
|
end
|
107
107
|
```
|
108
108
|
|
@@ -123,7 +123,7 @@ Predicate to check if a transition is possible with the rules we've set.
|
|
123
123
|
|
124
124
|
```ruby
|
125
125
|
state_machine_enum :state, prefix: "state" do |s|
|
126
|
-
|
126
|
+
s.permit_transition(:registered, :active)
|
127
127
|
end
|
128
128
|
|
129
129
|
state_may_transition_to?(:active) # => true
|
data/lib/state_machine_enum.rb
CHANGED
@@ -72,6 +72,21 @@ module StateMachineEnum
|
|
72
72
|
class InvalidState < StandardError
|
73
73
|
end
|
74
74
|
|
75
|
+
class InvalidTransition < InvalidState
|
76
|
+
attr_reader :attribute_name, :from_state, :to_state
|
77
|
+
|
78
|
+
def already_in_target_state?
|
79
|
+
@from_state == @to_state
|
80
|
+
end
|
81
|
+
|
82
|
+
def initialize(attribute_name, attempted_transition_from_state, attempted_transition_to_state, *args_for_super)
|
83
|
+
@attribute_name = attribute_name.to_s
|
84
|
+
@from_state = attempted_transition_from_state.to_s
|
85
|
+
@to_state = attempted_transition_to_state.to_s
|
86
|
+
super(*args_for_super)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
75
90
|
class_methods do
|
76
91
|
def state_machine_enum(attribute_name, **options_for_enum)
|
77
92
|
# Collect the states
|
@@ -117,10 +132,10 @@ module StateMachineEnum
|
|
117
132
|
|
118
133
|
define_method(:"ensure_#{attribute_name}_may_transition_to!") do |next_state|
|
119
134
|
val = self[attribute_name]
|
120
|
-
raise
|
135
|
+
raise InvalidTransition.new(attribute_name, val, next_state, "#{attribute_name} already is #{val.inspect}") if next_state.to_s == val
|
121
136
|
return if collector._may_transition?(val, next_state)
|
122
137
|
|
123
|
-
raise
|
138
|
+
raise InvalidTransition.new(attribute_name, val, next_state, "#{attribute_name} may not transition from #{val.inspect} to #{next_state.inspect}")
|
124
139
|
end
|
125
140
|
|
126
141
|
define_method(:"#{attribute_name}_may_transition_to?") do |next_state|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: state_machine_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Julik Tarkhanov
|
@@ -10,22 +10,22 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2025-03-24 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activesupport
|
17
17
|
requirement: !ruby/object:Gem::Requirement
|
18
18
|
requirements:
|
19
|
-
- - "
|
19
|
+
- - ">="
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: '7'
|
21
|
+
version: '7.0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
25
|
requirements:
|
26
|
-
- - "
|
26
|
+
- - ">="
|
27
27
|
- !ruby/object:Gem::Version
|
28
|
-
version: '7'
|
28
|
+
version: '7.0'
|
29
29
|
- !ruby/object:Gem::Dependency
|
30
30
|
name: sqlite3
|
31
31
|
requirement: !ruby/object:Gem::Requirement
|
@@ -44,16 +44,16 @@ dependencies:
|
|
44
44
|
name: activerecord
|
45
45
|
requirement: !ruby/object:Gem::Requirement
|
46
46
|
requirements:
|
47
|
-
- - "
|
47
|
+
- - ">="
|
48
48
|
- !ruby/object:Gem::Version
|
49
|
-
version: '7'
|
49
|
+
version: '7.0'
|
50
50
|
type: :development
|
51
51
|
prerelease: false
|
52
52
|
version_requirements: !ruby/object:Gem::Requirement
|
53
53
|
requirements:
|
54
|
-
- - "
|
54
|
+
- - ">="
|
55
55
|
- !ruby/object:Gem::Version
|
56
|
-
version: '7'
|
56
|
+
version: '7.0'
|
57
57
|
description: Concern that makes it easy to define and enforce possibe state transitions
|
58
58
|
for a field/object
|
59
59
|
email:
|
@@ -94,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
94
|
- !ruby/object:Gem::Version
|
95
95
|
version: '0'
|
96
96
|
requirements: []
|
97
|
-
rubygems_version: 3.
|
97
|
+
rubygems_version: 3.3.7
|
98
98
|
signing_key:
|
99
99
|
specification_version: 4
|
100
100
|
summary: Define possible state transitions for a field
|