steady_state 1.1.0 → 2.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 02e5f7eb48f3ada429e0d38598535c77489ab57172d920c2dbb6083a19dc5466
4
- data.tar.gz: bbc957eeed78c83b1a7e2c49395df741ba34eea99d3d4adaabdbbabe1298bb06
3
+ metadata.gz: 4824f112c40684778015b921d582801f2b95c134ed2881ed8c87e346a2bfcad2
4
+ data.tar.gz: 64837c8febda4b8b4adce2261e7a5a8b43014bdc5e09b3dec8c2543d86b0f6e1
5
5
  SHA512:
6
- metadata.gz: a2f28a60fae258f08f3bb57925419c86015d4dc5d738667fffe8e943223fe3603a0a90960653ff2b1d171dac4f077e50b113b4f5b5024da5cb5c8f58a950a096
7
- data.tar.gz: 3fd2023e60bb8ff68c5ea40dcc4a263184fe0698ff3d3507dd7b02ecacf8417bcb9e9b0f44c1d789513994ac5010211b1c50716cdedc7f4eb39f21213a481f5f
6
+ metadata.gz: f3b3ba6cc7e0317163d8b65c3a0f4033f34f6b5066c09c2ae8af7c6daf380943f03a2a49710142675931d23ff6b8037d033cb489562fac514142558d727ad09d
7
+ data.tar.gz: c8c835c627ee902f045dd72188ed26f4112cf9722ff8827c1bae3589029141ff8e1c8d7bed8d0a18bf3aabb8b39cd3224c6b211ca3bde95d3deedb51a69ebba4
data/README.md CHANGED
@@ -210,6 +210,22 @@ steady_state :step, scopes: false do
210
210
  end
211
211
  ```
212
212
 
213
+ `steady_state` also follows the same `prefix` api as `delegate` in Rails. You may optionally define your scopes to be prefixed to the name of the state machine with `prefix: true`, or you may provide a custom prefix with `prefix: :some_custom_name`. This may be useful when dealing with multiple state machines on one object.
214
+
215
+ ```ruby
216
+ steady_state :temperature, scopes: { prefix: true } do
217
+ state 'cold', default: true
218
+ end
219
+
220
+ steady_state :color_temperature, scopes: { prefix: 'color' } do
221
+ state 'cold', default: true
222
+ end
223
+
224
+ Material.solid # => query for 'solid' records
225
+ Material.temperature_cold # => query for records with a cold temperature
226
+ Material.color_cold # => query for for records with a cold color temperature
227
+ ```
228
+
213
229
  ### Next and Previous States
214
230
 
215
231
  The `may_become?` method can be used to see if setting the state to a particular value would be allowed (ignoring all other validations):
@@ -277,7 +293,7 @@ class Material
277
293
  self.state = 'liquid'
278
294
  valid? # will return `false` if state transition is invalid
279
295
  end
280
-
296
+
281
297
  def melt!
282
298
  self.state = 'liquid'
283
299
  validate! # will raise an exception if state transition is invalid
@@ -56,13 +56,24 @@ module SteadyState
56
56
 
57
57
  delegate(*state_machines[attr_name].predicates, to: attr_name, allow_nil: true) if predicates
58
58
  if scopes
59
+ scopes = {} unless scopes.is_a?(Hash)
60
+ prefix = SteadyState::Attribute.build_prefix(attr_name, **scopes)
61
+
59
62
  state_machines[attr_name].states.each do |state|
60
- scope state.to_sym, -> { where(attr_name.to_sym => state) }
63
+ scope :"#{prefix}#{state}", -> { where(attr_name.to_sym => state) }
61
64
  end
62
65
  end
63
66
 
64
67
  validates :"#{attr_name}", 'steady_state/attribute/transition' => true,
65
- inclusion: { in: state_machines[attr_name].states }
68
+ inclusion: { in: state_machines[attr_name].states }
69
+ end
70
+ end
71
+
72
+ def self.build_prefix(attr_name, prefix: false)
73
+ if prefix
74
+ "#{prefix == true ? attr_name : prefix}_"
75
+ else
76
+ ""
66
77
  end
67
78
  end
68
79
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SteadyState
4
- VERSION = '1.1.0'
4
+ VERSION = '2.2.0'
5
5
  end
@@ -0,0 +1,49 @@
1
+ # typed: true
2
+
3
+ # Sorbet annotations for SteadyState's DSL, shipped with the gem so that
4
+ # Tapioca merges them into consumers' generated gem RBIs.
5
+ #
6
+ # The key annotation is the `T.proc.bind` on `steady_state`'s block: the
7
+ # block is `instance_eval`'d against a `StateMachine`, so without the bind,
8
+ # Sorbet resolves `state` calls against the including class and errors.
9
+
10
+ module SteadyState
11
+ mixes_in_class_methods SteadyState::Attribute::ClassMethods
12
+ end
13
+
14
+ module SteadyState::Attribute
15
+ mixes_in_class_methods SteadyState::Attribute::ClassMethods
16
+ end
17
+
18
+ module SteadyState::Attribute::ClassMethods
19
+ sig do
20
+ params(
21
+ attr_name: T.any(Symbol, String),
22
+ predicates: T::Boolean,
23
+ states_getter: T::Boolean,
24
+ scopes: T.any(T::Boolean, T::Hash[Symbol, T.untyped]),
25
+ block: T.proc.bind(SteadyState::Attribute::StateMachine).void,
26
+ ).void
27
+ end
28
+ def steady_state(attr_name, predicates: true, states_getter: true, scopes: false, &block); end
29
+ end
30
+
31
+ class SteadyState::Attribute::StateMachine
32
+ sig do
33
+ params(
34
+ state: String,
35
+ default: T::Boolean,
36
+ from: T.any(String, T::Array[String]),
37
+ ).void
38
+ end
39
+ def state(state, default: false, from: []); end
40
+
41
+ sig { returns(T.nilable(String)) }
42
+ def start; end
43
+
44
+ sig { returns(T::Array[String]) }
45
+ def states; end
46
+
47
+ sig { returns(T::Array[Symbol]) }
48
+ def predicates; end
49
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: steady_state
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Griffith
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-01-04 00:00:00.000000000 Z
11
+ date: 2026-08-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '5.2'
19
+ version: '8.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '5.2'
26
+ version: '8.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '5.2'
33
+ version: '8.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '5.2'
40
+ version: '8.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: appraisal
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: sorbet
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
97
111
  description: |
98
112
  A minimalist approach to managing object state,
99
113
  perhaps best described as "an enum with guard rails."
@@ -113,6 +127,7 @@ files:
113
127
  - lib/steady_state/attribute/state_machine.rb
114
128
  - lib/steady_state/attribute/transition_validator.rb
115
129
  - lib/steady_state/version.rb
130
+ - rbi/steady_state.rbi
116
131
  homepage:
117
132
  licenses: []
118
133
  metadata:
@@ -125,14 +140,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
125
140
  requirements:
126
141
  - - ">="
127
142
  - !ruby/object:Gem::Version
128
- version: '3.0'
143
+ version: '3.3'
129
144
  required_rubygems_version: !ruby/object:Gem::Requirement
130
145
  requirements:
131
146
  - - ">="
132
147
  - !ruby/object:Gem::Version
133
148
  version: '0'
134
149
  requirements: []
135
- rubygems_version: 3.5.1
150
+ rubygems_version: 3.5.22
136
151
  signing_key:
137
152
  specification_version: 4
138
153
  summary: Minimalist state management via "an enum with guard rails"