mini-aasm 0.1.0.pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/main.yml +18 -0
- data/.gitignore +8 -0
- data/.rubocop.yml +38 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +62 -0
- data/README.md +82 -0
- data/Rakefile +25 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/mini-aasm.rb +93 -0
- data/lib/mini_aasm/version.rb +5 -0
- data/mini-aasm.gemspec +34 -0
- metadata +59 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 84be5609986ebac0b4a28f3ac8ee3e484b74e230926c2ca9315c11099f074e09
|
4
|
+
data.tar.gz: 51c35e5155e38b8f1386191c684d2e8a2ad2962b4a9cd96b6aa1e38e4e21c7a1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9e82f17fc45134a2698fe728700305b60d30460e2ba2ef03386490c7e1df0ee0283e83a35d0c794884953393ce782750b3873332f2e1c8d759dabbffa160a306
|
7
|
+
data.tar.gz: d2db46d854bc7e62bc73f61499be1255a55c4ec8c791266e7d4cd07f6af101d393d3c86803b41033d3d44e49aebf7cabc0b81c73797888638b597e393c8186f9
|
@@ -0,0 +1,18 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on: [push,pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
runs-on: ubuntu-latest
|
8
|
+
steps:
|
9
|
+
- uses: actions/checkout@v2
|
10
|
+
- name: Set up Ruby
|
11
|
+
uses: ruby/setup-ruby@v1
|
12
|
+
with:
|
13
|
+
ruby-version: 2.5.3
|
14
|
+
- name: Run the default task
|
15
|
+
run: |
|
16
|
+
gem install bundler -v 2.2.3
|
17
|
+
bundle install
|
18
|
+
bundle exec rake
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.5
|
3
|
+
|
4
|
+
Style/StringLiterals:
|
5
|
+
Enabled: true
|
6
|
+
EnforcedStyle: double_quotes
|
7
|
+
|
8
|
+
Style/StringLiteralsInInterpolation:
|
9
|
+
Enabled: true
|
10
|
+
EnforcedStyle: double_quotes
|
11
|
+
|
12
|
+
Layout/LineLength:
|
13
|
+
Max: 120
|
14
|
+
Exclude:
|
15
|
+
- mini-aasm.gemspec
|
16
|
+
|
17
|
+
Metrics/CyclomaticComplexity:
|
18
|
+
Max: 10
|
19
|
+
|
20
|
+
Metrics/AbcSize:
|
21
|
+
Max: 30
|
22
|
+
|
23
|
+
Metrics/MethodLength:
|
24
|
+
Max: 20
|
25
|
+
|
26
|
+
Metrics/BlockLength:
|
27
|
+
Max: 50
|
28
|
+
|
29
|
+
Style/Documentation:
|
30
|
+
Enabled: false
|
31
|
+
|
32
|
+
Naming/FileName:
|
33
|
+
Exclude:
|
34
|
+
- lib/mini-aasm.rb
|
35
|
+
|
36
|
+
Lint/UnderscorePrefixedVariableName:
|
37
|
+
Exclude:
|
38
|
+
- lib/mini-aasm.rb
|
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in mini-aasm.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "minitest"
|
9
|
+
gem "minitest-reporters"
|
10
|
+
gem "pry", require: true
|
11
|
+
gem "rake", "~> 13.0"
|
12
|
+
gem "rubocop", "~> 0.80"
|
13
|
+
gem "rubocop-minitest", require: false
|
14
|
+
gem "rubocop-rake", require: false
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
mini-aasm (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ansi (1.5.0)
|
10
|
+
ast (2.4.2)
|
11
|
+
builder (3.2.4)
|
12
|
+
coderay (1.1.3)
|
13
|
+
method_source (1.0.0)
|
14
|
+
minitest (5.15.0)
|
15
|
+
minitest-reporters (1.5.0)
|
16
|
+
ansi
|
17
|
+
builder
|
18
|
+
minitest (>= 5.0)
|
19
|
+
ruby-progressbar
|
20
|
+
parallel (1.22.1)
|
21
|
+
parser (3.2.0.0)
|
22
|
+
ast (~> 2.4.1)
|
23
|
+
pry (0.14.2)
|
24
|
+
coderay (~> 1.1)
|
25
|
+
method_source (~> 1.0)
|
26
|
+
rainbow (3.1.1)
|
27
|
+
rake (13.0.6)
|
28
|
+
regexp_parser (2.6.2)
|
29
|
+
rexml (3.2.5)
|
30
|
+
rubocop (0.93.1)
|
31
|
+
parallel (~> 1.10)
|
32
|
+
parser (>= 2.7.1.5)
|
33
|
+
rainbow (>= 2.2.2, < 4.0)
|
34
|
+
regexp_parser (>= 1.8)
|
35
|
+
rexml
|
36
|
+
rubocop-ast (>= 0.6.0)
|
37
|
+
ruby-progressbar (~> 1.7)
|
38
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
39
|
+
rubocop-ast (1.17.0)
|
40
|
+
parser (>= 3.1.1.0)
|
41
|
+
rubocop-minitest (0.19.1)
|
42
|
+
rubocop (>= 0.90, < 2.0)
|
43
|
+
rubocop-rake (0.5.1)
|
44
|
+
rubocop
|
45
|
+
ruby-progressbar (1.11.0)
|
46
|
+
unicode-display_width (1.8.0)
|
47
|
+
|
48
|
+
PLATFORMS
|
49
|
+
x86_64-linux
|
50
|
+
|
51
|
+
DEPENDENCIES
|
52
|
+
mini-aasm!
|
53
|
+
minitest
|
54
|
+
minitest-reporters
|
55
|
+
pry
|
56
|
+
rake (~> 13.0)
|
57
|
+
rubocop (~> 0.80)
|
58
|
+
rubocop-minitest
|
59
|
+
rubocop-rake
|
60
|
+
|
61
|
+
BUNDLED WITH
|
62
|
+
2.2.3
|
data/README.md
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
# MiniAASM
|
2
|
+
|
3
|
+
A State Machine library intended to be compatible with lightweight implementations of the Ruby language using 100LOC and only standard libraries. Inspired by [Heroku Postgres State Machines](https://www.citusdata.com/blog/2016/08/12/state-machines-to-run-databases/).
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
class PeriodicJob
|
9
|
+
include MiniAASM
|
10
|
+
|
11
|
+
aasm do
|
12
|
+
state :waiting, initial: true
|
13
|
+
state :executing
|
14
|
+
state :terminated
|
15
|
+
|
16
|
+
event :work_succeeded do
|
17
|
+
transitions from: :executing, to: :waiting
|
18
|
+
transitions from: :waiting, to: :executing, guard: %i[ready?]
|
19
|
+
end
|
20
|
+
|
21
|
+
event :work_failed do
|
22
|
+
transitions from: %i[waiting executing], to: :terminated
|
23
|
+
end
|
24
|
+
|
25
|
+
# ...
|
26
|
+
end
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
_See [test/support/periodic_job.rb](test/support/periodic_job.rb)._
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
> job = PeriodicJob.new
|
34
|
+
=> #<PeriodicJob:0x000055de0c03ec98>
|
35
|
+
> job.current_state
|
36
|
+
=> :waiting
|
37
|
+
job.work_succeeded!
|
38
|
+
=> :executing
|
39
|
+
> job.work_succeeded!
|
40
|
+
=> :waiting
|
41
|
+
> job.work_failed!
|
42
|
+
=> :terminated
|
43
|
+
```
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
> job = PeriodicJob.new(ready: false)
|
47
|
+
=> #<PeriodicJob:0x000055de0c11e528>
|
48
|
+
> job.work_succeeded!
|
49
|
+
MiniAASM::InvalidTransition: MiniAASM::InvalidTransition
|
50
|
+
```
|
51
|
+
|
52
|
+
## Best Practice
|
53
|
+
|
54
|
+
1. The state machine should be trying to converge an eventually consistent end state which looks like a status.
|
55
|
+
2. States should be separated into atomic units of work.
|
56
|
+
3. State transitions should be invoked in an idempotent way.
|
57
|
+
|
58
|
+
## Installation
|
59
|
+
|
60
|
+
Add this line to your application's Gemfile:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
gem 'mini-aasm'
|
64
|
+
```
|
65
|
+
|
66
|
+
And then execute:
|
67
|
+
|
68
|
+
$ bundle install
|
69
|
+
|
70
|
+
Or install it yourself as:
|
71
|
+
|
72
|
+
$ gem install mini-aasm
|
73
|
+
|
74
|
+
## Development
|
75
|
+
|
76
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
77
|
+
|
78
|
+
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).
|
79
|
+
|
80
|
+
## Contributing
|
81
|
+
|
82
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mini-aasm.
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
|
5
|
+
# Test
|
6
|
+
|
7
|
+
require "rake/testtask"
|
8
|
+
|
9
|
+
Rake::TestTask.new do |task|
|
10
|
+
task.libs << "test" # require File.expand_path('test/test_helper', File.dirname(__FILE__))
|
11
|
+
task.pattern = "test/*_test.rb"
|
12
|
+
end
|
13
|
+
|
14
|
+
# Lint
|
15
|
+
|
16
|
+
require "rubocop/rake_task"
|
17
|
+
|
18
|
+
RuboCop::RakeTask.new do |task|
|
19
|
+
task.requires << "rubocop-rake"
|
20
|
+
task.requires << "rubocop-minitest"
|
21
|
+
end
|
22
|
+
|
23
|
+
# Default
|
24
|
+
|
25
|
+
task default: %i[test rubocop]
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "mini-aasm"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/mini-aasm.rb
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "mini_aasm/version"
|
4
|
+
|
5
|
+
module MiniAASM
|
6
|
+
class InvalidTransition < RuntimeError; end
|
7
|
+
class UndefinedState < RuntimeError; end
|
8
|
+
|
9
|
+
class Configuration
|
10
|
+
module DSL
|
11
|
+
module ClassMethods
|
12
|
+
def aasm(&block)
|
13
|
+
config = Configuration.new(self)
|
14
|
+
config.instance_eval(&block)
|
15
|
+
config.configure!
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class StateTransitions < Array
|
20
|
+
attr_reader :name
|
21
|
+
|
22
|
+
def initialize(name)
|
23
|
+
@name = name
|
24
|
+
|
25
|
+
super()
|
26
|
+
end
|
27
|
+
|
28
|
+
def transitions(from:, to:, guard: [])
|
29
|
+
self << [[from].flatten, to, [guard].flatten]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def state(name, opts = {})
|
34
|
+
states[name] = opts
|
35
|
+
end
|
36
|
+
|
37
|
+
def event(name, &block)
|
38
|
+
event = StateTransitions.new(name)
|
39
|
+
event.instance_eval(&block)
|
40
|
+
events[name] = event
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
include DSL
|
45
|
+
|
46
|
+
attr_reader :klass, :states, :events
|
47
|
+
|
48
|
+
def initialize(klass)
|
49
|
+
@klass = klass
|
50
|
+
@states = {}
|
51
|
+
@events = {}
|
52
|
+
end
|
53
|
+
|
54
|
+
def configure!
|
55
|
+
_aasm = self
|
56
|
+
|
57
|
+
klass.define_method(:_aasm) { _aasm }
|
58
|
+
|
59
|
+
klass.define_method(:states) do
|
60
|
+
_aasm.states.keys
|
61
|
+
end
|
62
|
+
|
63
|
+
klass.define_method(:current_state) do
|
64
|
+
@current_state ||= _aasm.initial_state
|
65
|
+
end
|
66
|
+
|
67
|
+
klass.define_method(:set_current_state) do |state|
|
68
|
+
raise UndefinedState unless states.include?(state)
|
69
|
+
|
70
|
+
@current_state = state
|
71
|
+
end
|
72
|
+
|
73
|
+
events.each do |(name, event)|
|
74
|
+
klass.define_method(:"#{name}!") do
|
75
|
+
transitions = event.select { |(from_states, _)| from_states.include?(current_state) }
|
76
|
+
_, to_state = transitions.find { |(_, _, guards)| guards.all? { |guard| send(guard) } }
|
77
|
+
|
78
|
+
raise InvalidTransition unless to_state
|
79
|
+
|
80
|
+
set_current_state to_state
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def initial_state
|
86
|
+
states.find { |(_name, opts)| opts[:initial] }.first
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.included(klass)
|
91
|
+
klass.extend Configuration::DSL::ClassMethods
|
92
|
+
end
|
93
|
+
end
|
data/mini-aasm.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/mini_aasm/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "mini-aasm"
|
7
|
+
spec.version = MiniAASM::VERSION
|
8
|
+
spec.authors = ["James Moriarty"]
|
9
|
+
spec.email = ["jamespaulmoriarty@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "A State Machine library intended to be compatible with lightweight implementations of the Ruby language."
|
12
|
+
spec.description = spec.summary
|
13
|
+
spec.homepage = "https://github.com/jamesmoriarty/mini-aasm"
|
14
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = "https://github.com/jamesmoriarty/mini-aasm"
|
18
|
+
spec.metadata["changelog_uri"] = "https://github.com/jamesmoriarty/mini-aasm/CHANGELOG.md"
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
24
|
+
end
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
# Uncomment to register a new dependency of your gem
|
30
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
31
|
+
|
32
|
+
# For more information and examples about making a new gem, checkout our
|
33
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mini-aasm
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0.pre
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- James Moriarty
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-01-25 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A State Machine library intended to be compatible with lightweight implementations
|
14
|
+
of the Ruby language.
|
15
|
+
email:
|
16
|
+
- jamespaulmoriarty@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".github/workflows/main.yml"
|
22
|
+
- ".gitignore"
|
23
|
+
- ".rubocop.yml"
|
24
|
+
- Gemfile
|
25
|
+
- Gemfile.lock
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- bin/console
|
29
|
+
- bin/setup
|
30
|
+
- lib/mini-aasm.rb
|
31
|
+
- lib/mini_aasm/version.rb
|
32
|
+
- mini-aasm.gemspec
|
33
|
+
homepage: https://github.com/jamesmoriarty/mini-aasm
|
34
|
+
licenses: []
|
35
|
+
metadata:
|
36
|
+
homepage_uri: https://github.com/jamesmoriarty/mini-aasm
|
37
|
+
source_code_uri: https://github.com/jamesmoriarty/mini-aasm
|
38
|
+
changelog_uri: https://github.com/jamesmoriarty/mini-aasm/CHANGELOG.md
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.5.0
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">"
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 1.3.1
|
53
|
+
requirements: []
|
54
|
+
rubygems_version: 3.2.3
|
55
|
+
signing_key:
|
56
|
+
specification_version: 4
|
57
|
+
summary: A State Machine library intended to be compatible with lightweight implementations
|
58
|
+
of the Ruby language.
|
59
|
+
test_files: []
|