steady_state 1.1.0 → 1.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 +4 -4
- data/README.md +17 -1
- data/lib/steady_state/attribute.rb +12 -1
- data/lib/steady_state/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 138a31884498cda0cd7785af5ba7a13c59c71459a2970680652edc20bb9a42e8
|
4
|
+
data.tar.gz: 224574b15d9664ea395a4c38656bfa9d3c3ddb8fe100018a60cf30b2f35a4422
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1f7f923baf74fadcfd46973f86a5f67523e8138f65a7bdcdd5e07393468677b96a2e7fafa37c9315099e562d7e467a5d71153152c004bb4c004e1b2224de92ff
|
7
|
+
data.tar.gz: d1669039e0f85a5d5926ac35f344f90cf9039e319162b3a1ce47be3a240969e236d2b07ed6d0e12a573fc175d68c812b0b07e92f52e4b02136aea86622257f8a
|
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,8 +56,11 @@ 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
|
63
|
+
scope :"#{prefix}#{state}", -> { where(attr_name.to_sym => state) }
|
61
64
|
end
|
62
65
|
end
|
63
66
|
|
@@ -65,5 +68,13 @@ module SteadyState
|
|
65
68
|
inclusion: { in: state_machines[attr_name].states }
|
66
69
|
end
|
67
70
|
end
|
71
|
+
|
72
|
+
def self.build_prefix(attr_name, prefix: false)
|
73
|
+
if prefix
|
74
|
+
"#{prefix == true ? attr_name : prefix}_"
|
75
|
+
else
|
76
|
+
""
|
77
|
+
end
|
78
|
+
end
|
68
79
|
end
|
69
80
|
end
|
data/lib/steady_state/version.rb
CHANGED
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.
|
4
|
+
version: 1.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-
|
11
|
+
date: 2024-06-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -132,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
132
132
|
- !ruby/object:Gem::Version
|
133
133
|
version: '0'
|
134
134
|
requirements: []
|
135
|
-
rubygems_version: 3.5.
|
135
|
+
rubygems_version: 3.5.14
|
136
136
|
signing_key:
|
137
137
|
specification_version: 4
|
138
138
|
summary: Minimalist state management via "an enum with guard rails"
|