steady_state 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 02e5f7eb48f3ada429e0d38598535c77489ab57172d920c2dbb6083a19dc5466
4
- data.tar.gz: bbc957eeed78c83b1a7e2c49395df741ba34eea99d3d4adaabdbbabe1298bb06
3
+ metadata.gz: 138a31884498cda0cd7785af5ba7a13c59c71459a2970680652edc20bb9a42e8
4
+ data.tar.gz: 224574b15d9664ea395a4c38656bfa9d3c3ddb8fe100018a60cf30b2f35a4422
5
5
  SHA512:
6
- metadata.gz: a2f28a60fae258f08f3bb57925419c86015d4dc5d738667fffe8e943223fe3603a0a90960653ff2b1d171dac4f077e50b113b4f5b5024da5cb5c8f58a950a096
7
- data.tar.gz: 3fd2023e60bb8ff68c5ea40dcc4a263184fe0698ff3d3507dd7b02ecacf8417bcb9e9b0f44c1d789513994ac5010211b1c50716cdedc7f4eb39f21213a481f5f
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.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
 
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SteadyState
4
- VERSION = '1.1.0'
4
+ VERSION = '1.2.0'
5
5
  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: 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-01-04 00:00:00.000000000 Z
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.1
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"