statesman 0.2.1 → 0.3.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/.rubocop.yml +3 -0
- data/CHANGELOG.md +4 -0
- data/README.md +6 -0
- data/lib/generators/statesman/active_record_transition_generator.rb +1 -1
- data/lib/generators/statesman/mongoid_transition_generator.rb +0 -1
- data/lib/statesman/callback.rb +26 -10
- data/lib/statesman/guard.rb +0 -2
- data/lib/statesman/machine.rb +12 -2
- data/lib/statesman/version.rb +1 -1
- data/spec/statesman/callback_spec.rb +1 -1
- data/spec/statesman/guard_spec.rb +1 -1
- data/spec/statesman/machine_spec.rb +35 -0
- data/spec/support/mongoid.rb +2 -2
- data/statesman.gemspec +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 105f8a5aeb999d07eaeb59d69bbe52ff42317895
|
4
|
+
data.tar.gz: b6ac7988cea6d0a5a260ceda12be3c1ca64b38d5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 00608fb54abb049e5615c96c0903e6564a565838c4cf6db53da2151fefef2ffdabf529b18752eae466bedcd22e26237a44fb1ce1af25fcc0f1494e3164d0454a
|
7
|
+
data.tar.gz: 7715676c1ea620c9509e470e285474f31fa991795abc60bb9bd45f692d90bc10453a33918567eb3a11c0cbb19a027ecf03822684b859f0d106df4a481db1c748
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -69,6 +69,9 @@ end
|
|
69
69
|
Order.first.state_machine.current_state
|
70
70
|
# => "pending"
|
71
71
|
|
72
|
+
Order.first.state_machine.allowed_transitions
|
73
|
+
# => ["checking_out", "cancelled"]
|
74
|
+
|
72
75
|
Order.first.state_machine.can_transition_to?(:cancelled)
|
73
76
|
# => true/false
|
74
77
|
|
@@ -201,6 +204,9 @@ Returns a sorted array of all transition objects.
|
|
201
204
|
#### `Machine#last_transition`
|
202
205
|
Returns the most recent transition object.
|
203
206
|
|
207
|
+
#### `Machine#allowed_transitions`
|
208
|
+
Returns an array of states you can `transition_to` from current state.
|
209
|
+
|
204
210
|
#### `Machine#can_transition_to?(:state)`
|
205
211
|
Returns true if the current state can transition to the passed state and all
|
206
212
|
applicable guards pass.
|
@@ -2,7 +2,7 @@ require "rails/generators"
|
|
2
2
|
|
3
3
|
module Statesman
|
4
4
|
class ActiveRecordTransitionGenerator < Rails::Generators::Base
|
5
|
-
desc "Create an ActiveRecord-based transition model"
|
5
|
+
desc "Create an ActiveRecord-based transition model"\
|
6
6
|
"with the required attributes"
|
7
7
|
|
8
8
|
argument :parent, type: :string, desc: "Your parent model name"
|
data/lib/statesman/callback.rb
CHANGED
@@ -21,16 +21,32 @@ module Statesman
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def applies_to?(options = { from: nil, to: nil })
|
24
|
-
from
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
(from
|
32
|
-
(from
|
33
|
-
|
24
|
+
matches(options[:from], options[:to])
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def matches(from, to)
|
30
|
+
matches_all_transitions ||
|
31
|
+
matches_to_state(from, to) ||
|
32
|
+
matches_from_state(from, to) ||
|
33
|
+
matches_both_states(from, to)
|
34
|
+
end
|
35
|
+
|
36
|
+
def matches_all_transitions
|
37
|
+
from.nil? && to.nil?
|
38
|
+
end
|
39
|
+
|
40
|
+
def matches_from_state(from, to)
|
41
|
+
(from == self.from && (to.nil? || self.to.nil?))
|
42
|
+
end
|
43
|
+
|
44
|
+
def matches_to_state(from, to)
|
45
|
+
((from.nil? || self.from.nil?) && to == self.to)
|
46
|
+
end
|
47
|
+
|
48
|
+
def matches_both_states(from, to)
|
49
|
+
from == self.from && to == self.to
|
34
50
|
end
|
35
51
|
end
|
36
52
|
end
|
data/lib/statesman/guard.rb
CHANGED
data/lib/statesman/machine.rb
CHANGED
@@ -98,7 +98,7 @@ module Statesman
|
|
98
98
|
def validate_not_from_terminal_state(from)
|
99
99
|
unless from.nil? || successors.keys.include?(from)
|
100
100
|
raise InvalidTransitionError,
|
101
|
-
"
|
101
|
+
"Cannot transition away from terminal state '#{from}'"
|
102
102
|
end
|
103
103
|
end
|
104
104
|
|
@@ -106,7 +106,7 @@ module Statesman
|
|
106
106
|
def validate_not_to_initial_state(to)
|
107
107
|
unless to.nil? || successors.values.flatten.include?(to)
|
108
108
|
raise InvalidTransitionError,
|
109
|
-
"
|
109
|
+
"Cannot transition to initial state '#{to}'"
|
110
110
|
end
|
111
111
|
end
|
112
112
|
|
@@ -152,6 +152,12 @@ module Statesman
|
|
152
152
|
last_action ? last_action.to_state : self.class.initial_state
|
153
153
|
end
|
154
154
|
|
155
|
+
def allowed_transitions
|
156
|
+
successors_for(current_state).select do |state|
|
157
|
+
can_transition_to?(state)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
155
161
|
def last_transition
|
156
162
|
@storage_adapter.last
|
157
163
|
end
|
@@ -193,6 +199,10 @@ module Statesman
|
|
193
199
|
|
194
200
|
private
|
195
201
|
|
202
|
+
def successors_for(from)
|
203
|
+
self.class.successors[from] || []
|
204
|
+
end
|
205
|
+
|
196
206
|
def guards_for(options = { from: nil, to: nil })
|
197
207
|
select_callbacks_for(self.class.guards, options)
|
198
208
|
end
|
data/lib/statesman/version.rb
CHANGED
@@ -235,6 +235,41 @@ describe Statesman::Machine do
|
|
235
235
|
end
|
236
236
|
end
|
237
237
|
|
238
|
+
describe "#allowed_transitions" do
|
239
|
+
before do
|
240
|
+
machine.class_eval do
|
241
|
+
state :x, initial: true
|
242
|
+
state :y
|
243
|
+
state :z
|
244
|
+
transition from: :x, to: [:y, :z]
|
245
|
+
transition from: :y, to: :z
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
let(:instance) { machine.new(my_model) }
|
250
|
+
subject { instance.allowed_transitions }
|
251
|
+
|
252
|
+
context "with multiple possible states" do
|
253
|
+
it { should eq(%w(y z)) }
|
254
|
+
end
|
255
|
+
|
256
|
+
context "with one possible state" do
|
257
|
+
before do
|
258
|
+
instance.transition_to!(:y)
|
259
|
+
end
|
260
|
+
|
261
|
+
it { should eq(['z']) }
|
262
|
+
end
|
263
|
+
|
264
|
+
context "with no possible transitions" do
|
265
|
+
before do
|
266
|
+
instance.transition_to!(:z)
|
267
|
+
end
|
268
|
+
|
269
|
+
it { should eq([]) }
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
238
273
|
describe "#last_transition" do
|
239
274
|
let(:instance) { machine.new(my_model) }
|
240
275
|
let(:last_action) { "Whatever" }
|
data/spec/support/mongoid.rb
CHANGED
@@ -17,10 +17,10 @@ class MyMongoidModelTransition
|
|
17
17
|
field :sort_key, type: Integer
|
18
18
|
field :statesman_metadata, type: Hash
|
19
19
|
|
20
|
-
index(
|
20
|
+
index(sort_key: 1)
|
21
21
|
|
22
22
|
belongs_to :my_mongoid_model, index: true
|
23
23
|
|
24
24
|
alias_method :metadata, :statesman_metadata
|
25
25
|
alias_method :metadata=, :statesman_metadata=
|
26
|
-
end
|
26
|
+
end
|
data/statesman.gemspec
CHANGED
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.add_development_dependency "rake"
|
23
23
|
spec.add_development_dependency "rspec", "~> 2.14.1"
|
24
24
|
spec.add_development_dependency "guard-rspec", "~> 3.0.2"
|
25
|
-
spec.add_development_dependency "rubocop", "~> 0.
|
25
|
+
spec.add_development_dependency "rubocop", "~> 0.18.1"
|
26
26
|
spec.add_development_dependency "guard-rubocop", "~> 0.2.2"
|
27
27
|
spec.add_development_dependency "activerecord", "~> 3.2"
|
28
28
|
spec.add_development_dependency "sqlite3", "~> 1.3.8"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: statesman
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Harry Marr
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2014-02-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -73,14 +73,14 @@ dependencies:
|
|
73
73
|
requirements:
|
74
74
|
- - ~>
|
75
75
|
- !ruby/object:Gem::Version
|
76
|
-
version: 0.
|
76
|
+
version: 0.18.1
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
79
|
version_requirements: !ruby/object:Gem::Requirement
|
80
80
|
requirements:
|
81
81
|
- - ~>
|
82
82
|
- !ruby/object:Gem::Version
|
83
|
-
version: 0.
|
83
|
+
version: 0.18.1
|
84
84
|
- !ruby/object:Gem::Dependency
|
85
85
|
name: guard-rubocop
|
86
86
|
requirement: !ruby/object:Gem::Requirement
|