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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2ebf6e84cf86743ab926a166de6477906ad46be9
4
- data.tar.gz: 787d61de37b561637bf53cb16de85c87e272f124
3
+ metadata.gz: 105f8a5aeb999d07eaeb59d69bbe52ff42317895
4
+ data.tar.gz: b6ac7988cea6d0a5a260ceda12be3c1ca64b38d5
5
5
  SHA512:
6
- metadata.gz: c9641ecdabfeb6df4359887a1de93a81df466965f5a1dd7c2343f23600a9829da270347733b145489d2e4d20befe7c3555e7fc7378a00c52d626681fc5814dae
7
- data.tar.gz: ae0250320f5c320a295e41200e6ba76cb315810fe0c951de7ec9e7daa8add7fcfff74479db7534fa93ff0f530db3c45d43d55908601724a762fcb98913a8a090
6
+ metadata.gz: 00608fb54abb049e5615c96c0903e6564a565838c4cf6db53da2151fefef2ffdabf529b18752eae466bedcd22e26237a44fb1ce1af25fcc0f1494e3164d0454a
7
+ data.tar.gz: 7715676c1ea620c9509e470e285474f31fa991795abc60bb9bd45f692d90bc10453a33918567eb3a11c0cbb19a027ecf03822684b859f0d106df4a481db1c748
data/.rubocop.yml CHANGED
@@ -13,6 +13,9 @@ StringLiterals:
13
13
  Documentation:
14
14
  Enabled: false
15
15
 
16
+ SignalException:
17
+ Enabled: false
18
+
16
19
  # Avoid methods longer than 30 lines of code
17
20
  MethodLength:
18
21
  CountComments: false # count full line comments?
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## v0.3.0, 20 February 2014
2
+ *Additions*
3
+ - Adds Machine#allowed_transitions method (patch by [@prikha](https://github.com/prikha))
4
+
1
5
  ## v0.2.1, 31 December 2013
2
6
  *Fixes*
3
7
  - Don't add attr_accessible to generated transition model if running in Rails 4
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"
@@ -26,6 +26,5 @@ module Statesman
26
26
  def parent_id
27
27
  parent.underscore + "_id"
28
28
  end
29
-
30
29
  end
31
30
  end
@@ -21,16 +21,32 @@ module Statesman
21
21
  end
22
22
 
23
23
  def applies_to?(options = { from: nil, to: nil })
24
- from = options[:from]
25
- to = options[:to]
26
- # rubocop:disable RedundantSelf
27
- (self.from.nil? && self.to.nil?) ||
28
- (from.nil? && to == self.to) ||
29
- (self.from.nil? && to == self.to) ||
30
- (from == self.from && to.nil?) ||
31
- (from == self.from && self.to.nil?) ||
32
- (from == self.from && to == self.to)
33
- # rubocop:enable RedundantSelf
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
@@ -3,13 +3,11 @@ require_relative "exceptions"
3
3
 
4
4
  module Statesman
5
5
  class Guard < Callback
6
-
7
6
  def call(*args)
8
7
  unless super(*args)
9
8
  raise GuardFailedError,
10
9
  "Guard on transition from: '#{from}' to '#{to}' returned false"
11
10
  end
12
11
  end
13
-
14
12
  end
15
13
  end
@@ -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
- "Cannont transition away from terminal state '#{from}'"
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
- "Cannont transition to initial state '#{to}'"
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
@@ -1,3 +1,3 @@
1
1
  module Statesman
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -1,7 +1,7 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Statesman::Callback do
4
- let(:cb_lambda) { -> { } }
4
+ let(:cb_lambda) { -> {} }
5
5
  let(:callback) do
6
6
  Statesman::Callback.new(from: nil, to: nil, callback: cb_lambda)
7
7
  end
@@ -1,7 +1,7 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Statesman::Guard do
4
- let(:callback) { -> { } }
4
+ let(:callback) { -> {} }
5
5
  let(:guard) { Statesman::Guard.new(from: nil, to: nil, callback: callback) }
6
6
 
7
7
  specify { expect(guard).to be_a(Statesman::Callback) }
@@ -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" }
@@ -17,10 +17,10 @@ class MyMongoidModelTransition
17
17
  field :sort_key, type: Integer
18
18
  field :statesman_metadata, type: Hash
19
19
 
20
- index({ sort_key: 1 })
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.12.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.2.1
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: 2013-12-31 00:00:00.000000000 Z
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.12.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.12.0
83
+ version: 0.18.1
84
84
  - !ruby/object:Gem::Dependency
85
85
  name: guard-rubocop
86
86
  requirement: !ruby/object:Gem::Requirement