state_machina 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 03d12ffeaf652cc4da21dfce6218b807c15234179973298ae52b5201f0439ae6
4
+ data.tar.gz: 06bddb26f70659df01bbc8a9e23cf586e0f616cc26b7ec1c267996a64342b7b2
5
+ SHA512:
6
+ metadata.gz: 771e60029f66d6f0b8ea70c599335356ae9944e05d4148e7b8eccab34c18e5e506c3ee9b6ca69546789830abc0b52482ad26788e5414d66cec3b97e051c33380
7
+ data.tar.gz: 9302926d8356f8858b196a340665e455734d13897c464b4d1cd8a9363aad05a08eaacd3cd378a504b24054cd8c615bcb92a52921a0d5a1ca722563625ab92ec3
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
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
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 3.2.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-03-08
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at jan.vanderpas@beequip.nl. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in state_machina.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
data/Gemfile.lock ADDED
@@ -0,0 +1,64 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ state_machina (0.1.0)
5
+ concurrent-ruby (< 2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ coderay (1.1.3)
12
+ concurrent-ruby (1.1.9)
13
+ diff-lcs (1.5.0)
14
+ method_source (1.0.0)
15
+ parallel (1.21.0)
16
+ parser (3.1.1.0)
17
+ ast (~> 2.4.1)
18
+ pry (0.14.1)
19
+ coderay (~> 1.1)
20
+ method_source (~> 1.0)
21
+ rainbow (3.1.1)
22
+ rake (13.0.6)
23
+ regexp_parser (2.2.1)
24
+ rexml (3.2.5)
25
+ rspec (3.11.0)
26
+ rspec-core (~> 3.11.0)
27
+ rspec-expectations (~> 3.11.0)
28
+ rspec-mocks (~> 3.11.0)
29
+ rspec-core (3.11.0)
30
+ rspec-support (~> 3.11.0)
31
+ rspec-expectations (3.11.0)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.11.0)
34
+ rspec-mocks (3.11.0)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.11.0)
37
+ rspec-support (3.11.0)
38
+ rubocop (1.25.1)
39
+ parallel (~> 1.10)
40
+ parser (>= 3.1.0.0)
41
+ rainbow (>= 2.2.2, < 4.0)
42
+ regexp_parser (>= 1.8, < 3.0)
43
+ rexml
44
+ rubocop-ast (>= 1.15.1, < 2.0)
45
+ ruby-progressbar (~> 1.7)
46
+ unicode-display_width (>= 1.4.0, < 3.0)
47
+ rubocop-ast (1.16.0)
48
+ parser (>= 3.1.1.0)
49
+ ruby-progressbar (1.11.0)
50
+ unicode-display_width (2.1.0)
51
+
52
+ PLATFORMS
53
+ arm64-darwin-21
54
+ x86_64-darwin-20
55
+
56
+ DEPENDENCIES
57
+ pry (< 0.15)
58
+ rake (~> 13.0)
59
+ rspec (~> 3.0)
60
+ rubocop (~> 1.21)
61
+ state_machina!
62
+
63
+ BUNDLED WITH
64
+ 2.3.8
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Jan van der Pas
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,43 @@
1
+ # StateMachina
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/state_machina`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'state_machina'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install state_machina
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/state_machina. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/state_machina/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the StateMachina project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/state_machina/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,20 @@
1
+ module StateMachina
2
+ module Definitions
3
+ def machine(machine_name = 'default', column_name: :state, metadata: {})
4
+ model_name = StateMachina::Util.normalized_klass_to_name(self)
5
+ machine = StateMachina::Machine.new(model_name, machine_name, column_name: column_name, metadata: metadata)
6
+
7
+ yield(StateMachina::Registry.register_machine(machine))
8
+ end
9
+
10
+ alias_method :state_machine, :machine
11
+
12
+ def extend_machine(klass, machine_name = 'default')
13
+ model_name = StateMachina::Util.normalized_klass_to_name(klass)
14
+
15
+ yield(StateMachina::Registry.find_machine!(model_name, machine_name))
16
+ end
17
+
18
+ alias_method :extend_state_machine, :extend_machine
19
+ end
20
+ end
@@ -0,0 +1,48 @@
1
+ module StateMachina
2
+ class Event
3
+ attr_reader :model_name, :machine_name, :name, :metadata
4
+ attr_accessor :machine, :guard_context
5
+ alias_method :state_machine, :machine
6
+
7
+ def initialize(model_name, machine_name, name, metadata: {})
8
+ @model_name = model_name
9
+ @machine_name = machine_name
10
+ @name = name.to_s
11
+ @metadata = metadata
12
+ end
13
+
14
+ def transition(from_state_name, to_state_name, guard: nil, metadata: {})
15
+ machine = StateMachina::Registry.find_machine!(model_name, machine_name)
16
+ transition = StateMachina::Transition.new(model_name, machine.name, name, from_state_name, to_state_name, guard: guard, metadata: metadata)
17
+
18
+ StateMachina::Registry.register_transition(transition)
19
+ end
20
+
21
+ def transitions
22
+ transitions = StateMachina::Registry.find_transitions(model_name, machine_name, name)
23
+ transitions.each do |transition|
24
+ transition.machine = machine
25
+ transition.guard_context = guard_context
26
+ end
27
+ transitions
28
+ end
29
+
30
+ def execute
31
+ resolve_to_transition&.execute
32
+ end
33
+
34
+ def resolve_to_transition
35
+ transitions.find do |transition|
36
+ transition.possible? && transition.permitted?
37
+ end
38
+ end
39
+
40
+ def possible?
41
+ transitions.any?(&:possible?)
42
+ end
43
+
44
+ def permitted?
45
+ transitions.any?(&:permitted?)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,23 @@
1
+ module StateMachina
2
+ class EventsCollection
3
+ include Enumerable
4
+
5
+ def initialize(events)
6
+ @events = events
7
+ end
8
+
9
+ def each(&block)
10
+ if block_given?
11
+ @events.each(&block)
12
+ else
13
+ to_enum(:each)
14
+ end
15
+ end
16
+
17
+ def find_by_name(event_name)
18
+ find do |event|
19
+ event.name == event_name
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,29 @@
1
+ module StateMachina
2
+ module Integration
3
+ def self.included(model)
4
+ model.extend StateMachina::Definitions
5
+ end
6
+
7
+ def machine(machine_name = 'default')
8
+ model_name = StateMachina::Util.normalized_klass_to_name(self.class)
9
+
10
+ machine = StateMachina::Registry.find_machine!(model_name, machine_name)
11
+ machine.model = self
12
+ machine
13
+ end
14
+
15
+ alias_method :state_machine, :machine
16
+
17
+ def machines
18
+ model_name = StateMachina::Util.normalized_klass_to_name(self.class)
19
+
20
+ machines = StateMachina::Registry.find_machines(model_name)
21
+ machines.map do |machine|
22
+ machine.model = self
23
+ machine
24
+ end
25
+ end
26
+
27
+ alias_method :state_machines, :machines
28
+ end
29
+ end
@@ -0,0 +1,69 @@
1
+ module StateMachina
2
+ class Machine
3
+ attr_reader :model_name, :name, :column_name, :metadata
4
+ attr_accessor :model
5
+
6
+ def initialize(model_name, name, column_name: :state, metadata: {})
7
+ @model_name = model_name
8
+ @name = name.to_s
9
+ @column_name = column_name
10
+ @metadata = metadata
11
+ end
12
+
13
+ def state(state_name, metadata: {})
14
+ state = StateMachina::State.new(model_name, name, state_name, metadata: metadata)
15
+
16
+ StateMachina::Registry.register_state(state)
17
+ end
18
+
19
+ def event(event_name, metadata: {})
20
+ event = StateMachina::Event.new(model_name, name, event_name, metadata: metadata)
21
+
22
+ yield(StateMachina::Registry.register_event(event))
23
+ end
24
+
25
+ def states
26
+ states = StateMachina::Registry.find_states(model_name, name).values
27
+ states.each do |state|
28
+ state.machine = self
29
+ end
30
+
31
+ StateMachina::StatesCollection.new(states)
32
+ end
33
+
34
+ def events(possible: false, permitted: false, guard_context: model)
35
+ events = StateMachina::Registry.find_events(model_name, name).values
36
+ events.each do |event|
37
+ event.machine = self
38
+ event.guard_context = guard_context
39
+ end
40
+
41
+ events = select_possible_events(events) if possible || permitted
42
+ events = select_permitted_events(events) if permitted
43
+
44
+ StateMachina::EventsCollection.new(events)
45
+ end
46
+
47
+ def current_state_name
48
+ model.public_send(column_name)
49
+ end
50
+
51
+ def current_state
52
+ states.find_by_name(current_state_name)
53
+ end
54
+
55
+ def update_state(new_state)
56
+ model.public_send("#{column_name}=", new_state)
57
+ end
58
+
59
+ private
60
+
61
+ def select_possible_events(events)
62
+ events.select(&:possible?)
63
+ end
64
+
65
+ def select_permitted_events(events)
66
+ events.select(&:permitted?)
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,71 @@
1
+ module StateMachina
2
+ module Registry
3
+ def self.machines
4
+ @machines ||= Concurrent::Map.new
5
+ end
6
+
7
+ def self.find_machines(model_name)
8
+ machines.fetch(model_name.to_s, {}).values
9
+ end
10
+
11
+ def self.find_machine!(model_name, machine_name)
12
+ machines.fetch(model_name.to_s, {}).fetch(machine_name.to_s) || raise(NoMachineFoundError, "for model: #{model_name}, with name: #{machine_name}")
13
+ end
14
+
15
+ def self.register_machine(machine)
16
+ machines[machine.model_name] ||= {}
17
+ machines[machine.model_name][machine.name] = machine
18
+
19
+ machine
20
+ end
21
+
22
+ def self.states
23
+ @states ||= Concurrent::Map.new
24
+ end
25
+
26
+ def self.find_states(model_name, machine_name)
27
+ states.fetch(model_name.to_s, {}).fetch(machine_name.to_s, [])
28
+ end
29
+
30
+ def self.register_state(state)
31
+ states[state.model_name] ||= {}
32
+ states[state.model_name][state.machine_name] ||= {}
33
+ states[state.model_name][state.machine_name][state.name] = state
34
+
35
+ state
36
+ end
37
+
38
+ def self.events
39
+ @events ||= Concurrent::Map.new
40
+ end
41
+
42
+ def self.find_events(model_name, machine_name)
43
+ events.fetch(model_name.to_s, {}).fetch(machine_name.to_s, {})
44
+ end
45
+
46
+ def self.register_event(event)
47
+ events[event.model_name] ||= {}
48
+ events[event.model_name][event.machine_name] ||= {}
49
+ events[event.model_name][event.machine_name][event.name] = event
50
+
51
+ event
52
+ end
53
+
54
+ def self.transitions
55
+ @transitions ||= Concurrent::Map.new
56
+ end
57
+
58
+ def self.find_transitions(model_name, machine_name, event_name)
59
+ transitions.fetch(model_name.to_s, {}).fetch(machine_name.to_s, {}).fetch(event_name.to_s, [])
60
+ end
61
+
62
+ def self.register_transition(transition)
63
+ transitions[transition.model_name] ||= {}
64
+ transitions[transition.model_name][transition.machine_name] ||= {}
65
+ transitions[transition.model_name][transition.machine_name][transition.event_name] ||= []
66
+ transitions[transition.model_name][transition.machine_name][transition.event_name].append(transition)
67
+
68
+ transition
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,49 @@
1
+ module StateMachina
2
+ class State
3
+ attr_reader :model_name, :machine_name, :name, :metadata
4
+ attr_accessor :machine
5
+
6
+ def initialize(model_name, machine_name, name, metadata: {})
7
+ @model_name = model_name
8
+ @machine_name = machine_name
9
+ @name = name.to_s
10
+ @metadata = metadata
11
+ end
12
+
13
+ def before?(other_state_name)
14
+ return false if machine.nil?
15
+
16
+ !machine.states.before(machine.current_state_name).map(&:name).include?(other_state_name)
17
+ end
18
+
19
+ def before_inclusive?(other_state_name)
20
+ return false if machine.nil?
21
+
22
+ !machine.states.before_inclusive(machine.current_state_name).map(&:name).include?(other_state_name)
23
+ end
24
+
25
+ def past?(other_state_name)
26
+ return false if machine.nil?
27
+
28
+ !machine.states.past(machine.current_state_name).map(&:name).include?(other_state_name)
29
+ end
30
+
31
+ def past_inclusive?(other_state_name)
32
+ return false if machine.nil?
33
+
34
+ !machine.states.past_inclusive(machine.current_state_name).map(&:name).include?(other_state_name)
35
+ end
36
+
37
+ def between?(from_state_name, to_state_name)
38
+ return false if machine.nil?
39
+
40
+ machine.states.between(from_state_name, to_state_name).map(&:name).include?(machine.current_state_name)
41
+ end
42
+
43
+ def between_inclusive?(from_state_name, to_state_name)
44
+ return false if machine.nil?
45
+
46
+ machine.states.between_inclusive(from_state_name, to_state_name).map(&:name).include?(machine.current_state_name)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,57 @@
1
+ module StateMachina
2
+ class StatesCollection
3
+ include Enumerable
4
+
5
+ def initialize(states)
6
+ @states = states
7
+ end
8
+
9
+ def each(&block)
10
+ if block_given?
11
+ @states.each(&block)
12
+ else
13
+ to_enum(:each)
14
+ end
15
+ end
16
+
17
+ def find_by_name(state_name)
18
+ find do |state|
19
+ state.name == state_name.to_s
20
+ end
21
+ end
22
+
23
+ def before(state_name)
24
+ to_a[..(index_by_name(state_name) - 1)]
25
+ end
26
+
27
+ def before_inclusive(state_name)
28
+ to_a[..index_by_name(state_name)]
29
+ end
30
+
31
+ def past(state_name)
32
+ to_a[(index_by_name(state_name) + 1)..]
33
+ end
34
+
35
+ def past_inclusive(state_name)
36
+ to_a[index_by_name(state_name)..]
37
+ end
38
+
39
+ def between(from_state_name, to_state_name)
40
+ to_a[index_by_name(from_state_name)...index_by_name(to_state_name)]
41
+ end
42
+
43
+ def between_inclusive(from_state_name, to_state_name)
44
+ to_a[index_by_name(from_state_name)..index_by_name(to_state_name)]
45
+ end
46
+
47
+ private
48
+
49
+ def index_by_name(name)
50
+ listed_by_name.index(name.to_s)
51
+ end
52
+
53
+ def listed_by_name
54
+ @listed_by_name ||= map(&:name)
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,47 @@
1
+ module StateMachina
2
+ class Transition
3
+ attr_reader :model_name, :machine_name, :event_name, :from_state_name, :to_state_name, :guard, :metadata
4
+ attr_accessor :machine, :guard_context
5
+ alias_method :state_machine, :machine
6
+
7
+ def initialize(model_name, machine_name, event_name, from_state_name, to_state_name, guard: nil, metadata: {})
8
+ @model_name = model_name
9
+ @machine_name = machine_name
10
+ @event_name = event_name
11
+ @from_state_name = from_state_name.to_s
12
+ @to_state_name = to_state_name.to_s
13
+ @guard = guard
14
+ @metadata = metadata
15
+ end
16
+
17
+ def possible?
18
+ return false if machine.nil?
19
+
20
+ from_state_name == machine.current_state_name
21
+ end
22
+
23
+ def permitted?
24
+ return true if guard.nil?
25
+ return false if guard_context.nil?
26
+
27
+ case guard
28
+ when Symbol
29
+ guard_context.public_send(guard) # Supports `guard: :meat?`
30
+ when Proc
31
+ if guard.arity >= 1
32
+ guard.call(guard_context) # Supports `guard: (model) -> { model.meat? }`
33
+ else
34
+ guard_context.instance_exec(&guard) # Supports `guard: -> { meat? }`
35
+ end
36
+ else
37
+ guard.call(guard_context) if guard.respond_to?(:call) # Supports `guard: CallableObject`
38
+ end
39
+ end
40
+
41
+ def execute
42
+ return false if machine.nil?
43
+
44
+ machine.update_state(to_state_name)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,30 @@
1
+ module StateMachina
2
+ module Util
3
+ def self.normalized_klass_to_name(klass)
4
+ underscore(demodulize(klass.name)).to_s
5
+ end
6
+
7
+ # Based on the implementation of ActiveSupport::Inflector#demodulize
8
+ def self.demodulize(path)
9
+ path = path.to_s
10
+
11
+ if i = path.rindex("::")
12
+ path[(i + 2)..-1]
13
+ else
14
+ path
15
+ end
16
+ end
17
+
18
+ # Based on the implementation of ActiveSupport::Inflector#underscore
19
+ def self.underscore(camel_cased_word)
20
+ return camel_cased_word unless /[A-Z-]|::/.match?(camel_cased_word)
21
+
22
+ word = camel_cased_word.to_s.gsub("::", "/")
23
+ word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
24
+ word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
25
+ word.tr!("-", "_")
26
+ word.downcase!
27
+ word
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StateMachina
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "concurrent"
4
+
5
+ require_relative "state_machina/definitions"
6
+ require_relative "state_machina/event"
7
+ require_relative "state_machina/events_collection"
8
+ require_relative "state_machina/integration"
9
+ require_relative "state_machina/machine"
10
+ require_relative "state_machina/registry"
11
+ require_relative "state_machina/state"
12
+ require_relative "state_machina/states_collection"
13
+ require_relative "state_machina/transition"
14
+ require_relative "state_machina/util"
15
+ require_relative "state_machina/version"
16
+
17
+ module StateMachina
18
+ class Error < StandardError; end
19
+
20
+ class NoMachineFoundError < Error; end
21
+ end
@@ -0,0 +1,4 @@
1
+ module StateMachina
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: state_machina
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jan van der Pas
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-10-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: concurrent-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "<"
18
+ - !ruby/object:Gem::Version
19
+ version: '2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "<"
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "<"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "<"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.15'
41
+ description:
42
+ email:
43
+ - jan.vanderpas@beequip.nl
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rspec"
49
+ - ".rubocop.yml"
50
+ - ".tool-versions"
51
+ - CHANGELOG.md
52
+ - CODE_OF_CONDUCT.md
53
+ - Gemfile
54
+ - Gemfile.lock
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - lib/state_machina.rb
59
+ - lib/state_machina/definitions.rb
60
+ - lib/state_machina/event.rb
61
+ - lib/state_machina/events_collection.rb
62
+ - lib/state_machina/integration.rb
63
+ - lib/state_machina/machine.rb
64
+ - lib/state_machina/registry.rb
65
+ - lib/state_machina/state.rb
66
+ - lib/state_machina/states_collection.rb
67
+ - lib/state_machina/transition.rb
68
+ - lib/state_machina/util.rb
69
+ - lib/state_machina/version.rb
70
+ - sig/state_machina.rbs
71
+ homepage:
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: 2.6.0
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubygems_version: 3.4.21
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: State machine supporting multiple machines per object
94
+ test_files: []