estate 0.0.1 → 0.0.2
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/LICENSE.txt +1 -1
- data/README.md +89 -0
- data/lib/estate/active_record.rb +10 -18
- data/lib/estate/configuration.rb +1 -1
- data/lib/estate/estate.rb +4 -7
- data/lib/estate/requirements.rb +3 -5
- data/lib/estate/state_machine.rb +1 -1
- data/lib/estate/version.rb +2 -2
- metadata +65 -8
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f445ae55c16632d7805cd1774f2800eed57ebf16ce63db5ef7e780225ecc52a1
|
|
4
|
+
data.tar.gz: 60527e77ed67584c663bde4504a104c1b9b38e179fdd299e5344bef2031d6bb9
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e27a18f1f5a82b3b2316d559eb7847f400f15b025fad62dc3d1ced8106856d2c8c60014a86af2837e81f03cf6956175af7f2ab0dabf08d0b0d4fb6b7dac37495
|
|
7
|
+
data.tar.gz: a2f52b92fff0482abbc7981ab5685d59b5430e171f37c568f68f8ab72d1ed57d851886b0bbf631c472ee34ced52699203571c1ffd420ee636d94cfa00daf1984
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# Estate Gem
|
|
2
|
+
|
|
3
|
+
Estate is a Ruby gem designed to simplify state management in ActiveRecord models. The primary focus of this gem is to provide a straightforward way to define states and transitions using a clean syntax, allowing seamless integration into ActiveRecord models.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile and run `bundle install`:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'estate'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Or install it manually using:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
gem install estate
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
To use the Estate gem, include it in your ActiveRecord model and define your states and transitions inside a block using the `estate` method. Here's a simple example:
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
class MyModel < ApplicationRecord
|
|
25
|
+
include Estate
|
|
26
|
+
|
|
27
|
+
estate do
|
|
28
|
+
state :state_1
|
|
29
|
+
state :state_2
|
|
30
|
+
state :state_3
|
|
31
|
+
|
|
32
|
+
transition from: :state_1, to: :state_2
|
|
33
|
+
transition from: :state_2, to: :state_3
|
|
34
|
+
transition from: :state_3, to: :state_1
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
The default field for storing the state is named state. You can customize this field by providing options to the estate method:
|
|
40
|
+
|
|
41
|
+
```ruby
|
|
42
|
+
class MyModel < ApplicationRecord
|
|
43
|
+
include Estate
|
|
44
|
+
|
|
45
|
+
estate(column: :custom_state_field, empty_initial_state: true) do
|
|
46
|
+
state :state_1
|
|
47
|
+
state :state_2
|
|
48
|
+
state :state_3
|
|
49
|
+
|
|
50
|
+
transition from: :state_1, to: :state_2
|
|
51
|
+
transition from: :state_2, to: :state_3
|
|
52
|
+
transition from: :state_3, to: :state_1
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Migration Example
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
bundle exec rails generate migration AddStateToMyModels state:string
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
This will generate a migration file. Open the migration file and modify it to suit your needs, for example:
|
|
64
|
+
|
|
65
|
+
```ruby
|
|
66
|
+
class AddStateToMyModels < ActiveRecord::Migration[7.0]
|
|
67
|
+
def change
|
|
68
|
+
add_column :my_models, :state, :string
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Run the migration:
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
bundle exec rails db:migrate
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## License
|
|
80
|
+
|
|
81
|
+
Copyright (c) 2023-2024 Igor Korepanov
|
|
82
|
+
|
|
83
|
+
MIT License
|
|
84
|
+
|
|
85
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
86
|
+
|
|
87
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
88
|
+
|
|
89
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/estate/active_record.rb
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
module Estate
|
|
4
4
|
module ActiveRecord
|
|
5
|
-
CALLBACK_NAMES = [:before_validation]
|
|
5
|
+
CALLBACK_NAMES = [:before_validation].freeze
|
|
6
6
|
|
|
7
7
|
def setup_callbacks(base)
|
|
8
8
|
base.class_eval do
|
|
@@ -17,27 +17,19 @@ module Estate
|
|
|
17
17
|
to_state = instance.public_send(Estate::Configuration.column_name)
|
|
18
18
|
|
|
19
19
|
if from_state == to_state
|
|
20
|
-
if to_state.nil?
|
|
21
|
-
|
|
22
|
-
# OK
|
|
23
|
-
else
|
|
24
|
-
raise(StandardError, "empty `#{Estate::Configuration.column_name}` is not allowed")
|
|
25
|
-
end
|
|
20
|
+
if to_state.nil? && !Estate::Configuration.allow_empty_initial_state
|
|
21
|
+
raise(StandardError, "empty `#{Estate::Configuration.column_name}` is not allowed")
|
|
26
22
|
end
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
else
|
|
32
|
-
instance.errors.add(Estate::Configuration.column_name, message: "transition from `#{from_state}` to `#{to_state}` is not allowed")
|
|
33
|
-
end
|
|
34
|
-
else
|
|
35
|
-
instance.errors.add(:base, "state `#{to_state}` is not defined")
|
|
36
|
-
# raise(StandardError, "state `#{to_state}` is not defined")
|
|
23
|
+
elsif Estate::StateMachine.state_exists?(to_state) # TODO: check to_state.nil?
|
|
24
|
+
unless Estate::StateMachine.transition_exists?(from: from_state, to: to_state)
|
|
25
|
+
instance.errors.add(Estate::Configuration.column_name,
|
|
26
|
+
message: "transition from `#{from_state}` to `#{to_state}` is not allowed")
|
|
37
27
|
end
|
|
28
|
+
else
|
|
29
|
+
instance.errors.add(:base, "state `#{to_state}` is not defined")
|
|
38
30
|
end
|
|
39
31
|
end
|
|
40
32
|
|
|
41
33
|
module_function :setup_callbacks, :validate_state_changes
|
|
42
34
|
end
|
|
43
|
-
end
|
|
35
|
+
end
|
data/lib/estate/configuration.rb
CHANGED
data/lib/estate/estate.rb
CHANGED
|
@@ -8,7 +8,6 @@ module Estate
|
|
|
8
8
|
Estate::ActiveRecord.setup_callbacks(base)
|
|
9
9
|
Estate::StateMachine.create_store
|
|
10
10
|
|
|
11
|
-
# TODO: make sure inheritance (aka subclassing) works with AASM
|
|
12
11
|
super
|
|
13
12
|
end
|
|
14
13
|
|
|
@@ -29,12 +28,10 @@ module Estate
|
|
|
29
28
|
end
|
|
30
29
|
|
|
31
30
|
def transition(from:, to:)
|
|
32
|
-
unless Estate::StateMachine.state_exists?(from)
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
raise(StandardError, "state `#{to}` is not defined")
|
|
37
|
-
end
|
|
31
|
+
raise(StandardError, "state `#{from}` is not defined") unless Estate::StateMachine.state_exists?(from)
|
|
32
|
+
|
|
33
|
+
raise(StandardError, "state `#{to}` is not defined") unless Estate::StateMachine.state_exists?(to)
|
|
34
|
+
|
|
38
35
|
if Estate::StateMachine.transition_exists?(from: from, to: to)
|
|
39
36
|
raise(StandardError, "`transition from: :#{from}, to: :#{to}` already defined")
|
|
40
37
|
end
|
data/lib/estate/requirements.rb
CHANGED
|
@@ -3,13 +3,11 @@
|
|
|
3
3
|
module Estate
|
|
4
4
|
module Requirements
|
|
5
5
|
def check_requirements(base)
|
|
6
|
-
ancestors = base.ancestors.map
|
|
6
|
+
ancestors = base.ancestors.map(&:to_s)
|
|
7
7
|
|
|
8
|
-
unless ancestors.include?(
|
|
9
|
-
raise(StandardError, "Estate requires ActiveRecord")
|
|
10
|
-
end
|
|
8
|
+
raise(StandardError, 'Estate requires ActiveRecord') unless ancestors.include?('ActiveRecord::Base')
|
|
11
9
|
end
|
|
12
10
|
|
|
13
11
|
module_function :check_requirements
|
|
14
12
|
end
|
|
15
|
-
end
|
|
13
|
+
end
|
data/lib/estate/state_machine.rb
CHANGED
data/lib/estate/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,17 +1,74 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: estate
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Igor Korepanov
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2024-01-
|
|
12
|
-
dependencies:
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
date: 2024-01-22 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: rubocop
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - '='
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 1.59.0
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - '='
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 1.59.0
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rubocop-performance
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - '='
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: 1.20.2
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - '='
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: 1.20.2
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rubocop-rspec
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - '='
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: 2.26.1
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - '='
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: 2.26.1
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rspec
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - '='
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: 3.12.0
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - '='
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: 3.12.0
|
|
69
|
+
description: Estate is a Ruby gem designed to simplify state management in ActiveRecord
|
|
70
|
+
models
|
|
71
|
+
email: noemail@example.com
|
|
15
72
|
executables: []
|
|
16
73
|
extensions: []
|
|
17
74
|
extra_rdoc_files: []
|
|
@@ -25,7 +82,7 @@ files:
|
|
|
25
82
|
- lib/estate/requirements.rb
|
|
26
83
|
- lib/estate/state_machine.rb
|
|
27
84
|
- lib/estate/version.rb
|
|
28
|
-
homepage: https://
|
|
85
|
+
homepage: https://github.com/igorkorepanov/estate
|
|
29
86
|
licenses:
|
|
30
87
|
- MIT
|
|
31
88
|
metadata: {}
|
|
@@ -37,7 +94,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
37
94
|
requirements:
|
|
38
95
|
- - ">="
|
|
39
96
|
- !ruby/object:Gem::Version
|
|
40
|
-
version: '
|
|
97
|
+
version: '2.7'
|
|
41
98
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
42
99
|
requirements:
|
|
43
100
|
- - ">="
|
|
@@ -47,5 +104,5 @@ requirements: []
|
|
|
47
104
|
rubygems_version: 3.2.22
|
|
48
105
|
signing_key:
|
|
49
106
|
specification_version: 4
|
|
50
|
-
summary:
|
|
107
|
+
summary: State machine for Rails models
|
|
51
108
|
test_files: []
|