state_machine_enum 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.standard.yml +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +60 -0
- data/Rakefile +10 -0
- data/lib/state_machine_enum/version.rb +5 -0
- data/lib/state_machine_enum.rb +127 -0
- data/sig/state_machine_enum.rbs +4 -0
- metadata +67 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c62f9ef7d3062c2cfec2c914d2b4bdcb8dbd38621e7317af107d0ce65edf88ef
|
4
|
+
data.tar.gz: 98f294f99cb3943dd145874afef82add8533816a863a35411511cc32a7447826
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 213b039f7fe5b20f9a68306f17ae10d044bf56107d33d4cd0ad0c03c86d3c01a3bb963df8ccc117b3dda9a1cfe324dc5647adb77da0c0f33c57f293153a079ec
|
7
|
+
data.tar.gz: 490ec1ce157ce84481dd36e0973fa2601cc1638503b2769f04d60010527b81db4c9f208b1d6dfe285b81e65e143cfbaec802e55b8b6ee047af9a672bd0bedc3d
|
data/.standard.yml
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Stanislav Katkov
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# StateMachineEnum
|
2
|
+
|
3
|
+
This concern adds a method called "state_machine_enum" useful for defining an enum using string values along with valid state transitions. Validations will be added for the state transitions and a proper enum is going to be defined. For example:
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
state_machine_enum :state do |states|
|
7
|
+
states.permit_transition(:created, :approved_pending_settlement)
|
8
|
+
states.permit_transition(:approved_pending_settlement, :rejected)
|
9
|
+
states.permit_transition(:created, :rejected)
|
10
|
+
states.permit_transition(:approved_pending_settlement, :settled)
|
11
|
+
end
|
12
|
+
```
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Install the gem and add to the application's Gemfile by executing:
|
17
|
+
|
18
|
+
$ bundle add state_machine_enum
|
19
|
+
|
20
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
21
|
+
|
22
|
+
$ gem install state_machine_enum
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
StateMachineEnum needs to be extended and then it could be used, as example in AR model.
|
27
|
+
|
28
|
+
```
|
29
|
+
class User < ApplicationRecord
|
30
|
+
include StateMachineEnum
|
31
|
+
|
32
|
+
state_machine_enum :state do |s|
|
33
|
+
s.permit_transition(:registered, :active)
|
34
|
+
s.permit_transition(:active, :banned)
|
35
|
+
s.permit_transition(:banned, :active)
|
36
|
+
s.permit_transition(:active, :deleted)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
And then it will offer bunch of convenient methods and callbacks that ensure proper state transitions.
|
42
|
+
|
43
|
+
```
|
44
|
+
user = User.new(state: 'registered')
|
45
|
+
user.active?
|
46
|
+
user.registered! # throws InvalidState error, because state can not transition to "registered".
|
47
|
+
```
|
48
|
+
## Development
|
49
|
+
|
50
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
51
|
+
|
52
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/cheddar-me/state_machine_enum.
|
57
|
+
|
58
|
+
## License
|
59
|
+
|
60
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "state_machine_enum/version"
|
4
|
+
require "active_support/concern"
|
5
|
+
|
6
|
+
# This concern adds a method called "state_machine_enum" useful for defining an enum using
|
7
|
+
# string values along with valid state transitions. Validations will be added for the
|
8
|
+
# state transitions and a proper enum is going to be defined. For example:
|
9
|
+
#
|
10
|
+
# state_machine_enum :state do |states|
|
11
|
+
# states.permit_transition(:created, :approved_pending_settlement)
|
12
|
+
# states.permit_transition(:approved_pending_settlement, :rejected)
|
13
|
+
# states.permit_transition(:created, :rejected)
|
14
|
+
# states.permit_transition(:approved_pending_settlement, :settled)
|
15
|
+
# end
|
16
|
+
|
17
|
+
module StateMachineEnum
|
18
|
+
extend ActiveSupport::Concern
|
19
|
+
|
20
|
+
class StatesCollector
|
21
|
+
attr_reader :states
|
22
|
+
attr_reader :after_commit_hooks
|
23
|
+
attr_reader :common_after_commit_hooks
|
24
|
+
attr_reader :after_attribute_write_hooks
|
25
|
+
attr_reader :common_after_write_hooks
|
26
|
+
|
27
|
+
def initialize
|
28
|
+
@transitions = Set.new
|
29
|
+
@states = Set.new
|
30
|
+
@after_commit_hooks = {}
|
31
|
+
@common_after_commit_hooks = []
|
32
|
+
@after_attribute_write_hooks = {}
|
33
|
+
@common_after_write_hooks = []
|
34
|
+
end
|
35
|
+
|
36
|
+
def permit_transition(from, to)
|
37
|
+
@states << from.to_s << to.to_s
|
38
|
+
@transitions << [from.to_s, to.to_s]
|
39
|
+
end
|
40
|
+
|
41
|
+
def may_transition?(from, to)
|
42
|
+
@transitions.include?([from.to_s, to.to_s])
|
43
|
+
end
|
44
|
+
|
45
|
+
def after_inline_transition_to(target_state, &blk)
|
46
|
+
@after_attribute_write_hooks[target_state.to_s] ||= []
|
47
|
+
@after_attribute_write_hooks[target_state.to_s] << blk.to_proc
|
48
|
+
end
|
49
|
+
|
50
|
+
def after_committed_transition_to(target_state, &blk)
|
51
|
+
@after_commit_hooks[target_state.to_s] ||= []
|
52
|
+
@after_commit_hooks[target_state.to_s] << blk.to_proc
|
53
|
+
end
|
54
|
+
|
55
|
+
def after_any_committed_transition(&blk)
|
56
|
+
@common_after_commit_hooks << blk.to_proc
|
57
|
+
end
|
58
|
+
|
59
|
+
def validate(model, attribute_name)
|
60
|
+
return unless model.persisted?
|
61
|
+
|
62
|
+
was = model.attribute_was(attribute_name)
|
63
|
+
is = model[attribute_name]
|
64
|
+
|
65
|
+
unless was == is || @transitions.include?([was, is])
|
66
|
+
model.errors.add(attribute_name, "Invalid transition from #{was} to #{is}")
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
class InvalidState < StandardError
|
72
|
+
end
|
73
|
+
|
74
|
+
class_methods do
|
75
|
+
def state_machine_enum(attribute_name, **options_for_enum)
|
76
|
+
# Collect the states
|
77
|
+
collector = StatesCollector.new
|
78
|
+
yield(collector).tap do
|
79
|
+
# Define the enum using labels, with string values
|
80
|
+
enum_map = collector.states.map(&:to_sym).zip(collector.states.to_a).to_h
|
81
|
+
enum(attribute_name, enum_map, **options_for_enum)
|
82
|
+
|
83
|
+
# Define validations for transitions
|
84
|
+
validates attribute_name, presence: true
|
85
|
+
validate { |model| collector.validate(model, attribute_name) }
|
86
|
+
|
87
|
+
# Define inline hooks
|
88
|
+
before_save do |model|
|
89
|
+
_value_was, value_has_become = model.changes[attribute_name]
|
90
|
+
next unless value_has_become
|
91
|
+
hook_procs = collector.after_attribute_write_hooks[value_has_become].to_a + collector.common_after_write_hooks.to_a
|
92
|
+
hook_procs.each do |hook_proc|
|
93
|
+
hook_proc.call(model)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# Define after commit hooks
|
98
|
+
after_commit do |model|
|
99
|
+
_value_was, value_has_become = model.previous_changes[attribute_name]
|
100
|
+
next unless value_has_become
|
101
|
+
hook_procs = collector.after_commit_hooks[value_has_become].to_a + collector.common_after_commit_hooks.to_a
|
102
|
+
hook_procs.each do |hook_proc|
|
103
|
+
hook_proc.call(model)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# Define the check methods
|
108
|
+
define_method(:"ensure_#{attribute_name}_one_of!") do |*allowed_states|
|
109
|
+
val = self[attribute_name]
|
110
|
+
return if Set.new(allowed_states.map(&:to_s)).include?(val)
|
111
|
+
raise InvalidState, "#{attribute_name} must be one of #{allowed_states.inspect} but was #{val.inspect}"
|
112
|
+
end
|
113
|
+
|
114
|
+
define_method(:"ensure_#{attribute_name}_may_transition_to!") do |next_state|
|
115
|
+
val = self[attribute_name]
|
116
|
+
raise InvalidState, "#{attribute_name} already is #{val.inspect}" if next_state.to_s == val
|
117
|
+
end
|
118
|
+
|
119
|
+
define_method(:"#{attribute_name}_may_transition_to?") do |next_state|
|
120
|
+
val = self[attribute_name]
|
121
|
+
return false if val == next_state.to_s
|
122
|
+
collector.may_transition?(val, next_state)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: state_machine_enum
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stanislav Katkov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-06-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '6.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '6.0'
|
27
|
+
description: Concern that makes it easy to define and enforce possibe state transitions
|
28
|
+
for a field/object
|
29
|
+
email:
|
30
|
+
- skatkov@cheddar.me
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- ".standard.yml"
|
36
|
+
- LICENSE.txt
|
37
|
+
- README.md
|
38
|
+
- Rakefile
|
39
|
+
- lib/state_machine_enum.rb
|
40
|
+
- lib/state_machine_enum/version.rb
|
41
|
+
- sig/state_machine_enum.rbs
|
42
|
+
homepage: https://cheddar.me
|
43
|
+
licenses:
|
44
|
+
- MIT
|
45
|
+
metadata:
|
46
|
+
homepage_uri: https://cheddar.me
|
47
|
+
source_code_uri: https://github.com/cheddar-me/state_machine_enum
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 3.0.0
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubygems_version: 3.5.3
|
64
|
+
signing_key:
|
65
|
+
specification_version: 4
|
66
|
+
summary: Define possible state transitions for a field
|
67
|
+
test_files: []
|