tiny_state 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/.tool-versions +1 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/LICENSE.txt +21 -0
- data/README.md +246 -0
- data/Rakefile +6 -0
- data/config/quickdraw.rb +3 -0
- data/lib/tiny_state/errors.rb +3 -0
- data/lib/tiny_state/event.rb +35 -0
- data/lib/tiny_state/state_machine.rb +43 -0
- data/lib/tiny_state/tiny_state.rb +29 -0
- data/lib/tiny_state/version.rb +5 -0
- data/lib/tiny_state.rb +5 -0
- data/tiny_state.gemspec +34 -0
- metadata +73 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 312b61f20ffa06f30ae9c77de4eaf9839b5ccf6338a869425f05f7c871b7f55e
|
4
|
+
data.tar.gz: 7229d0a178cf65d3b6f1b8f1ff8419400318915d2479a4fabb5703f223f8647d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 51f3601a4a992b0537f3de0ebf3d16c3be84bf2bbbdfff635489cb0a7f2cf6cf971e8fcb6ee90cdd7a7ad3d3653f45ba59e4949ad9015d9c9d8cd5eb03748a41
|
7
|
+
data.tar.gz: 3ef88f8ea58f9d1ffa2940fd83a5b419cbe9518a23a7c5bd91f4aabb3c77b2da856beb2f7fcb627f8fc4fa44022a134d66b219fd6b7fdb4dd7c7e247700352d6
|
data/.standard.yml
ADDED
data/.tool-versions
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby 3.3.1
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -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 dennis@paagman.dev. 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/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Dennis Paagman
|
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,246 @@
|
|
1
|
+
# Tiny State
|
2
|
+
|
3
|
+
[![CI](https://github.com/djfpaagman/tiny_state/actions/workflows/ci.yml/badge.svg)](https://github.com/djfpaagman/tiny_state/actions/workflows/ci.yml)
|
4
|
+
|
5
|
+
> [!WARNING]
|
6
|
+
> Tiny State is currently in early development. Use at your own risk!
|
7
|
+
|
8
|
+
Tiny State is a library to add a state machine to any Ruby class. It has a few design goals:
|
9
|
+
|
10
|
+
- It is very small on purpose, with only a small DSL to define the states and events.
|
11
|
+
- Each event is handled by a simple Ruby class, which only defines the transitions it allows and modifies the state's attribute. These can also just be executed as Plain Old Ruby Objects outside of the state machine's context.
|
12
|
+
- No other logic (callbacks, guards, persistence, error handling) is supplied. If you need them you can implement it inside the event classes using regular Ruby code in any way you prefer.
|
13
|
+
- This prevents your main class from being poluted with a lot of state machine related logic, methods, callbacks, conditions, etc. It only defines the state machine.
|
14
|
+
|
15
|
+
## Getting Started
|
16
|
+
|
17
|
+
Tiny State is not published to Rubygems yet.
|
18
|
+
|
19
|
+
Add it to your Gemfile like so:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
gem "tiny_state", github: "djfpaagman/tiny_state"
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
This example implements a state machine with three states: `new`, `published` and `rejected`. It has two events: `publish` and `reject`. The `publish` event can only be triggered from the `new` and `rejected` states, and the `reject` event can only be triggered from the `new` state, so you can't reject a post that is already published.
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class Post
|
31
|
+
# include the TinyState module in your class.
|
32
|
+
include TinyState
|
33
|
+
|
34
|
+
# set up a state attribute, usually this will come from something like your Rails model.
|
35
|
+
attr_accessor :state
|
36
|
+
|
37
|
+
def initialize(state: :initial)
|
38
|
+
@state = state
|
39
|
+
end
|
40
|
+
|
41
|
+
# define the state machine with the `tiny_state` method.
|
42
|
+
tiny_state do
|
43
|
+
state :new # define one state at a time
|
44
|
+
state [:approved, :published] # or multiple at once
|
45
|
+
|
46
|
+
# define each event with the class that handles it.
|
47
|
+
event :publish, PublishPost
|
48
|
+
event :reject, RejectPost
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# define the event classes that handle the transitions, they need to inherit from `TinyState::Event`.
|
53
|
+
class PublishPost < TinyState::Event
|
54
|
+
# define the transitions this event allows. It can transition from multiple states to a single state.
|
55
|
+
transitions from: %i[new rejected], to: :published
|
56
|
+
end
|
57
|
+
|
58
|
+
class RejectPost < TinyState::Event
|
59
|
+
transitions from: :new, to: :rejected
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
`tiny_state` takes a block and only defines the possible states and events. The `state` methods defines possible states, and the `event` method defines possible events. The event class is passed as a second argument.
|
64
|
+
|
65
|
+
Note that the `state` attribute itself is **not** defined by Tiny State. You need to define it yourself in your class or model. There is also no default value set by Tiny State.
|
66
|
+
|
67
|
+
This will then add the following event methods to a `Post` instance.
|
68
|
+
|
69
|
+
- `#publish?` and `#publish!`
|
70
|
+
- `#reject?` and `#reject!`
|
71
|
+
|
72
|
+
The `#question_mark?` methods will return `true` or `false` depending on whether the transition is allowed. These methods are also used internally to check if we can transition the state.
|
73
|
+
|
74
|
+
The `#bang!` methods will raise an exception if the transition is not allowed, if it is they will change the `state` attribute on the instance to the new value. This will not trigger any database update or other side effects, that is left up to you to implement.
|
75
|
+
|
76
|
+
### Defining your states
|
77
|
+
|
78
|
+
You can define states with the `state` method, eiter individually or multiple states at once. States are deduplicated automatically.
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
class Post
|
82
|
+
include TinyState
|
83
|
+
|
84
|
+
tiny_state do
|
85
|
+
state :new
|
86
|
+
state [:approved, :rejected]
|
87
|
+
end
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
91
|
+
You can define multiple state machines on a single class, each with their own attribute. If you redefine a state machine, the previous one is overwritten.
|
92
|
+
|
93
|
+
### Defining your events
|
94
|
+
|
95
|
+
You define events with the `event` method. Each event is defined with a class that handles that event.
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
class Post
|
99
|
+
include TinyState
|
100
|
+
|
101
|
+
tiny_state do
|
102
|
+
# ...
|
103
|
+
event :publish, PublishPost
|
104
|
+
end
|
105
|
+
end
|
106
|
+
```
|
107
|
+
|
108
|
+
This class should inherit from `TinyState::Event`. The Event class takes one configuration that defines which transitions it allows. This is checked before transitioning the state. If the transition is not allowed, an `TinyState::InvalidTransitionError` exception is raised.
|
109
|
+
|
110
|
+
You can define multiple transitions with the `transitions` method. This takes a `from` and `to` keyword argument. The `from` argument can be a single state or an array of states, and the `to` argument is the (single) state the event transitions to.
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
class PublishPost < TinyState::Event
|
114
|
+
transitions from: [:new, :rejected], to: :published
|
115
|
+
end
|
116
|
+
```
|
117
|
+
|
118
|
+
### Exposed state machines
|
119
|
+
|
120
|
+
To be able to peek inside the state machines, the `#tiny_state_machines` and `#tiny_state_machine` methods are added to the instance of your class.
|
121
|
+
|
122
|
+
`#tiny_state_machines` returns a hash with the field as the key and a `TinyState::Machine` instance as the value, which contains the defined states and events.
|
123
|
+
|
124
|
+
As a shortcut the singular `#tiny_state_machine` is also defined, which returns the machine for the first state machine you define or the machine for a specific field if you give it an argument.
|
125
|
+
|
126
|
+
```ruby
|
127
|
+
post = Post.new # from the example above
|
128
|
+
|
129
|
+
post.tiny_state_machine.states
|
130
|
+
# => #<TinyState::StateMachine:0x00000000000ff0 :state, states: [:initial, :approved, :rejected], events: [:approve, :reject]>
|
131
|
+
|
132
|
+
post.tiny_state_machine(:state)
|
133
|
+
# => #<TinyState::StateMachine:0x00000000000ff0 :state, states: [:initial, :approved, :rejected], events: [:approve, :reject]>
|
134
|
+
```
|
135
|
+
|
136
|
+
The `TinyState::StateMachine` instance has the following methods:
|
137
|
+
|
138
|
+
- `#attribute` returns the attribute the state machine is defined on.
|
139
|
+
- `#states` returns an array of the defined states.
|
140
|
+
- `#events` returns a hash with the event names as keys and the event classes as values.
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
post = Post.new # from the example above
|
144
|
+
|
145
|
+
post.tiny_state_machine.states
|
146
|
+
# => [:new, :published, :rejected]
|
147
|
+
|
148
|
+
post.tiny_state_machine.events
|
149
|
+
# => [:publish, :reject]
|
150
|
+
|
151
|
+
post.tiny_state_machine.attribute
|
152
|
+
# => :state
|
153
|
+
```
|
154
|
+
|
155
|
+
### Using a different field for state
|
156
|
+
|
157
|
+
By default Tiny State uses the `state` field on your resource. If you want to use a different field, you can supply that as an option when defining the state machine:
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
class Post
|
161
|
+
# ...
|
162
|
+
|
163
|
+
tiny_state :status do
|
164
|
+
# ...
|
165
|
+
end
|
166
|
+
end
|
167
|
+
```
|
168
|
+
|
169
|
+
### Adding extra transition logic
|
170
|
+
|
171
|
+
If you want to extend the logic that allows a transition, you can do so by overriding the `#transition?` method in the event class. Be sure to always call `super` to ensure the transition state checks defined in the event itself are also performed.
|
172
|
+
|
173
|
+
```ruby
|
174
|
+
class PublishPost < TinyState::Event
|
175
|
+
# ...
|
176
|
+
|
177
|
+
def transition?
|
178
|
+
# Add your own logic here, for example:
|
179
|
+
super && some_other_condition?
|
180
|
+
end
|
181
|
+
|
182
|
+
private
|
183
|
+
|
184
|
+
def some_other_condition?
|
185
|
+
# ...
|
186
|
+
end
|
187
|
+
end
|
188
|
+
```
|
189
|
+
|
190
|
+
### Implementing side effects
|
191
|
+
|
192
|
+
If you want to implement side effects before or after a transition, you can do so by overriding the `#transition!` method in the event class. Be sure to always call `super` to ensure the transition is allowed and the `state` is changed.
|
193
|
+
|
194
|
+
```ruby
|
195
|
+
class PublishPost < TinyState::Event
|
196
|
+
# ...
|
197
|
+
|
198
|
+
def transition!
|
199
|
+
super
|
200
|
+
# Add your own logic here, for example:
|
201
|
+
log_publication!
|
202
|
+
end
|
203
|
+
|
204
|
+
private
|
205
|
+
|
206
|
+
def log_publication!
|
207
|
+
# ...
|
208
|
+
end
|
209
|
+
end
|
210
|
+
```
|
211
|
+
|
212
|
+
If you don't call `super` in your `transition!` method, it won't check if the transition is allowed and won't change the `state` attribute. If you are fine with that, you can still call `change_state!` to change the state at the moment you want.
|
213
|
+
|
214
|
+
**Note**: if you plan to have any asynchronous side effects like sending emails or queueing jobs, you should make sure they are fired after the resource is saved and the transaction is committed. This is because the state change is not persisted until the transaction is committed. I recommend to use the [after_commit_everywhere](https://github.com/Envek/after_commit_everywhere) gem for this.
|
215
|
+
|
216
|
+
### Using the object or resource itself
|
217
|
+
|
218
|
+
The object itself is always referenceable through `resource`. This can be used if you want to access the object in the event class.
|
219
|
+
|
220
|
+
```ruby
|
221
|
+
class PublishPost < TinyState::Event
|
222
|
+
# ...
|
223
|
+
|
224
|
+
def transition!
|
225
|
+
# ...
|
226
|
+
resource.published_at = Time.now
|
227
|
+
# ...
|
228
|
+
end
|
229
|
+
end
|
230
|
+
```
|
231
|
+
|
232
|
+
If you want to use a different name for the resource, for example to match the type of resource you pass in, you can alias the method or define a simple one line method that refers to `resource`.
|
233
|
+
|
234
|
+
```ruby
|
235
|
+
class PublishPost < TinyState::Event
|
236
|
+
# ...
|
237
|
+
|
238
|
+
alias_method :post, :resource
|
239
|
+
# or
|
240
|
+
def post = resource
|
241
|
+
end
|
242
|
+
```
|
243
|
+
|
244
|
+
## Contributing
|
245
|
+
|
246
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/djfpaagman/tiny_state.
|
data/Rakefile
ADDED
data/config/quickdraw.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require "active_support/core_ext/class/attribute"
|
2
|
+
|
3
|
+
module TinyState
|
4
|
+
class Event
|
5
|
+
class_attribute :from_states, instance_predicate: false
|
6
|
+
class_attribute :to_state, instance_predicate: false
|
7
|
+
attr_reader :resource, :attribute
|
8
|
+
|
9
|
+
def self.transitions(from:, to:)
|
10
|
+
self.from_states = [*from]
|
11
|
+
self.to_state = to
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(resource:, attribute:)
|
15
|
+
@resource = resource
|
16
|
+
@attribute = attribute
|
17
|
+
end
|
18
|
+
|
19
|
+
def transition!
|
20
|
+
if !transition?
|
21
|
+
raise InvalidTransitionError, "Invalid transition from #{current_state} to #{to_state}. Expected #{from_states}."
|
22
|
+
end
|
23
|
+
|
24
|
+
change_state!
|
25
|
+
end
|
26
|
+
|
27
|
+
def transition? = from_states.include?(current_state)
|
28
|
+
|
29
|
+
def change_state! = @resource.send(:"#{attribute}=", to_state)
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def current_state = @resource.send(attribute)
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module TinyState
|
2
|
+
class StateMachine
|
3
|
+
attr_reader :attribute
|
4
|
+
|
5
|
+
def initialize(attribute:)
|
6
|
+
@attribute = attribute
|
7
|
+
@states = Set.new
|
8
|
+
@events = {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def states = @states.to_a
|
12
|
+
|
13
|
+
def events = @events.keys
|
14
|
+
|
15
|
+
def inspect
|
16
|
+
"#<#{self.class}:#{"%#016x" % (object_id << 1)} :#{attribute}, states: #{states}, events: #{events}>"
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def state(state)
|
22
|
+
@states.merge([*state].map(&:to_sym))
|
23
|
+
end
|
24
|
+
|
25
|
+
def event(name, klass)
|
26
|
+
@events[name.to_sym] = klass
|
27
|
+
end
|
28
|
+
|
29
|
+
def register_events(klass)
|
30
|
+
attribute = @attribute
|
31
|
+
|
32
|
+
@events.each do |name, event_klass|
|
33
|
+
klass.define_method(:"#{name}!") do
|
34
|
+
event_klass.new(resource: self, attribute:).transition!
|
35
|
+
end
|
36
|
+
|
37
|
+
klass.define_method(:"#{name}?") do
|
38
|
+
event_klass.new(resource: self, attribute:).transition?
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require "active_support/core_ext/class/attribute"
|
2
|
+
require "active_support/concern"
|
3
|
+
|
4
|
+
module TinyState
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
class_attribute :tiny_state_machines, instance_predicate: false, default: {}
|
9
|
+
end
|
10
|
+
|
11
|
+
class_methods do
|
12
|
+
def tiny_state(attribute = :state, &)
|
13
|
+
state_machine = TinyState::StateMachine.new(attribute:)
|
14
|
+
|
15
|
+
state_machine.instance_eval(&)
|
16
|
+
state_machine.send(:register_events, self)
|
17
|
+
|
18
|
+
tiny_state_machines[attribute] = state_machine
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def tiny_state_machine(attribute = nil)
|
23
|
+
if attribute
|
24
|
+
self.class.tiny_state_machines[attribute]
|
25
|
+
else
|
26
|
+
self.class.tiny_state_machines.values.first
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/tiny_state.rb
ADDED
data/tiny_state.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/tiny_state/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "tiny_state"
|
7
|
+
spec.version = TinyState::VERSION
|
8
|
+
spec.authors = ["Dennis Paagman"]
|
9
|
+
spec.email = ["dennis@paagman.dev"]
|
10
|
+
|
11
|
+
spec.summary = "A tiny Ruby state machine using Plain Ruby Objects (PORO)."
|
12
|
+
spec.description = "A tiny Ruby state machine using Plain Ruby Objects (PORO)."
|
13
|
+
spec.homepage = "https://github.com/djfpaagman/tiny_state"
|
14
|
+
spec.required_ruby_version = ">= 3.3.0"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(__dir__) do
|
22
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
23
|
+
(File.expand_path(f) == __FILE__) ||
|
24
|
+
f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
|
25
|
+
end
|
26
|
+
end
|
27
|
+
spec.bindir = "exe"
|
28
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ["lib"]
|
30
|
+
|
31
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
32
|
+
|
33
|
+
spec.add_dependency "activesupport", "~> 7.1"
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tiny_state
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dennis Paagman
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-05-21 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: '7.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '7.1'
|
27
|
+
description: A tiny Ruby state machine using Plain Ruby Objects (PORO).
|
28
|
+
email:
|
29
|
+
- dennis@paagman.dev
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".standard.yml"
|
35
|
+
- ".tool-versions"
|
36
|
+
- CODE_OF_CONDUCT.md
|
37
|
+
- LICENSE.txt
|
38
|
+
- README.md
|
39
|
+
- Rakefile
|
40
|
+
- config/quickdraw.rb
|
41
|
+
- lib/tiny_state.rb
|
42
|
+
- lib/tiny_state/errors.rb
|
43
|
+
- lib/tiny_state/event.rb
|
44
|
+
- lib/tiny_state/state_machine.rb
|
45
|
+
- lib/tiny_state/tiny_state.rb
|
46
|
+
- lib/tiny_state/version.rb
|
47
|
+
- tiny_state.gemspec
|
48
|
+
homepage: https://github.com/djfpaagman/tiny_state
|
49
|
+
licenses:
|
50
|
+
- MIT
|
51
|
+
metadata:
|
52
|
+
homepage_uri: https://github.com/djfpaagman/tiny_state
|
53
|
+
rubygems_mfa_required: 'true'
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 3.3.0
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubygems_version: 3.5.9
|
70
|
+
signing_key:
|
71
|
+
specification_version: 4
|
72
|
+
summary: A tiny Ruby state machine using Plain Ruby Objects (PORO).
|
73
|
+
test_files: []
|