activerecord-fsm 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +61 -0
- data/LICENSE.txt +21 -0
- data/README.md +91 -0
- data/Rakefile +6 -0
- data/activerecord-fsm.gemspec +30 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/activerecord/fsm.rb +34 -0
- data/lib/activerecord/fsm/graph.rb +29 -0
- data/lib/activerecord/fsm/version.rb +5 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b281902b922a683073baf632654a0657f7e9bdbb3c3348960491432428a68444
|
4
|
+
data.tar.gz: 157d440eb10fd597a283e677c6f698c2cf5b76e3888599a0d26d960678132eca
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 696e9bc135f6c6b755102e5fb535a941bebf6d9e1fd224312673b889bb9ee1628b7551aa1cc939fc520b9570f9eff8495aa7470b3946e87dcbb2a2a5595c1524
|
7
|
+
data.tar.gz: e9d4c60a0d9eed133867c619bdf4558cdbb31766f225c3d892f55f5a76ed2eb691f745c8ae7b468d1aa91b0e9cb4001cf634788735acb5bd918ceaa6e8690a30
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
activerecord-fsm (0.1.1)
|
5
|
+
activerecord (>= 5.2, < 7.0)
|
6
|
+
activesupport (>= 5.2, < 7.0)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activemodel (6.0.3)
|
12
|
+
activesupport (= 6.0.3)
|
13
|
+
activerecord (6.0.3)
|
14
|
+
activemodel (= 6.0.3)
|
15
|
+
activesupport (= 6.0.3)
|
16
|
+
activesupport (6.0.3)
|
17
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
18
|
+
i18n (>= 0.7, < 2)
|
19
|
+
minitest (~> 5.1)
|
20
|
+
tzinfo (~> 1.1)
|
21
|
+
zeitwerk (~> 2.2, >= 2.2.2)
|
22
|
+
coderay (1.1.3)
|
23
|
+
concurrent-ruby (1.1.6)
|
24
|
+
diff-lcs (1.4.4)
|
25
|
+
i18n (1.8.5)
|
26
|
+
concurrent-ruby (~> 1.0)
|
27
|
+
method_source (1.0.0)
|
28
|
+
minitest (5.14.1)
|
29
|
+
pry (0.13.1)
|
30
|
+
coderay (~> 1.1)
|
31
|
+
method_source (~> 1.0)
|
32
|
+
rake (12.3.3)
|
33
|
+
rspec (3.9.0)
|
34
|
+
rspec-core (~> 3.9.0)
|
35
|
+
rspec-expectations (~> 3.9.0)
|
36
|
+
rspec-mocks (~> 3.9.0)
|
37
|
+
rspec-core (3.9.2)
|
38
|
+
rspec-support (~> 3.9.3)
|
39
|
+
rspec-expectations (3.9.2)
|
40
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
41
|
+
rspec-support (~> 3.9.0)
|
42
|
+
rspec-mocks (3.9.1)
|
43
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
44
|
+
rspec-support (~> 3.9.0)
|
45
|
+
rspec-support (3.9.3)
|
46
|
+
thread_safe (0.3.6)
|
47
|
+
tzinfo (1.2.7)
|
48
|
+
thread_safe (~> 0.1)
|
49
|
+
zeitwerk (2.3.0)
|
50
|
+
|
51
|
+
PLATFORMS
|
52
|
+
ruby
|
53
|
+
|
54
|
+
DEPENDENCIES
|
55
|
+
activerecord-fsm!
|
56
|
+
pry (~> 0.10)
|
57
|
+
rake (~> 12.0)
|
58
|
+
rspec (~> 3.0)
|
59
|
+
|
60
|
+
BUNDLED WITH
|
61
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 ElliotCui
|
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,91 @@
|
|
1
|
+
# ActiveRecord::Fsm
|
2
|
+
|
3
|
+
A simple Finite State Machine.
|
4
|
+
Use this gem to auto define Finite State Machine on ActiveRecord with one column named `status`.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'activerecord-fsm'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle install
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install activerecord-fsm
|
21
|
+
|
22
|
+
Maybe you need to do migration, add one column named `status`:
|
23
|
+
|
24
|
+
bin/rails generate migration AddStatusToFsmModel status:integer:index
|
25
|
+
# or you can specify column type
|
26
|
+
bin/rails generate migration AddStatusToFsmModel status:{column_type}:index
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
1. Include `ActiveRecord::Fsm` into `ApplicationRecord/ActiveRecord::Base` or directly into `FsmModel`.
|
31
|
+
2. Use `#fsm_define` to arrange permit_transitions set in which status can be transitioned from one to another. This will add:
|
32
|
+
|
33
|
+
validates :status, presence: true, inclusion: { in: self.fsm_graph.states }
|
34
|
+
validate :fsm_graph_check_permit_transitions, on: :update, if: :will_save_change_to_status?
|
35
|
+
|
36
|
+
`fsm_graph_check_permit_transitions` has been defined as:
|
37
|
+
```
|
38
|
+
def fsm_graph_check_permit_transitions
|
39
|
+
unless self.class.fsm_graph.valid_transition?(*self.status_change)
|
40
|
+
self.errors.add(:status, 'no permit status change')
|
41
|
+
throw :abort
|
42
|
+
end
|
43
|
+
end
|
44
|
+
```
|
45
|
+
**Model without using `fsm_define` will only act as the same with `NomalModel` which doesn't has fsm mechanic.**
|
46
|
+
In other words, using `fsm_define` will make `NomalModel` become `FsmModel`.
|
47
|
+
|
48
|
+
3. Use `FsmModel.fsm_graph.states` to get permit_transition you defined.
|
49
|
+
4. Use `ActiveRecord::Fsm::Graph.defined_klasses` to get all the models which really used `ActiveRecord::Fsm`.
|
50
|
+
|
51
|
+
e.g.
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
class FsmModel < ApplicationRecord
|
55
|
+
STATE_1 = 1
|
56
|
+
STATE_2 = 2
|
57
|
+
STATE_3 = 3
|
58
|
+
|
59
|
+
PERMIT_TRANSITIONS = [
|
60
|
+
[STATE_1, STATE_2],
|
61
|
+
[STATE_2, STATE_3],
|
62
|
+
[STATE_1, STATE_3],
|
63
|
+
]
|
64
|
+
|
65
|
+
fsm_define(PERMIT_TRANSITIONS)
|
66
|
+
end
|
67
|
+
|
68
|
+
ActiveRecord::Fsm::Graph.defined_klasses
|
69
|
+
# => [FsmModel]
|
70
|
+
|
71
|
+
class NormalModel < ApplicationRecord
|
72
|
+
end
|
73
|
+
|
74
|
+
ActiveRecord::Fsm::Graph.defined_klasses
|
75
|
+
# => [FsmModel]
|
76
|
+
```
|
77
|
+
|
78
|
+
## Development
|
79
|
+
|
80
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
81
|
+
|
82
|
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
83
|
+
|
84
|
+
## Contributing
|
85
|
+
|
86
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/activerecord-fsm.
|
87
|
+
|
88
|
+
|
89
|
+
## License
|
90
|
+
|
91
|
+
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,30 @@
|
|
1
|
+
require_relative 'lib/activerecord/fsm/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "activerecord-fsm"
|
5
|
+
spec.version = ActiveRecord::Fsm::VERSION
|
6
|
+
spec.authors = ["ElliotCui"]
|
7
|
+
spec.email = ["cuiyi.plus@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = "A simple Finite State Machine."
|
10
|
+
spec.description = "Use this gem to auto define Finite State Machine on ActiveRecord with one column named `status`."
|
11
|
+
spec.homepage = "https://github.com/ElliotCui/activerecord-fsm"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_dependency "activerecord", ">= 5.2", "< 7.0"
|
27
|
+
spec.add_dependency "activesupport", ">= 5.2", "< 7.0"
|
28
|
+
|
29
|
+
spec.add_development_dependency 'pry', '~> 0.10'
|
30
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "activerecord/fsm"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require "activerecord/fsm/graph"
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module Fsm
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
attr_accessor :fsm_graph
|
9
|
+
|
10
|
+
def fsm_define(transitions)
|
11
|
+
self.class_eval do
|
12
|
+
private
|
13
|
+
|
14
|
+
def fsm_graph_check_permit_transitions
|
15
|
+
unless self.class.fsm_graph.valid_transition?(*self.status_change)
|
16
|
+
self.errors.add(:status, 'no permit status change')
|
17
|
+
throw :abort
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
ActiveRecord::Fsm::Graph.defined_klasses << self
|
23
|
+
self.fsm_graph = ActiveRecord::Fsm::Graph.new(transitions)
|
24
|
+
|
25
|
+
validates :status, presence: true, inclusion: { in: self.fsm_graph.states }
|
26
|
+
validate :fsm_graph_check_permit_transitions, on: :update, if: :will_save_change_to_status?
|
27
|
+
|
28
|
+
self.fsm_graph
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class Error < StandardError; end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Fsm
|
3
|
+
class Graph
|
4
|
+
attr_accessor :transitions, :states, :final_states
|
5
|
+
|
6
|
+
def initialize(transitions)
|
7
|
+
@transitions = transitions
|
8
|
+
@states = transitions.flatten.uniq
|
9
|
+
@final_states = transitions.map(&:last) - transitions.map(&:first)
|
10
|
+
end
|
11
|
+
|
12
|
+
def valid_transition?(cur_state, tar_state)
|
13
|
+
transitions.include?([cur_state, tar_state])
|
14
|
+
end
|
15
|
+
|
16
|
+
def final_state?(state)
|
17
|
+
final_states.include?(state)
|
18
|
+
end
|
19
|
+
|
20
|
+
class << self
|
21
|
+
attr_writer :defined_klasses
|
22
|
+
|
23
|
+
def defined_klasses
|
24
|
+
@defined_klasses ||= []
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-fsm
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ElliotCui
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-10-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.2'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '7.0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '5.2'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '7.0'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: activesupport
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '5.2'
|
40
|
+
- - "<"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '7.0'
|
43
|
+
type: :runtime
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '5.2'
|
50
|
+
- - "<"
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '7.0'
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: pry
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - "~>"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0.10'
|
60
|
+
type: :development
|
61
|
+
prerelease: false
|
62
|
+
version_requirements: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - "~>"
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0.10'
|
67
|
+
description: Use this gem to auto define Finite State Machine on ActiveRecord with
|
68
|
+
one column named `status`.
|
69
|
+
email:
|
70
|
+
- cuiyi.plus@gmail.com
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files: []
|
74
|
+
files:
|
75
|
+
- ".gitignore"
|
76
|
+
- ".rspec"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- Gemfile.lock
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- activerecord-fsm.gemspec
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- lib/activerecord/fsm.rb
|
87
|
+
- lib/activerecord/fsm/graph.rb
|
88
|
+
- lib/activerecord/fsm/version.rb
|
89
|
+
homepage: https://github.com/ElliotCui/activerecord-fsm
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata:
|
93
|
+
homepage_uri: https://github.com/ElliotCui/activerecord-fsm
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: 2.3.0
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubygems_version: 3.0.8
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: A simple Finite State Machine.
|
113
|
+
test_files: []
|